By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

Convert a string to Type

P: n/a
In an ASP.Net Website I've a string and want to convert that to Type.
Let's consider the string "Class1"
I tried: Type t = Type.GetType("Class1");

but it returns null.
Jan 3 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Do you have the namespace that includes the class? You might also try the
full namespace for the class such as
Type.GetType("MyNameSpace.Classes.Class1"); The class has to be accessible
in order for it to create the type so usually the most common problem is a
missing namespace reference.

--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"Saber" <saber[.AT.]oxin.irwrote in message
news:up****************@TK2MSFTNGP02.phx.gbl...
In an ASP.Net Website I've a string and want to convert that to Type.
Let's consider the string "Class1"
I tried: Type t = Type.GetType("Class1");

but it returns null.

Jan 3 '07 #2

P: n/a
No, it is a website, and when you create a New Website,
you see there is no namespace like windows forms.
My class is in a subfolder of App_Code folder.
I read somewhere, in the Websites you should include full assembly.
I also tried:
System.Reflection.Assembly asm;

asm = System.Reflection.Assembly.GetAssembly(this.GetTyp e());

Type[] typearray = asm.GetTypes();

after excuting, the typearray contains 3 items:
typearray[0].Value={Name = "_Default" FullName = "_Default"}
typearray[1].Value={Name = "default_aspx" FullName = "ASP.default_aspx"}
typearray[2].Value={Name = "FastObjectFactory_app_web_bdecafyb" FullName =
"__ASP.FastObjectFactory_app_web_bdecafyb"}
But where is Class1?! I think it is because I put this.GetType() as the
parameter of GetAssembly.

"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:eD**************@TK2MSFTNGP02.phx.gbl...
Do you have the namespace that includes the class? You might also try the
full namespace for the class such as
Type.GetType("MyNameSpace.Classes.Class1"); The class has to be accessible
in order for it to create the type so usually the most common problem is a
missing namespace reference.

--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"Saber" <saber[.AT.]oxin.irwrote in message
news:up****************@TK2MSFTNGP02.phx.gbl...
>In an ASP.Net Website I've a string and want to convert that to Type.
Let's consider the string "Class1"
I tried: Type t = Type.GetType("Class1");

but it returns null.


Jan 3 '07 #3

P: n/a
Thanks for replies,

I can't use typeof(Class1) or something similar,
because I don't know the string, and "Class1" is just an example.

I'll explain more,
There are lots of classes in business layer, they behave same and are
inherited from one base class.
There is a WebForm that users load it through menus, and the
classname given in QueryString.

Now we have something like it in the WebForm:

BaseClass baseclass;
string q=Request.QueryString["classname"];
switch (q)
{
case "ClassX":
BaseClass=new ClassX();break;
case "ClassY":
BaseClass=new ClassY();break;
case "ClassZ":
BaseClass=new ClassZ();break;
}

grid.DataSource=BaseClass().GetBrowsableData();

I want to get rid of this switch case statement, because the programmers
in our team want to add many classes.
In this situation, for each class they've to add a switch case and checkouts
and checkins and waitings!

So, it seems there is "no" way to do that in a WebSite. Right?
"Milosz Skalecki [MCAD]" <mi*****@REMOVEITwp.plwrote in message
news:66**********************************@microsof t.com...
Howdy,

Static method GetType(String) works on assemblies loaded from disk. MSFT
say "If you call GetType(String) to look up a type defined in a dynamic
assembly defined using the System.Reflection.Emit services, you might get
inconsistent behavior. The behavior depends on whether the dynamic
assembly
is persistent, that is, created using the RunAndSave or Save access"

instead use
typeof(Class1)
or
InstanceOfClass1.GetType()

hope this helps
--
Milosz
"Saber" wrote:
>In an ASP.Net Website I've a string and want to convert that to Type.
Let's consider the string "Class1"
I tried: Type t = Type.GetType("Class1");

but it returns null.

Jan 3 '07 #4

P: n/a
"Milosz Skalecki [MCAD]" <mi*****@REMOVEITwp.plwrote in message
news:EA**********************************@microsof t.com...
The problem is that classes in the ap_code are compiled to separate
assembly
Which means, as I understand it, that any code in the App_Code folder gets
compiled twice...
Jan 3 '07 #5

P: n/a
Hi Saber,

There's another method to obtain type loaded from dynamically loaded
assemblies:
System.Web.Compilation.BuildManager.GetType(...)
--
Milosz
"Saber" wrote:
In an ASP.Net Website I've a string and want to convert that to Type.
Let's consider the string "Class1"
I tried: Type t = Type.GetType("Class1");

but it returns null.
Jan 22 '07 #6

P: n/a
Thanks Milosz,
I'll try it tomorrow and well tell you the result.

"Milosz Skalecki [MCAD]" <mi*****@REMOVEITwp.plwrote in message
news:26**********************************@microsof t.com...
Hi Saber,

There's another method to obtain type loaded from dynamically loaded
assemblies:
System.Web.Compilation.BuildManager.GetType(...)
--
Milosz
"Saber" wrote:
>In an ASP.Net Website I've a string and want to convert that to Type.
Let's consider the string "Class1"
I tried: Type t = Type.GetType("Class1");

but it returns null.

Jan 22 '07 #7

P: n/a

Just tried the above method, and it does indeed work for classes in
appcode. Thanks!
*** Sent via Developersdex http://www.developersdex.com ***
Feb 3 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.