SwagLordKnight
SwagLordKnight

Reputation: 53

Call dynamic method from string

I'm trying to call a method from a dynamic without knowing its name. I have difficulties to explain this in english so there's the code:

public void CallMethod(dynamic d, string n)
{
    // Here I want to call the method named n in the dynamic d
}

I want something like:d.n() but with n replaced by the string.

I want this :

Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);

but with dynamic.

If you need the context to help you: I'm make an application that's support "mods", you put DLLs in the mod folder and it loads it and execute it. It works with dynamic (I have a dictionnary like this : Dictionnary<string, dynamic> instances;). I want the application to get the methods name from the library (with instances["topkek"].GetMethods();, I've already made this method) but then call the method with the string it returns. I don't know if what I said mean something (I'm french :/ )...

I'm using VS 2013 Express with the .Net framework 4.5, if you need more information to help me ask me.

Upvotes: 4

Views: 5245

Answers (3)

SommerEngineering
SommerEngineering

Reputation: 1698

I want to add another approach as solution:

In your case, the caller (developer of the mod) knows the method to call. Thus, this might helpful:

// In the main application:
public dynamic PerformMethodCall(dynamic obj, Func<dynamic, dynamic> method)
{
    return method(obj);
{


 // In a mod:
 mainProgram.PerformMethodCall(myDynamicObj, n => n.myDynamicMethod());

 // In another mod:
 mainProgram.PerformMethodCall(myDynamicObj, n => n.anotherMethod());

This is a further development of the idea of Yuval Itzchakov in his commentary. He had suggested using a delegate.

Upvotes: 1

Blas Soriano
Blas Soriano

Reputation: 566

If all methods are void, this could work. Otherwise you need to change it a bit.

    public void CallMethod(string className, string methodName)
    {
        object dynamicObject;
        // Here I want to call the method named n in the dynamic d
        string objectClass = "yourNamespace.yourFolder." + className;
        Type objectType = Type.GetType(objectClass);
        if (objectType == null)
        {
            // Handle here unknown dynamic objects
        }
        else
        {
            // Call here the desired method
            dynamicObject = Activator.CreateInstance(objectType);
            System.Reflection.MethodInfo method = objectType.GetMethod(methodName);
            if (method == null)
            {
                // Handle here unknown method for the known dynamic object
            }
            else
            {
                object[] parameters = new object[] { };   // No parameters
                method.Invoke(dynamicObject, parameters);
            }
        }
    }

Upvotes: 1

Sanjay Singh
Sanjay Singh

Reputation: 714

you can write your method as follows -

public void CallMethod(dynamic d, string n)
    {
        d.GetType().GetMethod(n).Invoke(d, null);
    }

Upvotes: 7

Related Questions