How access properties of inherited classes from an abstract class

I work with code that has an abstract class, for example:

public abstract class AbstractClass
{
   ...
}

and there are inherited classes from AbstractClass. One object is created depending on the user entering one of the inherited classes. Each inherited class has its own properties:

class classOne : AbstractClass
    {
         ...
         public int A { get; set;}
         public int B { get; set;}
         public int C { get; set;}
         ...
    }


class classTwo : AbstractClass
    {
         ...
         public int D { get; set;}
         public int E { get; set;}
         ...
    }

... Let's say I want to use functions in this code, and I know what type of object will be returned. How to change the properties of the output object, since the program is written so that the output class is determined only when the program starts?

+4
source share
2 answers

If you know what specific type you have, you can always drop your object:

var concreteObject = myObj as classOne;

, , , , :

if (myObj is classOne) {
    // Cast to classOne and use
} else if (myObj is classTwo) {
    // Cast to classTwo and use
}

, !

+4

, , , :

(SomeType)instance
+1

Source: https://habr.com/ru/post/1621908/


All Articles