简体   繁体   English

从未知类调用已知属性

[英]Calling Known Property from Unknown Class

I have a BaseClass from which I have derived multiple other Classes using Inheritance. 我有一个BaseClass,我从中使用继承派生了多个其他类。 Elsewhere I want to be able to retrieve Properties from anyone of these inherited classes even though the type of Class isn't known until Runtime. 在其他地方,我希望能够从这些继承类中的任何一个中检索属性,即使在运行时之前不知道类的类型。

For example lets say I have two classes derived from BaseClass 例如,假设我有两个派生自BaseClass的类

FirstClass : BaseClass
SecondClass : BaseClass

My BaseClass has a property called ID which is an int (amongst many others) 我的BaseClass有一个名为ID的属性,它是一个int(在许多其他中)

At runtime my app will receive 'either' a FirstClass object or a SecondClass object, but either way I need to retrieve the ID property. 在运行时,我的应用程序将接收'或'FirstClass对象或SecondClass对象,但无论哪种方式我都需要检索ID属性。

public int MyMethod(object unknownClass)
{
    int myID = unknownClass.ID; 
    return myID                    //...does not compile
}

public int MyMethod(object unknownClass)
{
    BaseClass tryCasting = (BaseClass)unknownClass;
    int myID = tryCasting.ID;           
    return myID                    //...does not compile either
}

I'm not sure what else to try. 我不知道还有什么可以尝试的。 How can I get a Property which I know exists without knowing the Object type first? 如何在不首先了解对象类型的情况获取我知道的属性?

Don't use object and use BaseClass : 不要使用对象并使用BaseClass

public int MyMethod(BaseClass unknownClass)
{
    int myID = unknownClass.ID; 
    return myID;                    
}

See the answer from Darren Davies for your specific case of ID. 请参阅Darren Davies的答案,了解您的ID特定案例。 The approaches in this answer are useful when the property is defined in the derived classes, not the base class. 当在派生类中定义属性而不是基类时,此答案中的方法很有用。


Use dynamic instead of object . 使用dynamic而不是object That will use the runtime to retrieve the actual object type. 这将使用运行时来检索实际的对象类型。

public int MyMethod(dynamic unknownClass)
{
    int myID = (int)unknownClass.ID; 
    return myID;
}

Alternatively, you can use Reflection to retrieve the property value: 或者,您可以使用Reflection来检索属性值:

var myID = (int)unknownClass.GetType().GetProperty("ID").GetValue(unknownClass);

Polymorfism is the answer. Polymorfism就是答案。 You should know that when you create a class derived from another, the derived class acquires inmediately the properties from the base class. 您应该知道,当您创建从另一个派生的类时,派生类会立即从基类中获取属性。 Thus, if you don't redefine the method (override), when you call the method MyMethod on a derived class it will always call the base method. 因此,如果您不重新定义方法(覆盖),当您在派生类上调用方法MyMethod时,它将始终调用基本方法。 So the next code should work well : 所以下一个代码应该运行良好:

BaseClass { 
    private : int id ; 
    public  : int getId { return id ; }
} ;

FirstClass : BaseClass { DO NOT OVERRIDE getID ...} ; 

int main (void) { 
    BaseClass base ; 
    FirstClass first ; 
    base = first ; 
    base.getId() ; //Gets the id of first.
}

What does this program? 这个节目是什么? When you assign first to base you upcast first type to BaseClass . 当您first分配到base时,您将first first类型转换为BaseClass Then when you get to the execution of base.getId() the program lookups if there is a redefinition (overriding) of this method in FirstClass, if there isn't it calls the base method. 然后当你执行base.getId() ,如果没有调用base方法,程序将查找是否在FirstClass中重新定义(覆盖)此方法。

Hope that you find this information useful. 希望您发现此信息有用。 You should read the first chapters of "Thinking in C++" of Bruce Eckels, it's a good introduction to Object Oriented Programming. 您应该阅读Bruce Eckels的“Thinking in C ++”的第一章,它是面向对象编程的一个很好的介绍。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM