[Mono-list] Reflection problem

Jonathan Pryor jonpryor@vt.edu
14 Feb 2003 12:23:45 -0500


The problem you're witnessing is the result of two pieces of documented
behavior:

1.  Calling Type.GetType(string) on a type that doesn't exists returns
    null.  (Type.GetType ("does-not-exist") == null)

2.  Type.GetType(string) only searches for the specified type in the
    entrypoint assembly (the .exe that started execution) and corlib
    (mscorlib.dll).  System.Data.IDbCommand is in the System.Data 
    assembly, which will not be searched.

To load a type from a particular assembly, you need to specify the
type's assembly-qualified name.  For example:

	Type t = Type.GetType ("System.Data.IDbCommand, System.Data");

In other words, append ", <assembly-name/>" to the type name.

This works under Mono, but .NET may (likely will) require more
information, such as the Version, Culture, and PulicKeyToken.  A sample
AssemblyQualifiedName for .NET is:

	System.Configuration.DictionarySectionHandler, System,	
	Version=1.0.3300.0, Culture=neutral,
	PublicKeyToken=b77a5c561934e089

Extra newlines added for readability; it should be on a single line,
though.

Alternatively, you could load the assembly through
System.Reflection.Assembly.Load() (or LoadFrom()), and then load the
type with System.Reflection.Assembly.GetType().  This may have the same
requirements for a fully-qualified assembly name, though, so it may not
actually be an improvement.

 - Jon

On Fri, 2003-02-14 at 02:27, Sanjaya Singharage wrote:
> Hi all,
> I am using the following class to discover classes, methods and properties.
> But for some classes (or are they interfaces) like
> System.Web.UI.WebControls.DataGrid and System.Data.IDbCommand when I call
> the method DispClsMethods I get the following error. Can somebody explain?
> 
> Unhandled Exception: System.NullReferenceException: A null value was found
> where an object instance was required
> in <0x0003b> 00 .ClassDets:DispClsMethods (string)
> in <0x00041> 00 .ClassDets:Main ()
> 
> The code for the class follows.
> 
> using System;
> using System.Reflection;
> using System.Web.UI.WebControls;
> using System.Web.UI;
> 
> public class ClassDets
> {
>       public void DispAssemblies()
>       {
>             //AssemblyName[] an = Assembly.GetReferencedAssemblies();
>       }
> 
>       public void DispClsMethods(string strType)
>       {
>             Type t = Type.GetType(strType);
>             MethodInfo [] mi = t.GetMethods();
> 
>             Console.WriteLine("milepost 3");
>             foreach(MethodInfo m in mi)
>             {
>                   Console.WriteLine(m);
>             }
>       }
> 
>       public void DispClsProps(string strType)
>       {
>             Type t = Type.GetType(strType);
>             PropertyInfo [] pi = t.GetProperties();
>             foreach(PropertyInfo p in pi)
>             {
>                   Console.WriteLine(p);
>             }
> 
>       }
> 
>       public void PrintClsFromAss(string strAssemblyName)
>       {
>             Assembly a = Assembly.Load(strAssemblyName);
>             Type[] types = a.GetTypes();
>             foreach(Type t in types)
>             {
>                   Console.WriteLine(t);
>             }
>       }
> 
>       public static void Main()
>       {
>             ClassDets cd = new ClassDets();
>             //cd.PrintClsFromAss("System.Data.dll");
>             cd.DispClsMethods("System.Data.IDbCommand");
>             //cd.DispClsMethods("System.Web.UI.WebControls.Table");
>             //cd.DispClsProps("System.Reflection.Assembly");
>       }
> 
> }
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list