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