473,803 Members | 3,073 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type.GetType(St ring) not working

In my code Type.GetType(te xt) works when text="System.IO .File". However, it
doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows. Forms.dll assembly?

Thanks
SG
Apr 27 '06 #1
9 13279
code4life
7 New Member
Actually you need to pass the fully qualified assembly name. The following example will reveal that more than just "System.Windows .Forms.Button" is required:

// I'm assuming this will go into a winform project, since you're referring to a button... you can call this method from the form you copy it over to.
public static void TypeTest()
{
//the actual string will be "System.Windows .Forms.Button, System.Windows. Forms, Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9" -- when you debug the code, this will become evident...
string fullyqualifiedn ame = new Button().GetTyp e().AssemblyQua lifiedName;
MessageBox.Show (fullyqualified name);
MessageBox.Show (System.Type.Ge tType(fullyqual ifiedname).Name );
}

In my code Type.GetType(te xt) works when text="System.IO .File". However, it
doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows. Forms.dll assembly?

Thanks
SG
Apr 27 '06 #2
"Gugale at Lincoln" <ph*******@yaho o.com> wrote:
In my code Type.GetType(te xt) works when text="System.IO .File". However, it
doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows. Forms.dll assembly?


I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.

You can however still load the button class as long as you fully qualify
the type:

| Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms, Culture=neutral , Version=2.0.0.0 , PublicKeyToken= b77a5c561934e08 9")

-- Barry
Apr 27 '06 #3
>I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.


No it's because when you don't specify the assembly name, Type.GetType
only looks in mscorlib.dll and the calling assembly. That works for
System.IO.File but not something in the Winforms assembly.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 27 '06 #4
Barry Kelly <ba***********@ gmail.com> wrote:
"Gugale at Lincoln" <ph*******@yaho o.com> wrote:
In my code Type.GetType(te xt) works when text="System.IO .File". However, it
doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows. Forms.dll assembly?


I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.


From the docs of Type.GetType(st ring):

<quote>
If typeName includes only the name of the Type, this method searches in
the calling object's assembly, then in the mscorlib.dll assembly. If
typeName is fully qualified with the partial or complete assembly name,
this method searches in the specified assembly.
</quote>

