Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Compose a Class in C#

Is it possible to dynamically compose a class from the methods contained in other Classes?

For instance. Class A, B and C have public methods named such that they can be identified easily. They need to be extracted and added to Class D. Class D, which then contains all of the implementation logic can be passed further into a system which only accepts a single instance of Class D and dynamically binds these methods to other functions.

To be clear, this is not inheritance I'm looking for. I'm literally stating that methods with different names need to be stuffed into one class. The system I pass it into understands these naming conventions and dynamically binds them (it's a black box to me).

I am going down the path of extracting the methods from A, B, and C, dynamically combining them with the source of Class D in memory and compiling them into a new Class D and then creating an instance of D and passing it forward.

public class A{  public void EXPORT_A_DoSomething(){} }
public class B{  public void EXPORT_B_DoSomethingElse(){}}
public class C{  public void EXPORT_C_DoAnything(){}}
 //should become
public class D{ 
          public void EXPORT_A_DoSomething(){} 
          public void EXPORT_B_DoSomethingElse(){}
          public void EXPORT_C_DoAnything(){}
}

Is there a way to extract the MethodInfos from class A, B and C and somehow directly attach them to Class D? If so how?

like image 993
Jonathan M. Avatar asked Mar 18 '13 14:03

Jonathan M.


2 Answers

I would consider using the C# Class compiler. From what I can remember you can build code that is in a string and you can get an assembly as output. This then enables you to invoke methods through reflection.

There is an example on the MSDN link I have specified but I will mock one up for here once I find my project.

like image 183
LukeHennerley Avatar answered Sep 28 '22 02:09

LukeHennerley


You won't be able to export just the methods. The methods can't really be separated from the class that they are in. (they need access to all of the member fields/properites including inherited).

I think the only thing you can do is emit a interface implementation. (even though you say it's not what you need, I don't see a way around needing the private state for those objects)

You can make a single interface which contains only the methods you need, and provide a class that supports it which contains an instance of each type of object.

public class A{  public void DoSomething(){} }
public class B{  public void DoSomethingElse(){}}
public class C{  public void DoAnything(){}}

public interface ID 
{ 
    void A_DoSomething(); 
    void B_DoSomethingElse(); 
    void C_DoAnything(); 
}

public class D : ID 
{ 
    private A a;
    private B b;
    private C c;

    public D(A a,B b, C c) { this.a=a;this.b=b;this.c=c; }

    public void A_DoSomething(){ a.DoSomething();} 
    public void B_DoSomethingElse(){ b.DoSomethingElse();}
    public void C_DoAnything(){ c.DoSomething();
}

If you need to generate this dynamically, look into Reflection.Emit. It'll be some business about how you've got to create a new assembly, and then load it dynamically into the AppDomain. I'd try to avoid that if you can.

like image 41
Matt Brunell Avatar answered Sep 28 '22 02:09

Matt Brunell