Q:

C# program to print class names created in the program using reflection

0

C# program to print class names created in the program using reflection

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to print class names created in the program is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to print class names created in 
//the program using reflection.

using System;
using System.Reflection;

class Sample
{
    int num1;
    int num2;

    public void SetValues(int n1, int n2)
    {
        num1 = n1;
        num2 = n2;
    }

    public void PrintValues()
    {
        Console.WriteLine("Num1 :"+ num1);
        Console.WriteLine("Num2 :"+ num2);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Assembly asm;
        Type[] types; 

        asm     = Assembly.GetExecutingAssembly();
        types   = asm.GetTypes();

        Console.WriteLine("Class Names:");
        foreach (Type classNames in types)
        {
            Console.WriteLine("\t" + classNames.Name);
        }
    }
}

Output:

Class Names:
        Sample
        Program
Press any key to continue . . .

Explanation:

Here, we created two classes Sample and Program. And imported the System.Reflection to use Assembly class.

The Program class contains the static method Main(), the Main() method is the entry point for the program.

Here, we created reference asm of assembly class which is initialized with object returned by the GetExecutingAssembly(), and then we get types from the current program assembly and printed the name of classes that are created within the current program.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to print class names and its method nam... >>
<< C# program to demonstrate the use of reflection to...