How can you get the names of method parameters?

java reflection get method parameter values
java reflection get method parameter types
the method used to fetch the parameter types using method parameter reflection is
get name of parameter java
which of the following methods preserve parameter names in java bytecode (through reflection api)
java get method param names
java 8 get variable name
getparametertypes() method used to fetch the parameter types using method parameter reflection

If I have a method such as:

public void MyMethod(int arg1, string arg2)

How would I go about getting the actual names of the arguments? I can't seem to find anything in the MethodInfo which will actually give me the name of the parameter.

I would like to write a method which looks like this:

public static string GetParamName(MethodInfo method, int index)

So if I called this method with:

string name = GetParamName(MyMethod, 0)

it would return "arg1". Is this possible?

public static string GetParamName(System.Reflection.MethodInfo method, int index)
{
    string retVal = string.Empty;

    if (method != null && method.GetParameters().Length > index)
        retVal = method.GetParameters()[index].Name;


    return retVal;
}

The above sample should do what you need.

Can I obtain method parameter name using Java reflection?, Method Parameter Reflection support was added in Java 8. Simply put, it provides support for getting the names of parameters at runtime. public static string GetParameterName ( Delegate method , int index ) { return method.Method.GetParameters ( ) [ index ].Name ; } You could use 'Func<TResult>' and derivatives to make this work for most situations

Try something like this:

foreach(ParameterInfo pParameter in pMethod.GetParameters())
{
    //Position of parameter in method
    pParameter.Position;

    //Name of parameter type
    pParameter.ParameterType.Name;

    //Name of parameter
    pParameter.Name;
}

Java 8 Reflection Access to Parameter Names of Method and , That's what happens if parameter names get lost. Granted, the average programmer will hardly ever want to read the name of a parameter via� You can obtain the names of the formal parameters of any method or constructor with the method java.lang.reflect.Executable.getParameters. (The classes Method and Constructor extend the class Executable and therefore inherit the method Executable.getParameters .) However, .class files do not store formal parameter names by default.

without any kind of error checking:

public static string GetParameterName ( Delegate method , int index )
{
    return method.Method.GetParameters ( ) [ index ].Name ;
}

You could use 'Func<TResult>' and derivatives to make this work for most situations

Method Parameter Reflection in Java, class Record { int num; String name; } Now we can pass the object as a parameter to a method: public static void tryObject(Record r) { r.num = 100; r. name� MethodInfo.GetCurrentMethod() will give you information about the current method and then get information about the parameters using GetParameters(). share | improve this answer | follow | answered Jan 17 '11 at 20:05

nameof(arg1) will return the name of the variable arg1

https://msdn.microsoft.com/en-us/library/dn986596.aspx

Java 8 Named Method Parameters, The following example shows how to get ParameterInfo objects for the parameters of a method, and then use the Name property to obtain the parameter names. If the class file was compiled without debug information included then it is impossible to get the original parameter names, as used in the source code. However, If the class file does include debug information, then the method names are hidden deep within the class file. You'd need to parse the class file yourself.

Java Method Arguments, The names you use for parameters can be the same as the names you use for the variables you pass to the method when you call it, but they don't have to be. You� You can declare local variables with the same exact name as the method parameters. This is certainly better than having magic numbers throughout your code. But I'd suggest not using the exact same names. Method parameter names should be more generic than the names of the arguments passed in a single method call.

ParameterInfo.Name Property (System.Reflection), A method definition consists of: required and optional positional arguments; an optional splat argument, whose name can be empty; required and optional named� Parameter names are only useful to the compiler. When the compiler generates a class file, the parameter names are not included - a method's argument list only consists of the number and types of its arguments. So it would be impossible to retrieve the parameter name using reflection (as tagged in your question) - it doesn't exist anywhere.

How to Use Methods that Take Parameters in Java, Using named parameters, you can specify the value of the parameter according to their names not their order in the method. Or in other words,� Possible Duplicate: Getting method parameter names in python Is there an easy way to be inside a python function and get a list of the parameter names? For example: def func(a,b,c): print

Comments
  • Any ideas about making this work with a function where the arguments are supplied as a params array?
  • @TomAnderson - blindly copying code from the internet is asking for trouble too!
  • I'm almost entirely sure that the OP meant from outside of the method. All of the other comments, left eight years before yours, also seem to confirm that.