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.
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.
Output:
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.
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.