Is there a way to get a reference to the class of the main body from the interface with C#?

Asked 2 years ago, Updated 2 years ago, 44 views

class Derived: Base,Iinterface1,Iinterface2{
}

Is there a way to get a Derived reference from the IIinterface1 reference when a class like this is defined?

c#

2022-09-30 21:13

1 Answers

I'm not sure what "reference" means, but

Iinterface1obj;

If you have an instance reference with an interface type such as , the Derived type variable is

Derived=(Derived)obj;

as shown in .

Also, if you want type information dynamically, refer to the results of obj.GetType().

If you want to get Type from Type,

AppDomain.CurrentDomain
         .GetAssemblies()
         .SelectMany(a=>a.GetTypes())
         .Where(t=>typeof(IInterface1).IsAssignableFrom(t))

You can search from Assembly.GetTypes as shown in .


2022-09-30 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.