vendredi 3 juillet 2015

I'm trying to use reflection to automate a method repository.

For example I have a method.

public string CanCallThis(int moduleFunctionId)
{
    return "Hello";
}

Which I'm registory in my command manager like this.

commandManager.RegisterCommand(moduleName,"CanCallThis", new ModuleCommand<int, string>(CanCallThis));

This works fine, but it's a manual process to register each command.

I'm trying to use reflection so that I can probe the class for commands, then call the RegistrCommand method with the information discovered by reflection - this will make life much easier as I don't have to remember to add each RegisterCommand entry.

I'm in the process of creating the method that registers the method, currently my code looks like this.

List<MethodInfo> methodInfos = IdentifyMethods();

foreach (var methodInfo in methodInfos)
{
    ParameterInfo[] methodParams = methodInfo.GetParameters();

    if (methodParams.Length = 1)
    {
        Type returnType = methodInfo.ReturnType;
        string methodName = methodInfo.Name;
        Type inputParam = methodParams[0].ParameterType;

        commandManager.RegisterCommand(moduleName, methodInfo.Name, new ModuleCommand<inputParam, returnType>(unknown));
    }
}

In the above examples, inputParam, returnType and unknown are causing compliation errors. My goal here is to create an instance of ModuleCommand. I'm sure this is something to do with creating a delegate, but I'm not sure how to go about this.

Can someone help me create the ModuleCommand?





Aucun commentaire:

Enregistrer un commentaire