views:

75

answers:

3

Does anyone knows how can I invoke method dynamically without reflection?

I`ll try to specify my question below

Suppose we have an interface IMyInterface and MyImplementation is an implementation of IMyInterface. Besides of that I have a generic proxy class MyProxy<T> where T : IMyInterface. In this proxy I wanna wrap all calls to all methods that have been defined in MyImplementation and in all inheritors of this class. The purpose I wanna achieve is the dynamic method invocation. In case of reflection everything will be transparent, but as I understand it might be quite slow. Does anyone know about solution that could be much faster?

Thanks a lot!

-- update

hmm, looks like my previous descriptions was not good, so I`ll try to describe my question again. With examples :)

So, lets imagine that we have the following code:

public interface IMyInterface
{
     void Method();
}

public class MyImplementation : IMyInterface
{
     public void Method()
     {
          Console.WriteLine("Yeaah!");
     }
}

The important point that I forgot to mention is that we have a class named for example Holder. This class should be used by the following way

var holder = // the way of instantiation doesn`t really matters
holder.Register(myImplementationInstance);

var myInterfaceInstance = holder.Resolve<IMyInterface>();
myInterfaceInstance.Method();

Holder instance will return some wrapper that will implement IMyInterface and will wrap the real instance of myImplementation that has been registered.

As I said above we have a wrapper MyImplementationWrapper that implements IMyInterface and has the method named Method with the following body

public void Method()
{
    _wrappedInstance.Method();
}

So, there are two questions a) How can I automatically create the wrapper for myImplementationInstance (I dont want to know anything about object that will be registred inHolder) b) How can I dynamically invoke the methods ofmyImplementationInstance` by its wrapper

+1  A: 

First things first. Have you made sure that Reflection is not too slow for your needs? Do not go on hearsay - test it yourself.

Edit: To include the use of dynamic

Secondly if you are in the .net 3.5 or .net 4.0 then you can use Iron Python (or in the case of the 4.0 - any DLR language or dynamic) to do the dynamic invocation.

The DLR is a very optimal solution for this kind of thing. In C# you can use the dynamic keyword to access the dlr

Preet Sangha
Or if he is using C# 4.0, he can use the `dynamic` keyword, if that is actually what he is trying to do. Either one is some form of dynamic method invocation using object metadata, or, in a generic sense, reflections.
Merlyn Morgan-Graham
+1 for checking if he can just use reflection, I'd bet is a non issue in this case. Agree with Merlyn, I'd use dynamic in 4.0
eglasius
A: 

I'm not sure what you're trying to achieve. It sounds, though, that what you want is a decorator, which does not require reflection.

Luke Schafer
A: 

This seems like you're trying to re-implement polymorphism...

class MyBaseImplementation
{
    public virtual void MyMethod()
    {
        Console.WriteLine("Base");
    }
}

class MyDerivedImplementation : MyBaseImplementation
{
    public override void MyMethod()
    {
        Console.WriteLine("Derived");
    }
}

static void DoSomething(MyBaseImplementation instance)
{
    instance.MyMethod();
}

static void Main(string[] args)
{
    MyBaseImplementation inst = new MyDerivedImplementation();
    DoSomething(inst);
}

This will print "Derived", even though the parameter in the function is of type "MyBaseImplementation"

Merlyn Morgan-Graham