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

Here, we are going to demonstrate the use of reflection to get namespace and base-type in C#.
Submitted by Nidhi, on October 26, 2020

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

Program:

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.