を行うことができませんが、リフレクションを使用して似たような達成することができます同様のオプションが必要です。ここで(がを更新)あなたの問題には、次の2つの可能なソリューションを提供する例です。
abstract class Foo
{
protected abstract string _ElementName { get; }
public static string GetElementName<T>() where T : Foo, new()
{
return typeof(T).GetProperty("_ElementName", BindingFlags.Instance | BindingFlags.NonPublic)?
.GetValue(new T()) as string;
}
public static string GetStaticElementName<T>() where T : Foo, new()
{
return typeof(T).GetProperty("ElementName", BindingFlags.Static | BindingFlags.NonPublic)?
.GetValue(null) as string;
}
}
class Bar : Foo
{
protected static string ElementName
{
get
{
return "StaticBar";
}
}
protected override string _ElementName
{
get
{
return "Bar";
}
}
}
class FooBar : Bar
{
protected static string ElementName
{
get
{
return "StaticFooBar";
}
}
protected override string _ElementName
{
get
{
return "FooBar";
}
}
}
class Baz<T> where T : Foo, new()
{
public string ElementName
{
get
{
return Foo.GetElementName<T>();
}
}
public string StaticElementName
{
get
{
return Foo.GetStaticElementName<T>();
}
}
}
...
Console.WriteLine(new Baz<Bar>().ElementName); // Bar
Console.WriteLine(new Baz<FooBar>().ElementName); // FooBar
Console.WriteLine(new Baz<Bar>().StaticElementName); // StaticBar
Console.WriteLine(new Baz<FooBar>().StaticElementName); // StaticFooBar
の可能性のある重複した[は、特定の静的関数を実装するためにC#クラスを強制する方法はありますか?](のhttp://のstackoverflow。 com/questions/577749/is-there-a-way-to-force-ac-sharp-class-to-implement-特定の静的関数) –