リピーターで使用する場合 (実際には ListView のことを意味しているといいのですが)、インターフェイスでプロパティを定義してから、Deal クラスと Store クラスでインターフェイスを実装します。次に、リストをリピーター/リストビューにバインドし、プロパティを名前で呼び出すことができます。トリックは必要ありません。これにより、インターフェイスはプロパティが使用可能であることを保証します (そうしないと、バインディング中に DataTextvalue が壊れます)。
つまり、ListView にバインドする場合は、Store クラスと Deal クラスの両方で表示プロパティに同じ名前を付ける必要があります。したがって、インターフェイスを最も基本的な形式で使用することもできます。
protected Page_Load(object sender, EventArgs e)
{
var list = new List<IWatchamacallit>();
list.Add(new Store { Property1 = "Store1", Property2 = "StoreInfo"});
list.Add(new Store { Property1 = "Store2", Property2 = "StoreInfo" });
list.Add(new Deal { Property1 = "Deal1", Property2 = "DealInfo" });
list.Add(new Deal { Property1 = "Deal2", Property2 = "DealInfo" });
myListView.DataSource = list;
myListView.DataBind();
/* from here just set your page controls to call the properties
for instance:
<asp:Label Text='<%# Eval("Property1") %>' />
<asp:Label text='<%# Eval("Property2") %>' />
*/
}
public interface IWatchamacallit
{
string Property1 { get; set; }
string Property2 { get; set; }
}
public class Store : IWatchamacallit
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
public class Deal : IWatchamacallit
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
入力は次のようにバインドされます。
Property1 Property1
=====================
Deal1 DealInfo
Deal2 DealInfo
Store1 StoreInfo
Store2 StoreInfo
保持する必要があるその他の値 (dealId や storeId など) は、プロパティとしてクラスに追加できます。インターフェイスでそれらを定義し、一貫した名前を使用していることを確認してください。これにより、クラス構造を維持しながら、2 つの異なる型でリストを埋めることができます。後でリストからそれらを選択する必要がある場合は、次のようにキャストできます。
foreach (var item in list)
{
var tempContainer = Activator.CreateInstance(item.GetType());
tempContainer = item;
}
または、達成しようとしていることに応じて、他のいくつかの方法のいずれかです。