You can obtain a list of a class's constructors via the GetConstructors ( ) method. There is no need to return the name of the constructor because in C# and VB, all constructors default to the name of their class. They only differ by their signatures. Even if you don't explicitly create a class constructor, a default parameterless constructor is created for you at compile time, so you'll always see at least one. Listing 2-9 shows how you can display the constructors in a class as well as their parameters.

Listing 2-9. Retrieving Class Constructors

ConstructorInfo[] aConstructorInfo = oType.GetConstructors(); LoadConstructors(aConstructorInfo);

private void LoadConstructors(ConstructorInfo[] aConstructorInfo) {

TreeNode oTN; TreeNode oSubTN;


foreach (ConstructorInfo oCI in aConstructorInfo) {

oTN = trvConstructors.Nodes.Add(oCI.Name);

foreach (ParameterInfo oPI in oCI.GetParameters()) {

oSubTN = oTN.Nodes.Add(oPI.Name);

oSubTN.Nodes.Add("DefaultValue: " + oPI.DefaultValue); oSubTN.Nodes.Add("IsIn: " + oPI. IsIn) ; oSubTN.Nodes.Add("IsOut: " + oPI.IsOut); oSubTN.Nodes.Add("IsOptional: " + oPI.IsOptional); oSubTN.Nodes.Add("Position: " + oPI.Position);


Figure 2-6 shows the results of this query.

Figure 2-6. Constructor information
0 0

Post a comment