27

I've a requirement in which i need to get the variables names of the constructor in my class. I tried it using c# reflection, but constructorinfo does not give sufficient information. As it only provides the datatype of the parameters but i want the names, ex

class a
{    
    public a(int iArg, string strArg)
    {
    }
}

Now i want "iArg" and "strArg"

Thanks

DiskJunky
  • 4,750
  • 3
  • 37
  • 66
manav inder
  • 3,531
  • 16
  • 45
  • 62
  • In which context do you need this? Could you use .NET Reflector? – Mitchel Sellers Jul 07 '11 at 06:11
  • 2
    That requirement could run into a lot of problems. For instance, why would the compiler even keep the names, they just make the compiled code longer. Also what happens if you run the code through a confuscation? What possible reason could you have for this requirement? – Yuriy Faktorovich Jul 07 '11 at 06:14
  • 6
    [Hungarian notation](http://en.wikipedia.org/wiki/Hungarian_notation): it lives on. – Rick Sladkey Jul 07 '11 at 06:21
  • maybe he is crafting self generating code that is inserted into the body of an existing constructor? – Anonymous Type Jul 07 '11 at 06:24
  • Try to have a look at this http://stackoverflow.com/questions/214086/how-can-you-get-the-names-of-method-parameters-in-c – Kushal Shah Jul 07 '11 at 06:23
  • Just as one possible use case: I need it to avoid magic strings when using constructor parameters in Autofac registration. I got in some trouble recently because of an upper/lower case typo. – IngoB Dec 09 '20 at 18:23

2 Answers2

50

If you call ConstructorInfo.GetParameters(), then you will get back an array of ParameterInfo objects, which has a Name property containing the name of the parameter.

See this MSDN page for more information and a sample.

The following sample prints information about each parameter of class A's constructor:

public class A
{
    public A(int iArg, string strArg)
    {
    }
}

....

public void PrintParameters()
{
    var ctors = typeof(A).GetConstructors();
    // assuming class A has only one constructor
    var ctor = ctors[0];
    foreach (var param in ctor.GetParameters())
    {
        Console.WriteLine(string.Format(
            "Param {0} is named {1} and is of type {2}",
            param.Position, param.Name, param.ParameterType));
    }
}

The above sample prints:

Param 0 is named iArg and is of type System.Int32
Param 1 is named strArg and is of type System.String
M4N
  • 94,805
  • 45
  • 217
  • 260
5

I just checked MSDN for your question. As I see any ConstructorInfo instance may provide you with a method GetParameters(). This method will return a ParameterInfo[] - and any ParameterInfo has a property Name. So this should do the trick

 ConstructorInfo ci = ...... /// get your instance of ConstructorInfo by using Reflection
 ParameterInfo[] parameters = ci.GetParameters();

 foreach (ParameterInfo pi in parameters)
 {
      Console.WriteLine(pi.Name);  
 }

you may check msdn GetParameters() for any additional information.

hth

DiskJunky
  • 4,750
  • 3
  • 37
  • 66
Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54