简体   繁体   English

我可以调用基于 class 的 static 方法,并参考实现该基础 ZA2F2ED4F8EBC2CBB4C21 的 class 吗?

[英]Can I call a static method that lives on a base class with a reference the the class implementing that base class (in C#)?

I'm having a hard time phrasing the question which is also making it hard for me to search for answers.我很难说出这个问题,这也让我很难寻找答案。

Here's a contrived scenario that mimics what I'd like to do:这是一个模仿我想做的人为场景:

void Main()
{
    Console.WriteLine(TestClassA.MyPropertyName());
    Console.WriteLine(TestClassB.MyPropertyName());
    
    var speaker = new TestSpeaker();
    speaker.Speak<TestClassA>();
    speaker.Speak<TestClassB>();
}

public class TestSpeaker {
    public void Speak<T>() where T : BaseClass<T> {
        Console.WriteLine(/* I want to call T.MyPropertyName() here */);
    }
}

public class TestClassA : BaseClass<TestClassA> {
    public string Name { get; set; }
}

public class TestClassB : BaseClass<TestClassB> {
    public string OtherPropertyName { get; set; }
    
}

public abstract class BaseClass<T> {

    public static string MyPropertyName(){
        return typeof(T).GetProperties().Single().Name;
    }
}

The Console right now would read:控制台现在会显示:

Name
OtherPropertyName

I'd like to replace my commented out code so that it would read:我想替换我注释掉的代码,这样它就会变成:

Name
OtherPropertyName
Name
OtherPropertyName

if you change your Writeline to如果您将 Writeline 更改为

Console.WriteLine(BaseClass<T>.MyPropertyName());

you will get what you want你会得到你想要的

Why use a static function in a base class to retrieve information about a derived class?为什么在基础 class 中使用 static function 来检索有关派生 ZA2F2ED4F8EBC2CBB4DC21A29 的信息? In any case, you could implement a member function to wrap the static call:在任何情况下,您都可以实现成员 function 来包装 static 调用:

public static string MyStaticFunction() => return "whatever";

public string MyMemberFunction() => MyStaticFunction();

But in your scenario, perhaps you should simply declare an abstract property (or function) meant to return the value you're looking for and override it in derived classes:但是在您的场景中,也许您应该简单地声明一个抽象属性(或函数),以返回您正在寻找的值并在派生类中覆盖它:

Base:根据:

public abstract string MyPropertyName { get; }

Derived:衍生的:

public override string MyPropertyName => nameof(OtherPropertyName); // or more complex logic

And yet another possible solution would be to pass the information to the base class's constructor as a string (or property expression should you be so inclined):另一种可能的解决方案是将信息作为字符串传递给基类的构造函数(或属性表达式,如果您愿意的话):

Base:根据:

public string MyPropertyName { get; init; }

public BaseClass(string propertyName)
{
    MyPropertyName = propertyName; // maybe validate that the property exists
}

Derived:衍生的:

public MyTestClassB() : BaseClass(nameof(OtherPropertyName)) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:如何从派生类的静态方法调用基类的静态方法? - C#: How do I call a static method of a base class from a static method of a derived class? C# - 基类中的调用方法 - C# - Call Method in Base Class C#使用基类引用调用派生类属性 - C# call derived class property using base class reference 您能否将派生类添加到其基类列表中,然后从C#中的基类列表中调用派生类的方法 - Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C# C#总是从基类调用基方法 - C# always call a base method from the base class 从基本构造函数C#调用基类重写方法 - Call base class override method from base constructor C# 如何从 C# 中的基础 class 参考中调用派生 class 的通用方法 - How to call generic method of derived class from base class reference in C# C#基类可以在静态成员中调用子类的构造函数吗? - Can a C# base class call a sub class's constructor in a static member? 如何在C#中使用静态工厂方法创建基类? - How to create a base class with static factory method in C#? C# - 使用对基类的引用,如果我知道我的引用是对继承类的引用,我如何访问继承类的属性? - C# - with a reference to a base class, how can I access a property of the inherited class if I know that my reference is to the inherited class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM