C# - Use of Reflection to Get Namespace and Base-type

In this example, we will learn how to use reflection to get namespace and base-type using C# program?
Submitted by Nidhi, on October 26, 2020 [Last updated : March 21, 2023]

Here, we will get data-type name, namespace, and base-type using reflection, here we will import the System.Reflection namespace.

C# program to demonstrate the use of reflection to get namespace and base-type

The source code to demonstrate reflection to get namespace and base-type is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

// C# program to demonstrate the use of reflection 
//to get namespace and base type.

using System;
using System.Reflection;

class RefDemo
{
    static void Main()
    {
        Type type;
        
        type = typeof(int);
        Console.WriteLine("Data Type Name : "+ type.Name        );
        Console.WriteLine("Full Type Name : "+ type.FullName    );
        Console.WriteLine("Namespace      : "+ type.Namespace   );
        Console.WriteLine("Base Type      : "+ type.BaseType    );
    }
}

Output

Data Type Name : Int32
Full Type Name : System.Int32
Namespace      : System
Base Type      : System.ValueType
Press any key to continue . . .

Explanation

Here, we created a class RefDemo. Here, we imported the System.Namespace to get system type name, namespace, and base-type using predefined properties.

The RefDemo class contains the Main() method. In the Main() method, we created a reference from the Type class.

type = typeof(int);

Here, reference type is initialized with reference returned by the typeof() operator, and then we printed system type-name, full type-name, namespace, and base-type using predefined properties.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.