gpt4 book ai didi

c# - 覆盖静态类成员并通过泛型访问

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:22 26 4
gpt4 key购买 nike

描述我的问题的最简单方法是使用示例代码。我知道这不会编译,但我需要一个类似的选项

abstract class Foo 
{
protected abstract static ElementName {get;}
}
class Bar : Foo
{
protected static override ElementName
{
get
{
return "bar";
}
}
}
class Baz<T> where T : Foo
{
public string ElementName
{
get
{
return T.ElementName;
}
}
}

问候

最佳答案

这不能以您想要的方式完成,但您可以使用反射实现类似的事情。以下示例为您的问题提供了两种可能的解决方案(已更新):

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# - 覆盖静态类成员并通过泛型访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37765650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com