473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Type.GetType(string) 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("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?

Thanks,
Martin
Jun 27 '08 #1
8 3246
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("System.Xml.XmlReader, System.Xml,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

The easiest way to get the aqn is via:
string aqn = typeof(XmlReader).AssemblyQualifiedName;

(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.XmlReader". But it could also be "System.String" or
"System.Xml.XmlNodeList" or whatever. Therefore I cannot issue the code line
string aqn = typeof(XmlReader).AssemblyQualifiedName;
statically, but have to replace the "typeof(XmlReader)" by something like
"typeof(<myInputString>)".

Is there a way to accomplish that?

Thanks,
Martin

"Marc Gravell" <ma**********@gmail.comwrote in message
news:On**************@TK2MSFTNGP03.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("System.Xml.XmlReader, System.Xml, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089");

The easiest way to get the aqn is via:
string aqn = typeof(XmlReader).AssemblyQualifiedName;

(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 <moartl17ATyahoo.de
wrote:
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");
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*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 20 May 2008 00:53:04 -0700, Martin Eckart <moartl17ATyahoo.de>
wrote:
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");
Do you have the System.Xml assembly referenced in your assembly?

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

Now, the issue I have is that I get a string value set to
"System.Xml.XmlReader". But it could also be "System.String" or
"System.Xml.XmlNodeList" or whatever. Therefore I cannot issue the code line
string aqn = typeof(XmlReader).AssemblyQualifiedName;
statically, but have to replace the "typeof(XmlReader)" by something like
"typeof(<myInputString>)".

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.GetReferencedAssemblies) and call Assembly.GetType 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.XmlReader". 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.Collections.Generic;
using System.Reflection;
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(name);
if (type != null) return type;

List<stringskip = new List<string>();
AssemblyName root = Assembly.GetEntryAssembly().GetName();
return WalkAssemblies(name, root, skip);
}
static Type WalkAssemblies(string name, AssemblyName an,
IList<stringskip)
{
// check "an" for the type
skip.Add(an.FullName);
Assembly a;
try {
a = Assembly.Load(an);
} catch {
return null; // oops
}
Type type = a.GetType(name);
if (type != null) return type;

// see what is referenced
foreach(AssemblyName nextRef in a.GetReferencedAssemblies()) {
if(skip.Contains(nextRef.FullName)) continue;
type = WalkAssemblies(name, nextRef, skip);
if (type != null) return type;
}
return null;

}
}
Jun 27 '08 #8


"Marc Gravell" <ma**********@gmail.comschreef in bericht
news:On**************@TK2MSFTNGP03.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("System.Xml.XmlReader, System.Xml, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089");
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("System.Xml.XmlReader, 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
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...
3
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,...
0
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...
6
by: tshad | last post by:
The error I am getting is: ******************************************************************* Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not...
4
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...
9
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:...
1
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...
9
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...
7
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}"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.