lundi 14 septembre 2015

Invoke instance method statically

Let's say there is a class A with an parameterless instance method

class A
{
    public A(int x) { this.x = x; }
    private int x;
    public int foo() { return x; }
}

It's easy to invoke the method using reflection:

A a = new A(100);
var method = typeof(A).GetMethod("foo");
var result = method.Invoke(a, new object[0]); // 100

However, I want to invoke the method as if it is static

var staticmethod = Something(typeof(A), "foo");
var result = staticmethod.Invoke(null, new object[] { a });

Is there any way I can get this staticmethod?

NOTE: I want Something to be universal, i.e. A can be any class, and foo can be any instance method.

EDIT: To make things clear:

There is no static method in class A.

There is a parameterless instance method called foo.

I want to invoke (using MethodInfo.Invoke) foo AS IF it is a static method, that takes class A as the parameter.

EDIT2: Why I want this: (to help you understand better)

I have a list of static methods that does similar job for different types, and they are stored in a dictionary Dictionary<Type, MethodInfo> dict.

Thus, whenever I have an object obj and want to do the job, I can

dict[obj.GetType()].Invoke(null, new object[] { obj, param1, param2, ... });

Now I want to add instance methods into it as well, but it will require me to remember which methods are static and which methods are instance-bond, and invoke them in different ways:

dict[obj.GetType()].Invoke(null, new object[] { obj, param1, param2, ... }); // static methods
dict[obj.GetType()].Invoke(obj, new object[] { param1, param2, ... }); // instance methods

Which is inconvenient. So I want to get static MethodInfo from instance methods, before adding them into the dict.

EDIT3: I don't understand why this question is marked duplicate. The linked page does NOT answer my question. If I'm missing something, please tell me.

The linked page has several answers, but they either

  1. requires that I know how many arguments foo takes, or
  2. gives a method that takes object[] as the parameter, instead of a list of parameters.

So none of them fit here.

After some research I found that there's something close to what I need:

 A a = new A(100);
 var method = typeof(A).GetMethod("foo");
 var deleg = Delegate.CreateDelegate(typeof(Func<A, int>), method)
 var result = deleg.DynamicInvoke(new object[] { a }); // 100

Here, it takes new object[] { a } as the argument. But the thing is, since I don't know how foo looks like, how can I pass the first argument of Delegate.CreateDelegate?

Last EDIT: Found a solution myself. Thank you for your help guys!





Aucun commentaire:

Enregistrer un commentaire