Menus

Tuesday, December 4, 2012

List all .NET Attributes in the Loaded Assemblies

Here’s how to list all the .NET Attributes in the loaded assemblies.
static void Main(string[] args)
{

    var attributes = from assembly in AppDomain.CurrentDomain.GetAssemblies()
                     from exptype in assembly.GetExportedTypes()
                     where typeof(Attribute).IsAssignableFrom(exptype)
                     select exptype;

    foreach (var nm in attributes)
    {
        Console.WriteLine("Attribute Name: {0} \nFullName: {1}",
            nm.Name, nm.FullName);
        Console.WriteLine("-------------------------------------------");
    }

    Console.ReadLine();
}



Note: The results may vary on your machine. Since we are referring to the GetExportedTypes() which returns type visible outside the assembly, you can get different results by adding new references (Right click project > Add Reference ) or by changing the access modifiers of the types.
OUTPUT
image

No comments:

Post a Comment