-1

I have a method, GetData(), which I would like to exchange its name dynamically based on a string parameter.

The first thing was to Get the Method out of the string parameter, MethodName.

var methodinfo = repository.GetType().GetMethod("MethodName");

Now, how to replace the GetData() method below with the dynamic value that is extracted in methodinfo?

var argumentType = repository.GetData().GetType().GetGenericArguments()[0];

I tried something like this but didn't work:

var argumentType =  methodinfo.GetType().GetGenericArguments()[0];
3
  • 3
    What are you trying to accomplish? I've read your question several times and I honestly don't have the foggiest idea. Commented Jun 3, 2014 at 17:40
  • If you are really trying to change the name of a method during runtime, forget it. You can't. Let us know what you are trying to do and maybe we can send you in the right direction. Commented Jun 3, 2014 at 17:43
  • 1
    Please post what you want to achieve, there is probably a better way than dynamically changing method names (which is pretty unusual). Commented Jun 3, 2014 at 17:45

1 Answer 1

0

If I understand your question right you would need something like this:

public class ProgChoice
{
    public static void ProgSelection()
    {
        Assembly assembly = Assembly.GetExecutingAssembly();

        Type t = assembly.GetType("ProgChoice.ProgSelection", false, true);

        string lcProgStr = "Prog";
        int liProgNumb = 4;

        // Concatenate the 2 strings
        lcProgStr = lcProgStr + liProgNumb.ToString();

        MethodInfo method = t.GetMethod(lcProgStr);
        method.Invoke(null, null);

        Console.ReadKey();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I will mark this as an answer; however, in my case I had to pass repository as an argument to method.Invoke(repository, null). So the end result was: var argumentType = methodinfo.Invoke(repository, null).GetType().GetGenericArguments()[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.