(The OP is mistaken about types from System.dll - System.IO.File, for
instance, is in mscorlib.dll. Trying a type which is genuinely in
System.dll, such as System.IO.FileS ystemWatcher, returns null as
expected given the above docs.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 27 '06 #5
Mattias Sjögren <ma************ ********@mvps.o rg> wrote:
I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.


No it's because when you don't specify the assembly name, Type.GetType
only looks in mscorlib.dll and the calling assembly. That works for
System.IO.File but not something in the Winforms assembly.


At first I thought it was just what you mentioned, that the assembly name
is missing. But it's more than that.

If you try it with just the class name and assembly name and without the
Version, Culture and PublicKeyToken fields it still doesn't work. You need
all fields for it to work.

-- Barry
Apr 27 '06 #6
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
From the docs of Type.GetType(st ring):

<quote>
If typeName includes only the name of the Type, this method searches in
the calling object's assembly, then in the mscorlib.dll assembly. If
typeName is fully qualified with the partial or complete assembly name,
this method searches in the specified assembly.
</quote>


If you try

| Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms")

you'll find it returns null, at least when you have both .NET 1.1 and 2.0
installed (I'm guessing). I tried before posting.

-- Barry
Apr 27 '06 #7
Barry Kelly <ba***********@ gmail.com> wrote:
<quote>
If typeName includes only the name of the Type, this method searches in
the calling object's assembly, then in the mscorlib.dll assembly. If
typeName is fully qualified with the partial or complete assembly name,
this method searches in the specified assembly.
</quote>


If you try

| Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms")

you'll find it returns null, at least when you have both .NET 1.1 and 2.0
installed (I'm guessing). I tried before posting.


I don't think that counts as "enough" of the assembly name - I think
you need to give at least some of the version number. Certainly if you
give the *complete* assembly name (with full version number) it will
work. There's more to an "assembly name" than the file name. It's not
terribly well documented, unfortunately.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 27 '06 #8
Thanks Barry!

In case if anyone is wondering, as I did, you can get version number and
public key using
gacutil /l assemblyname from Visual Studio .Net Command Prompt

"Barry Kelly" <ba***********@ gmail.com> wrote in message
news:4t******** *************** *********@4ax.c om... "Gugale at Lincoln" <ph*******@yaho o.com> wrote:
In my code Type.GetType(te xt) works when text="System.IO .File". However,
it
doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
for classes in System.dll assembly. Is there anyway I can make it work
for
classes in System.Windows. Forms.dll assembly?


I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.

You can however still load the button class as long as you fully qualify
the type:

| Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms,
Culture=neutral , Version=2.0.0.0 , PublicKeyToken= b77a5c561934e08 9")

-- Barry

Apr 27 '06 #9
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
Barry Kelly <ba***********@ gmail.com> wrote:
If you try

| Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms")

you'll find it returns null, at least when you have both .NET 1.1 and 2.0
installed (I'm guessing). I tried before posting.


I don't think that counts as "enough" of the assembly name - I think
you need to give at least some of the version number. Certainly if you
give the *complete* assembly name (with full version number) it will
work. There's more to an "assembly name" than the file name. It's not
terribly well documented, unfortunately.


The version number isn't enough. You also need *both* the Culture *and*
PublicKeyToken. I tried pretty much every combination.

I've used this method myself for extensibility scenarios, and all the
times I've used it I've only needed the type name and assembly name - I've
never seen the behaviour that System.Windows. Forms.Button manifests.

Odd. <g>

-- Barry
Apr 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5659
by: Kendall Gifford | last post by:
Greetings. While trying to get a simple app working, I've been forced to delve into embedded and/or linked resources a bit. I read all the reference for the System.Resources namespace as well as all the material within the "Resources and Localization..." tutorial. While I'm confident I now know completely how to accomplish my original, simple task of embedding some icons into my assembly and use of the same at runtime, I realize I've a...
3
3287
by: the fuzz | last post by:
yeah g'day..... is it somehow possible to not include the version & public key token in the call to Type.GetType(string) ie something like Type.GetType("System.Windows.Forms.ListView, System.Windows.Forms"); instead of Type.GetType("System.Windows.Forms.TextBox,
6
5445
by: tshad | last post by:
The error I am getting is: ******************************************************************* Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid. Source Error: Line 144: firstName.text = ClientReader("firstName") Line 145: lastName.text = ClientReader("lastName")
4
1840
by: Sparky Arbuckle | last post by:
I am looping through a listbox collection to build a SQL string that will be used to delete items from a database. I have tried many variances of the code below but have had no luck. The code below gives an error: Cast from type 'ListItem' to type 'String' is not valid. When i do a response.write(s) the item at the top row displays correctly. Here is the code I am using:
9
1941
by: Ben | last post by:
Hello, I'm not a developper, so sorry if it's a stupid question... I'm trying to develop an application in vb.net and I have the following problem: I have some information in an array: sdist(i). The information is a string. When I run the application, I don"t have problem for compilation but during te execution, I have the error Cast from type 'Object()' to type 'String' is not valid on the line: sdistinguishedname = Sdist(i). Or...
1
3192
by: Jason Chan | last post by:
in asp.net 2.0, Page.RegisterClientScriptBlock is replaced by Client.RegisterClientScriptBlock function signature: Client.RegisterClientScriptBlock(Type, String, String) what should i put on the Type?
7
7821
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
8
3285
by: Martin Eckart | last post by:
Hi folks, Who can explain me why the following expression does not result in getting the correct type, but null: Type t = Type.GetType("System.Xml.XmlReader"); For "System.String" it works as well as for "System.IO.Stream" or "System.Globalization.CultureInfo". Does that related to the constructor of XmlReader being not public?
2
3809
boss32178
by: boss32178 | last post by:
Ok, I am new to the form and been working with c# for 3 months now. I am creating a dll that i need to use in my asp.net page The dll grabs all the forms and creat a xml page, and grabs a xml page and fills out a form in you need to update the form. my problem is i have a check box with an if statement. I am geting an error that says cannot inplicity covert type string to bool here is the code that i am using. case "CheckBox": Cb...
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.