views:

348

answers:

2

Say I have a DLL assembly, containing an auto-generated class with two methods:

public class AutoGeneratedClass
{
    public int DoSomething(bool forReal) { ??? }
    public void DoSomethingElse(string whatever) { ??? }
}

The methods could be anything, really. The above is just an illustration.

What kind of code would I need to generate in place of ??? above to have it automatically pack up parameters of the call and send them to a single generic method on another class, which would then actually handle the call and return the result?

public class GenericClass
{
    public ??? GenericHandler(???) { ??? }
}

I guess I'm looking for something that works similar to Expression<Func<...>> in LINQ, which compiles to a data structure that can be sent anywhere and taken apart there.

I would like to keep the original method prototypes the same as if they were normal, locally implemented methods, though, to keep the caller of the AutoGeneratedClass non the wiser.

If this cannot be achieved, what are my options?

Thank you!

+1  A: 

Do you have to have a class? Life would be a lot easier using interfaces - you can then use System.Runtime.Remoting.Proxies.RealProxy or possibly create a new type on demand. I believe Rhino.Mocks does the latter (using a library to do the heavy lifting) - and if you can use an interface, seeing how Rhino.Mocks is implemented may well be a good starting point.

Jon Skeet
That's a really interesting idea! I'll explore what interfaces and proxies can do to help me. Thanks!
aoven
A: 

I'm going to take a stab in the dark here since I'm not sure what all of your limitations are. This sounds like a good case for partial methods/partial class for your AutoGenerated class. (Note I have not run this good through a compiler so it probably needs some cleanup.)

public partial class AutoGenerated
{
    public partial int ProcessDoSomething(bool forReal);
    public int DoSomething(bool forReal)
    {
        //some generated code against error conditions??
        return ProcessDoSomething(forReal)
    }

    .....
}

public partial class AutoGenerated
{
    public partial int ProcessDoSomething(bool forReal)
    {
        //call the generic handler...
    }
}

I'm going to stay away from the LINQ-like expression part of the generic handler since I'm still working on grokking Expressions. However something to consider is how hard/easy will your GenericClass be to maintain if you go down the LINQ-like expression path?

My experience has been that quite a few developers aren't comfortable with delegates and lamba expressions. They cringe (literally) when looking at code that uses Expressions.

confusedGeek