GetType returns null on a loaded assembly

Z

Zoury

Good day! :O)

I have the following code :

//***
using System;
using System.Windows.Forms;

<snip>

private void button1_Click(object sender, System.EventArgs e)
{
Type type = Type.GetType("System.Windows.Forms.TextBox");
if (type != null)
{
MessageBox.Show(type.FullName);
}
}
//***

I've read in the archives that GetType() returns null if the assembly isn't
loaded... In this case the assembly is loaded but GetType always returns
null. Any ideas on what I'm doing wrong?

Thanks a lot!
 
M

Mattias Sjögren

I've read in the archives that GetType() returns null if the assembly isn't
loaded... In this case the assembly is loaded but GetType always returns
null. Any ideas on what I'm doing wrong?

When you don't explicitly provide the assembly name, Type.GetType()
only looks for the type in mscorlib and the calling assembly. Whether
or not you have loaded System.Windows.Forms is irrelevant.

This should work

Type type = Type.GetType("System.Windows.Forms.TextBox, " +
"System.Windows.Forms, Version=1.0.5000.0, " +
"Culture=neutral, PublicKeyToken=b77a5c561934e089" );

But since you obviously have System.Windows.Forms referenced, it's
better to do

Type type = typeof(System.Windows.Forms.TextBox);



Mattias
 
Z

Zoury

Hi Mattias!

Type type = Type.GetType("System.Windows.Forms.TextBox, " +
"System.Windows.Forms, Version=1.0.5000.0, " +
"Culture=neutral, PublicKeyToken=b77a5c561934e089" );

Thanks! And where do I find this information? (i'm new to the .NET
environment..)

But since you obviously have System.Windows.Forms referenced, it's
better to do

Type type = typeof(System.Windows.Forms.TextBox);

I wish I could. ;O)

Actually I need a function that returns the property value of an object but
the only information I have to do so are of string type.

Something like :
private object GetPropertyValue(string sClassName, string sIntanceName,
string sPropertyName);

is it possible? thanks again!
 
Z

Zoury

I got it.. i'm just not sure about the GetControl function.. isn't there a
better way?

that did the trick :
//***
private void button1_Click(object sender, System.EventArgs e)
{

string sAssemblyInfo = "System.Windows.Forms.TextBox,
System.Windows.Forms, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089";
string sControlName = "textBox1";
string sPropertyName = "Text";

Type type = Type.GetType(sAssemblyInfo);
if (type != null)
{

MessageBox.Show((string)type.GetProperty(sPropertyName).GetValue(GetControl(
sControlName), null));
}
}
private Control GetControl(string sControlName)
{
Control control = null;
foreach (Control ctl in this.Controls)
{
if (ctl.Name == sControlName)
{
control = ctl;
break;
}
}
return control;
}

Now I only need to know where you got the assembly information of a given
class. :O)
Thanks for your help!
 

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