mardi 14 juin 2022

How can I create a class derived from a reflected interface

I would like to call a method in a reflected class that takes an interface also defined in the reflected library

My library is a .NET461 library

public interface InterfaceA{
  ...
}
...
public class ClassA {
  public void MethodA( InterfaceA interface )
  {
    ...
  }  
}

My executable is a .NET48 executable

(removed validation and some code for brevity)

// get the library
var asm = Assembly.LoadFrom( "...myAssembly.dll" );

// instantiate ClassA
var instance = asm.CreateInstance( "ClassA" )

// look for that class type so we can get the method "MethodA"
var class = asm.GetExportedTypes().FirstOrDefault( t => t.Name == "ClassA" ); 

// look for the Method
var method = class.GetMethod( "MethodA");

// look for the interface
var interface = asm.GetExportedTypes().FirstOrDefault( t => t.Name == "Interface" ); 

// how can I call the method here
method.Invoke( instance, new []{ ....} );

I know everything about the interface, I can create an exact class that has the same methods and properties ...

But how can I create the same class in my code, so I can pass it as an argument to the reflected class?





Aucun commentaire:

Enregistrer un commentaire