Nithesh Narayanan
Nithesh Narayanan

Reputation: 11765

How to get number of arguments in a constructor

In my .net windows application(c#) i want know the number of arguments in each constructor of a particular class. I get all the constructor by using reflection. Is it possible to get the number of arguments of each constructors?

Thanks in advance...

Upvotes: 4

Views: 2997

Answers (2)

Petar Ivanov
Petar Ivanov

Reputation: 93030

Type t = typeof(...);

var constructors = t.GetConstructors();
foreach (var con in constructors)
{
    Console.WriteLine(con.GetParameters().Length);
}

Upvotes: 5

zneak
zneak

Reputation: 138051

Ask for its parameters (through GetParameters()), then ask for the length of the array.

ConstructorInfo ctor = /* ... */
int numberOfArguments = ctor.GetParameters().Length;

Upvotes: 8

Related Questions