Type.GetTypeArray() Method in C#

The Type.GetTypeArray() method in C# is used to retrieve the Type objects representing the runtime types of all elements in a specified object array. This method is particularly useful in reflection scenarios where you need to determine the actual types of objects at runtime.

Syntax

Following is the syntax −

public static Type[] GetTypeArray (object[] args);

Parameters

  • args − An array of objects whose types are to be determined.

Return Value

This method returns an array of Type objects representing the types of the corresponding elements in the input array.

Using Type.GetTypeArray() with Mixed Object Types

The method is especially useful when working with arrays containing objects of different types −

using System;
using System.Reflection;

public class Demo {
    public static void Main() {
        Object[] obj = new Object[5];
        obj[0] = 10.20;
        obj[1] = 20;
        obj[2] = 30;
        obj[3] = 40;
        obj[4] = "tom";
        
        Type[] type = Type.GetTypeArray(obj);
        Console.WriteLine("Types...");
        for (int i = 0; i < type.Length; i++)
            Console.WriteLine(" {0}", type[i]);
    }
}

The output of the above code is −

Types...
System.Double
System.Int32
System.Int32
System.Int32
System.String

Using Type.GetTypeArray() with Decimal and Numeric Types

This example demonstrates how the method distinguishes between different numeric types −

using System;
using System.Reflection;

public class Demo {
    public static void Main() {
        Object[] obj = new Object[5];
        obj[0] = 100m;
        obj[1] = 20.98;
        obj[2] = 30.788m;
        obj[3] = 40;
        obj[4] = "jack";
        
        Type[] type = Type.GetTypeArray(obj);
        Console.WriteLine("Types...");
        for (int i = 0; i < type.Length; i++)
            Console.WriteLine(" {0}", type[i]);
    }
}

The output of the above code is −

Types...
System.Decimal
System.Double
System.Decimal
System.Int32
System.String

Using Type.GetTypeArray() for Method Parameter Analysis

A practical use case for GetTypeArray() is analyzing method parameters in reflection scenarios −

using System;
using System.Reflection;

public class Demo {
    public static void Main() {
        object[] parameters = { 42, "Hello", true, 3.14, DateTime.Now };
        Type[] parameterTypes = Type.GetTypeArray(parameters);
        
        Console.WriteLine("Method parameter analysis:");
        for (int i = 0; i < parameterTypes.Length; i++) {
            Console.WriteLine("Parameter {0}: {1} (Value: {2})", 
                i + 1, parameterTypes[i].Name, parameters[i]);
        }
    }
}

The output of the above code is −

Method parameter analysis:
Parameter 1: Int32 (Value: 42)
Parameter 2: String (Value: Hello)
Parameter 3: Boolean (Value: True)
Parameter 4: Double (Value: 3.14)
Parameter 5: DateTime (Value: 12/20/2024 10:30:45 AM)

Conclusion

The Type.GetTypeArray() method is a valuable utility in C# reflection that returns an array of Type objects representing the runtime types of elements in an object array. It's commonly used for method parameter analysis, dynamic method invocation, and type inspection in reflection scenarios.

Updated on: 2026-03-17T07:04:35+05:30

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements