Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of the constructor arguments in c#

Tags:

c#

reflection

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

like image 522
manav inder Avatar asked Jul 07 '11 06:07

manav inder


People also ask

What are arguments in constructor?

It is used to prevent a specific constructor from being called implicitly when constructing an object. For example, without the explicit keyword, the following code is valid C++: Array a = 10; This will call the Array single-argument constructor with the integer argument of 10.

What are the 3 types of constructor?

Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly 3 types of constructors in C++, Default, Parameterized and Copy constructors.

What is the name of constructor?

Constructors have the same name as the class--the name of the Rectangle class's constructor is Rectangle() , the name of the Thread class's constructor is Thread() , and so on. Java supports method name overloading so a class can have any number of constructors all of which have the same name.

Does constructor have argument?

Constructor has same name as the class itself. Default Constructors don't have input argument however, Copy and Parameterized Constructors have input arguments. Constructors don't have return type. A constructor is automatically called when an object is created.


2 Answers

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 
like image 119
M4N Avatar answered Sep 25 '22 14:09

M4N


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

like image 32
Pilgerstorfer Franz Avatar answered Sep 23 '22 14:09

Pilgerstorfer Franz