简体   繁体   中英

Is it possible to get the actual object type if it is assigned to an interface type?

I have an interface ITransaction and Class1, Class2 all implements this interface. At runtime i assign a object of Class1 or Class2 to a reference of ITransaction.

ITransaction itransaction = new Class1(); or

ITransaction itransaction = new Class2();

Is it possible to know the actual type that was assigned to the itransaction reference, may using reflection or something else?

You can call GetType() on the interface instance to get the effective type.

(Note that this may not return what you expect for COM and remote interfaces of MarshalByRefObject instances, but that is a different topic.)

你可以写

if (itransaction is Class2)

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