469,582 Members | 2,275 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,582 developers. It's quick & easy.

Get Assembly Type Names Without Loading The Assembly

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

Oct 24 '05 #1
11 10411
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 24 '05 #2
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

Oct 25 '05 #3
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.

Oct 25 '05 #4
Neo
What is this COM API called?

Oct 25 '05 #5
Neo
What is this COM API called?

Oct 25 '05 #6
Neo
What is this COM API called?

Oct 25 '05 #7
You may find following utility helpful in solving your problem -
(Finding types without loading Assembly),

http://blogs.ineta.org/devin/archive.../07/19424.aspx

regards,
Khurram Shakir
MVP Visual C#

Neo wrote:
What is this COM API called?


Oct 25 '05 #8
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!

Oct 25 '05 #9
And, if you are using VS2005, you can use ReflectionOnlyLoad to load an
assembly for reflection only.

Oct 25 '05 #10
>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 25 '05 #11
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.

Oct 25 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Arnaud Debaene | last post: by
3 posts views Thread by Atul Godbole | last post: by
5 posts views Thread by Abdul | last post: by
1 post views Thread by Luis Pinho | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.