简体   繁体   中英

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?

I'd like to do something like the following with my code:

BaseClass class;

if (BaseClass is InheritedClass)
{
    class.MethodOnlyInInheritedClass();
}

However I get an error that BaseClass does not contain a definition for MethodOnlyInInheritedClass. It doesn't, but I know based on the previous test that my particular reference to BaseClass is an inherited type that DOES contain that method. I tried casting class as InheritedClass but that didn't work.

Is there a way to do what I'm seeking?

Looks to be compile time error does not contain definition for...

Define MethodOnlyInInheritedClass as a virtual method in your base class (or have it as an abstract method in abstract base class) and override same in InheritedClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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