Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of arguments in a constructor

Tags:

c#

.net

winforms

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...

like image 911
Nithesh Narayanan Avatar asked Sep 19 '25 01:09

Nithesh Narayanan


2 Answers

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

ConstructorInfo ctor = /* ... */
int numberOfArguments = ctor.GetParameters().Length;
like image 166
zneak Avatar answered Sep 21 '25 15:09

zneak


Type t = typeof(...);

var constructors = t.GetConstructors();
foreach (var con in constructors)
{
    Console.WriteLine(con.GetParameters().Length);
}
like image 34
Petar Ivanov Avatar answered Sep 21 '25 16:09

Petar Ivanov