Get Assembly Type Names Without Loading The Assembly

N

Neo

Does anyone know how to get a list of the types in an Assembly without
actually loading the Assembly into your app domain?
 
M

Mattias Sjögren

Does anyone know how to get a list of the types in an Assembly without
actually loading the Assembly into your app domain?

Yes, by using a metadata reading library other than Reflection.
Microsoft provides a COM API, and there are some other available (with
C# source code) online.


Mattias
 
M

MUS

Hello Neo !

You can do it with "Type.GetType()" but before diving into the
programming world you need to remember following points;

1) If you are using "Type.GetType(<YOUR_TYPE>)" i.e. no assembly is
specified runtime will only look for the type in the calling assembly
and then in mscorlib.dll.

2) If you have a weak assembly i.e. NOT Strong-Named .. for runtime to
look for your type in any other assembly, you need to make a call such
as, Type.GetType(<Your_Type>, <Your_Assembly>); i.e. for example if
you have a type
"MyTestLib.MyClass" in an assembly "MyTestLib" then you need to make a
call like this Type.GetType("MyTestLib.MyClass, MyTestLib");. Reason is
simple, "part of type identity is the assembly".

3) For Strong-Named assembly you need to specify the information in the
following format: "<Your_Type>, <Assembly_Which_Contains_Your_Type>,
<Assembly_Version>, <Assembly_Culture>, <Public_Key_Token>, <Custom>".


For your convenience, below is an example that loads the
"System.Windows.Forms.Shortcut" type and then prints on the console
all the fields name.

<code>
private String SHORTCUT_TYPE_STRING = "System.Windows.Forms.Shortcut,
System.Windows.Forms, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089, Custom=null";
Type type = Type.GetType(this.SHORTCUT_TYPE_STRING);
if(type.IsEnum)
{
FieldInfo[] fields = type.GetFields();
foreach(FieldInfo field in fields)
{
if(field.FieldType.Name=="Shortcut")
{
Console.Writeline(field.Name);
}
}
}
</code>

NOTE: But you need to remember onething that assembly WILL GET LOADED
but it will be transparent to you.

You explicitly WONT need to load the assembly by using any of the 3
static methods of Assembly type i.e. "Assembly.Load()" or
"Assembly.LoadFrom()" or "Assembly.LoadFile()" and then with the loaded
assembly reference get the desired type information as,
<Loaded_Assembly_Reference>.GetType(<Your_Type>);

Hope this might be of some help.

Let me know in case of any inconsistancy.

Have a good day.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd
 
N

Neo

Thank you but all of these methods will load the assembly. I This won't
work for me. A lot of times I am loading multiple versions/builds of
the same assembly which causes a crash these assemblies are never
linked in at build time.

However all I need from them is the type names I don't need to execute
any code.
 
C

Chris Dunaway

Well, then create a new AppDomain, load the assembly in that appdomain
to get the type information and the unload the AppDomain which will
unload the assembly!
 
C

Chris Dunaway

And, if you are using VS2005, you can use ReflectionOnlyLoad to load an
assembly for reflection only.
 
M

Mattias Sjögren

What is this COM API called?

It's often just called "the unmanaged metadata API". You can find
documentation for it in the Framework SDK Tool Developers Guide\docs\
directory, and the interfaces (IMetaDataImport and
IAssemblyMetaDataImport) are declared in cor.h.


Mattias
 
N

Neo

Yea I saw that in 2005 I guess I may just wait it is right around the
corner. I tried loading it into another AppDomain , the problem is that
the code that actually does the reflection has to live in an assembly
and you have to load that one first which cause a lot of search path
problems. It looks like MS has it figured out with ReflectionOnlyLoad I
have to wait it out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top