views:

381

answers:

5

I'm trying to find a way to determine how many parameters a constructor has.

Now I've built one constructor with no parameters and 1 constructor with 4 parameters.

Is there, in C#, a way to find out how many parameters a used or given constructor has?

Thing is, I'm using a third constructor to read log files. These logs files are read as string[] elements and there should be just as many as there are arguments. If not, I have a corrupt log file.

But I'm using a lot of subclasses and each constructor has more parameters for their specific log-type.

So I wanted to know: is there a method to check the amount of parameters on a constructor?

And yes, this is a school assignment. I don't know what terms to look for really, so the VS2008 object browser is currently not of much use.

A: 

i'm not sure exactly what context you need this information, but if you need it dynamically at run-time try the System.Reflection namespace

otherwise the Intellisense drop-list should show you all the constructors available...

Steven A. Lowe
+3  A: 

You should look at the System.Reflection Namespace. More specifically, you can get a list of the constructors of a class with:

 System.Type.GetType("MYClassName").GetConstructors()
Kibbee
+1  A: 

It sounds as if you need to re think your code a bit. From your description, having to dynamically determine the number of arguments in a constructor sounds a bit hairy. You might consider a factory design pattern since the type of object created is determined at runtime. If I misunderstand your problem then using reflection as pointed out by other answers will do the trick for you.

Vincent Ramdhanie
A: 

The amount of parameters is constant. I've defined them and they're not changing.

What's happening is I'm simulating a sort of publications tree and I'm making divisions in that(a.k.a. subclasses)

Thusly, all the constructors of my subclasses have the parameters or the classes they inherit from.

Thusly, the length is different for each type of publication.

I have a third constructor, just in case I need to visualise my publication data throuhg reading the log file.

But I have to take into account that the log file might be corrupt. Which includes the possibility that there is no data for all my parameters in the log file.

This is why I have to know how to find the amount of parameters in my constructor: I have to check howmuch data there is in my log compared to the amount of parameters I have.

Vordreller
please post some code, your question is not making much sense
Steven A. Lowe
A: 

Can't you make a constructor that takes a reference to the log file (or the current raw logfile entry), reads it, and throw an error if there's any problem?

I'm trying to understand why you'd need to look at the number of elements a constructor has. It seems a weak design from what I've seen so far to trust that the number of elements in the log file happens to identify the type of publication to create.

The short answer to your immediate question is what was stated in an earlier answer: reflect on the constructor for the class you're trying to create, and examine its parameters.

Dan Fleet
That would be more usefull, I'll look into it.Also, I tried to look for "reflections" and "System.Reflections" in the VS2008 Object Browser and there were no search results
Vordreller
It's System.Reflection not Reflections. Reflection is the act of looking at class type information at runtime.
Dan Fleet