DotNet Programming World

Tuesday, July 10, 2007

Access Type metadata by using Assembly

using System;
using System.Reflection;

static public void ListOtherTypesInAssembly(Type yourType)
{
Assembly sampleAssembly;
Type[] publicTypesInAssembly;
AssemblyName[] referencedAssemblies;

try
{
sampleAssembly = Assembly.GetAssembly(yourType);

publicTypesInAssembly = sampleAssembly.GetExportedTypes();
referencedAssemblies = sampleAssembly.GetReferencedAssemblies();
Console.WriteLine("Type name: '{0}' is contained in assembly ", yourType.Name);
Console.WriteLine(sampleAssembly.FullName);
Console.WriteLine();
Console.WriteLine("This assembly references {0} other assemblies", referencedAssemblies.GetLength(0));
Console.WriteLine("There are {0} public types in this assembly.", publicTypesInAssembly.GetLength(0));
Console.WriteLine();
Console.ReadLine();

Console.WriteLine("Referenced Assemblies:");
Console.WriteLine();

foreach (AssemblyName name in referencedAssemblies)
{
Console.WriteLine(name.FullName);
Console.WriteLine();
}
Console.ReadLine();

Console.WriteLine("Public types in this assembly:");
foreach (Type t in publicTypesInAssembly)
{
Console.WriteLine(t.Name);
}

}//End try
catch (Exception xcp)
{
Console.WriteLine(xcp.ToString());
}
finally
{
Console.ReadLine();
}
}

0 Comments:

Post a Comment

<< Home