tags:

views:

47

answers:

3

In PHP, I can try to call any method that might exist on an object like this:

$object->{$method}();

Where $object is our PHP Object and $method is the name of the method that we want to call. I can dynamically call any method this way.

Is there any C# equivalent to this? Or am I just "doing it wrong"? I have a plugin/module loaded in via Reflection and I'd like to call a method on it that is not defined in the interface.

Thanks!

+5  A: 

Contrary to PHP, C# is a statically typed language meaning that types need to be known at compile time. Although such method has been introduced in C# 4.0. It's the dynamic keyword. It allows you to declare a variable of a dynamic type and call whatever method you like on it and the compiler won't protest. The resolution will be done at runtime:

dynamic obj = FetchInstanceFromSomewhere();
obj.Method();

Another more classic method is to use reflection but this could quickly turn into a nightmare.

Darin Dimitrov
Exactly what I needed. Thanks for the fast, and helpful reply. I'll mark it as answered as soon as SOF will let me.
kmark937
@kbo206: Glad you were able to find the answer. Just an SO(StackOverflow) would do (0:
KMan
Now that I think about it, "SO" makes much more sense. Thanks for the heads-up, KMan.
kmark937
+3  A: 

As answered here, C#4 has the dynamic keyword, that does dynamic method invoking.

If you are on an older version, you can do this using Reflection, but I think it is the wrong way of doing it. The C# way of doing it would be to ensure the plugin loaded has an interface, which contains the methods you need to call.

Anyways, if you need to do it using reflection, here is an example:

Type type = instance.GetType();
MethodInfo m = type.GetMethod("MethodName");
m.Invoke(instance, new object[] {});

This is for a public method taking no arguments.

driis
Thank you for the workaround, driis.
kmark937
+1  A: 

I have a plugin/module loaded in via Reflection and I'd like to call a method on it that is not defined in the interface

Be carefull though... The cited sentence let me guess that you are doing something wrong. Using reflection 'to the rescue' is a common misconception of many c# users. If the interface in the module was designed without the method you wish to call then there was probably a good reason for this decision. If the module was designed properly you should not be able to call this method anyway - it is either private or internal.

paul_71
Your assumptions are correct, but right now I'm just slapping together a modular command-line-like application. Each command is assigned to a method within a specific module. Therefore, I'd be easiest for me to call the module's method dynamically with some try ... catches around it.
kmark937
...which does not help you much, if the modules start to throw expceptions because of insufficient privilieges to call the desired methods. How do intend to handle the 'the-object-has-this-method-but-I-cannot-call-it' exception?
paul_71
I don't tend to handle it very well at all. Since it's just something I felt like slapping together in a day or two, I might go back and remake it.
kmark937