Need Help

  • Thread starter Thread starter P.Sunil
  • Start date Start date
P

P.Sunil

I am using Reflection to print the
Fields,Methods,Constructors of a particular class.The
sample logic i am using is below.
When i am running the programme
if i am giving user input as System.Console Output is
showing correctly.But when i am giving user input as
System.Windows.Forms.Form I am getting an exception
"Object reference not set to an instance of an object."
Is ther any mistake in my programming logic.But I found
that the programme is referencing "mscorlib.dll" only.but
not other dll. Is it correct or any other problem please
rectify this.

using System;
using System.IO;
using System.Reflection;
public class reflectdemo
{
public static void Main()
{
try
{

string userinput;

System.Console.WriteLine("Enter
Full Class name");
userinput=System.Console.ReadLine
();
Type t =Type.GetType(userinput);

System.Console.WriteLine
("INFORMATION ");
System.Console.WriteLine("---------
-------------------------------------");
System.Console.WriteLine();

System.Console.WriteLine("Fields");
System.Console.WriteLine("-------
");
System.Reflection.FieldInfo[]
finfo=t.GetFields();
if(finfo.Length==0)
{
System.Console.WriteLine
("No Fields");
}
else
{
foreach(FieldInfo f in
finfo)
{

System.Console.WriteLine(f);
}
}

System.Console.ReadLine();
}
catch(System.Exception e)
{
System.Console.WriteLine
(e.Message);
System.Console.WriteLine(e.Source);
}
finally
{
System.Console.ReadLine();
}
}

}
 
P.Sunil said:
I am using Reflection to print the
Fields,Methods,Constructors of a particular class.The
sample logic i am using is below.
When i am running the programme
if i am giving user input as System.Console Output is
showing correctly.But when i am giving user input as
System.Windows.Forms.Form I am getting an exception
"Object reference not set to an instance of an object."
Is ther any mistake in my programming logic.But I found
that the programme is referencing "mscorlib.dll" only.but
not other dll. Is it correct or any other problem please
rectify this.

It's not talking about assembly references, it's saying that you're
trying to dereference a null reference in your code - in this case, t
is null because it couldn't find the System.Windows.Forms.Form type.
This is because Type.GetType(string) only looks in mscorlib and the
currently executing assembly unless you specify assembly information
along with the type name.
 
Back
Top