Type Summary



Type Summary


public abstract class ConstructorInfo : MethodBase

    {

    // Constructors

       protected ConstructorInfo();



    // Fields

       public static readonly string ConstructorName = ".ctor";

       public static readonly string TypeConstructorName = ".cctor";



    // Properties

    MS public override MemberTypes MemberType { get; }



    // Methods

       public abstract object Invoke(BindingFlags invokeAttr, Binder binder,

                              object[] parameters, CultureInfo culture);

       public object Invoke(object[] parameters);

    }


Example

using System;

using System.Reflection;



/// <summary>

/// This example shows how to print out constructors in C#

/// syntax.

/// </summary>



public class ConstructorInfoSample

{

    public static void Main()

    {

        Type type = typeof(String);



        foreach (ConstructorInfo ctor in type.GetConstructors(

            BindingFlags.NonPublic | BindingFlags.Public | 

            BindingFlags.Instance ))

        {

        

            //Skip any constructors that are not publicly accessible

            if (!(ctor.IsPublic || ctor.IsFamily || ctor.IsFamilyOrAssembly))

            {

                continue;

            }

            Console.Write(ctor.IsPublic ? "public " : ctor.IsFamily ?

                "protected " : "protected internal ");

            Console.Write(ctor.DeclaringType.Name);

            Console.Write('(');

            ParameterInfo[] parameters = ctor.GetParameters();

            for (int i = 0; i < parameters.Length; i++)

            {

                Console.Write("{0} {1}", parameters[i].ParameterType,

                    parameters[i].Name);

                if (i != parameters.Length - 1)

                {

                    Console.Write(", ");

                }

            }

            Console.Write(");");

            Console.WriteLine();

        } //end foreach loop



        ConstructorInfo c = typeof(String).GetConstructor(new Type[] { 

                                                   typeof(char), typeof(int) });

        string s = (string) c.Invoke(new object[] {'.', 7});

        Console.WriteLine ("The result is '{0}'",s);

        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    } //end main

} //end class


The output is


public String(System.Char* value);

public String(System.Char* value, System.Int32 startIndex, System.Int32 length);

public String(System.SByte* value);

public String(System.SByte* value, System.Int32 startIndex, System.Int32 length);

public String(System.SByte* value, System.Int32 startIndex, System.Int32 length,

System.Text.Encoding enc);

public String(System.Char[] value, System.Int32 startIndex, System.Int32 length);

public String(System.Char[] value);

public String(System.Char c, System.Int32 count);

The result is '.......'





Press Enter to continue