System.Web.UI.Pageから派生した抽象ジェネリッククラスから派生した2つのクラスがあります。2つの派生ページクラスまたはSystem.Web.UI.Pageのいずれかのマスターである場合とそうでない場合があるSystem.Web.UI.MasterPageがあります。
問題は、ジェネリッククラスにMasterPageでアクセスする必要のあるプロパティがあることですが、それを取得するための洗練された方法がわかりません。
ここに例があります...
コンテンツタイプ:
public abstract class Fruit
{
public int ID { get; set; } //Just an identifier
}
public class Apple : Fruit { }
public class Banana : Fruit { }
ページ:
public abstract class FruitPage<T> : System.Web.UI.Page where T : Fruit
{
public T MyFruit { get; set; }
}
public class ApplePage : FruitPage<Apple> { }
public class BananaPage : FruitPage<Banana> { }
マスター:
public partial class FoodMaster : System.Web.UI.MasterPage
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Page is FruitPage<Fruit>) //I know this is wrong
{
if ((this.Page as FruitPage<Fruit>).MyFruit.ID <= 0) //This too
{
/*
I want to get here (this.Page being an ApplePage or BananaPage).
Basically... if ID<=0 then it is a new (unsaved) "fruit",
and I need to change some MasterPage GUI accordingly.
*/
}
}
}
}
ありがとうございました!