[C#] How to Get All Constructors and Argument Information Defined in a Class

Using the Type.GetConstructors method in C# Reflection allows you to dynamically list the initialization patterns (constructor overloads) of a class.

This technique is commonly used by Dependency Injection (DI) containers to automatically select the optimal constructor, or by tools that analyze class specifications to generate documentation.

Here, we will explain how to list defined constructors and parse the type information of their arguments, using the standard System.Exception class as an example.

目次

Retrieving and Analyzing the Constructor List

The following sample code retrieves public instance constructors using BindingFlags and formats the argument type names into a comma-separated string using LINQ.

Sample Code

using System;
using System.Linq;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        // 1. Get type information to analyze (using System.Exception as an example)
        Type targetType = typeof(Exception);

        Console.WriteLine($"Constructors of Class: {targetType.Name}\n");

        // 2. Get array of constructor information
        // Instance: For instance creation
        // Public: Publicly available
        var constructors = targetType.GetConstructors(BindingFlags.Instance | BindingFlags.Public);

        foreach (var ctor in constructors)
        {
            // 3. Get argument parameter information for each constructor
            ParameterInfo[] parameters = ctor.GetParameters();

            // Transform argument type names into an array and convert to a comma-separated string
            // Example: "String, Exception"
            string argString = string.Join(", ", 
                parameters.Select(p => p.ParameterType.Name));

            // Output format: ClassName(ArgType1, ArgType2...)
            // The constructor name (Name property) is usually ".ctor", 
            // but we display the class name here for readability.
            Console.WriteLine($"{targetType.Name}({argString})");
        }
    }
}

Execution Result (Example)

Plaintext

Constructors of Class: Exception

Exception()
Exception(String)
Exception(String, Exception)
Exception(SerializationInfo, StreamingContext)

Explanation and Technical Points

1. GetConstructors Method

The GetConstructors method of the Type class returns an array of metadata (ConstructorInfo) for the constructors defined in that type.

As with methods, it returns only public instance constructors by default. If you want to retrieve private constructors (e.g., for a Singleton pattern class), you must use BindingFlags.NonPublic in combination.

2. Analyzing Parameter Information

By calling GetParameters() on the retrieved ConstructorInfo object, you can obtain a list of arguments (ParameterInfo[]). From each ParameterInfo, you can get the following information:

  • ParameterType: The type of the argument (e.g., System.String).
  • Name: The variable name of the argument (e.g., message).
  • IsOptional: Whether the argument is optional.
  • DefaultValue: The default value (if optional).

3. Constructor Names

Internally in Reflection, constructor names are always treated as .ctor (instance constructor) or .cctor (static constructor). When outputting logs or documentation, substituting this with DeclaringType.Name (the class name) as shown in the sample code makes the output look closer to standard C# notation.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次