473,790 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type.GetType(st ring) question

Hi folks,

Who can explain me why the following expression does not result in getting
the correct type, but null:

Type t = Type.GetType("S ystem.Xml.XmlRe ader");

For "System.Str ing" it works as well as for "System.IO.Stre am" or
"System.Globali zation.CultureI nfo". Does that related to the constructor of
XmlReader being not public?

Thanks,
Martin
Jun 27 '08 #1
8 3284
Type.GetType() doesn't automatically search all assemblies for the type;
actually, it expects an "assembly qualified name", but will forgive
you (i.e. allow the short name) if the type is found in either the
calling assembly (your code) or mscorlib.dll (common types).

The following is the aqn version of XmlReader (watch for wrap):

Type t = Type.GetType("S ystem.Xml.XmlRe ader, System.Xml,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9");

The easiest way to get the aqn is via:
string aqn = typeof(XmlReade r).AssemblyQual ifiedName;

(then write that to the trace or something if you want it for a config
file etc)

Marc
Jun 27 '08 #2
Hi Marc,

That already helps.

Now, the issue I have is that I get a string value set to
"System.Xml.Xml Reader". But it could also be "System.Str ing" or
"System.Xml.Xml NodeList" or whatever. Therefore I cannot issue the code line
string aqn = typeof(XmlReade r).AssemblyQual ifiedName;
statically, but have to replace the "typeof(XmlRead er)" by something like
"typeof(<myInpu tString>)".

Is there a way to accomplish that?

Thanks,
Martin

"Marc Gravell" <ma**********@g mail.comwrote in message
news:On******** ******@TK2MSFTN GP03.phx.gbl...
Type.GetType() doesn't automatically search all assemblies for the type;
actually, it expects an "assembly qualified name", but will forgive you
(i.e. allow the short name) if the type is found in either the calling
assembly (your code) or mscorlib.dll (common types).

The following is the aqn version of XmlReader (watch for wrap):

Type t = Type.GetType("S ystem.Xml.XmlRe ader, System.Xml, Version=2.0.0.0 ,
Culture=neutral , PublicKeyToken= b77a5c561934e08 9");

The easiest way to get the aqn is via:
string aqn = typeof(XmlReade r).AssemblyQual ifiedName;

(then write that to the trace or something if you want it for a config
file etc)

Marc

Jun 27 '08 #3
On Tue, 20 May 2008 00:53:04 -0700, Martin Eckart <moartl17ATyaho o.de
wrote:
Who can explain me why the following expression does not result in
getting
the correct type, but null:

Type t = Type.GetType("S ystem.Xml.XmlRe ader");
Do you have the System.Xml assembly referenced in your assembly?

Pete
Jun 27 '08 #4
Yes, have. Has nothing do to with it.
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Tue, 20 May 2008 00:53:04 -0700, Martin Eckart <moartl17ATyaho o.de>
wrote:
Who can explain me why the following expression does not result in
getting
the correct type, but null:

Type t = Type.GetType("S ystem.Xml.XmlRe ader");
Do you have the System.Xml assembly referenced in your assembly?

Pete
Jun 27 '08 #5
On May 20, 9:25 am, "Martin Eckart" <moartl17ATyaho o.dewrote:
That already helps.

Now, the issue I have is that I get a string value set to
"System.Xml.Xml Reader". But it could also be "System.Str ing" or
"System.Xml.Xml NodeList" or whatever. Therefore I cannot issue the code line
string aqn = typeof(XmlReade r).AssemblyQual ifiedName;
statically, but have to replace the "typeof(XmlRead er)" by something like
"typeof(<myInpu tString>)".

Is there a way to accomplish that?
No - typeof(...) is a compile-time operator - it looks up the type
name based on the context of the code and the referenced assemblies,
and works out the fully-qualified name at compile-time.

What you *can* do is recursively look through the assemblies
referenced by your current assembly (I can't remember the method name
off hand, but it's something obvious like
Assembly.GetRef erencedAssembli es) and call Assembly.GetTyp e on each of
those assemblies until you find the type.

Jon
Jun 27 '08 #6
The "typeof" line was just so you could obtain the assembly-qualified
name of the type - this isn't something you'd normally keep in the real
code. The idea being to use the assembly-qualified name *instead* of
"System.Xml.Xml Reader". Of course, if this isn't an option you'll have
to start trawling assemlbies...

Marc
Jun 27 '08 #7
What you *can* do is recursively look through the assemblies
referenced by your current assembly
Actually, one thing to note here is that the compiler is clever - it
will drop things that you have referenced but not used... just one to
watch if it doesn't work. You also need to watch for the circular
reference at the bottom ;-p

But something like below.

Marc

using System;
using System.Collecti ons.Generic;
using System.Reflecti on;
static class Program
{
static void Main()
{
Type type = GetType("System .Xml.XmlReader" );
}
static Type GetType(string name)
{
// tryu the lazy way first
Type type = Type.GetType(na me);
if (type != null) return type;

List<stringskip = new List<string>();
AssemblyName root = Assembly.GetEnt ryAssembly().Ge tName();
return WalkAssemblies( name, root, skip);
}
static Type WalkAssemblies( string name, AssemblyName an,
IList<stringski p)
{
// check "an" for the type
skip.Add(an.Ful lName);
Assembly a;
try {
a = Assembly.Load(a n);
} catch {
return null; // oops
}
Type type = a.GetType(name) ;
if (type != null) return type;

// see what is referenced
foreach(Assembl yName nextRef in a.GetReferenced Assemblies()) {
if(skip.Contain s(nextRef.FullN ame)) continue;
type = WalkAssemblies( name, nextRef, skip);
if (type != null) return type;
}
return null;

}
}
Jun 27 '08 #8


"Marc Gravell" <ma**********@g mail.comschreef in bericht
news:On******** ******@TK2MSFTN GP03.phx.gbl...
Type.GetType() doesn't automatically search all assemblies for the type;
actually, it expects an "assembly qualified name", but will forgive you
(i.e. allow the short name) if the type is found in either the calling
assembly (your code) or mscorlib.dll (common types).

The following is the aqn version of XmlReader (watch for wrap):

Type t = Type.GetType("S ystem.Xml.XmlRe ader, System.Xml, Version=2.0.0.0 ,
Culture=neutral , PublicKeyToken= b77a5c561934e08 9");
If you use just typename and assembly-name (no "Version" etc), then GetType
will find it (*if* the assembly was referenced).
So: Type t = Type.GetType("S ystem.Xml.XmlRe ader, System.Xml");

Hans Kesting
Jun 27 '08 #9

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
3286
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,
0
1474
by: Yuri Vanzine | last post by:
Saw this requested awhile ago on Matthew Reynolds's blog here: http://www.dotnet247.com/247reference/msgs/11/57944.aspx Problem: often times I deal with querystring-formatted strings w/o the actual HttpRequest object involved. Solution: the function accepts such a querystring-type string and outputs a NameValueCollection object
6
5444
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?
9
13279
by: Gugale at Lincoln | last post by:
In my code Type.GetType(text) works when text="System.IO.File". However, it doesn't work when text="System.Windows.Forms.Button". 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
7
7818
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...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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
10200
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...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5422
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...
1
4094
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
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.