473,406 Members | 2,956 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,406 software developers and data experts.

Convert a string to Type

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
7 1990
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
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
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
"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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.