473,657 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("C lass1");

but it returns null.
Jan 3 '07 #1
7 2007
Do you have the namespace that includes the class? You might also try the
full namespace for the class such as
Type.GetType("M yNameSpace.Clas ses.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******** ********@TK2MSF TNGP02.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("C lass1");

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.Reflecti on.Assembly asm;

asm = System.Reflecti on.Assembly.Get Assembly(this.G etType());

Type[] typearray = asm.GetTypes();

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

"Mark Fitzpatrick" <ma******@fitzm e.comwrote in message
news:eD******** ******@TK2MSFTN GP02.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("M yNameSpace.Clas ses.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******** ********@TK2MSF TNGP02.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("C lass1");

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.Query String["classname"];
switch (q)
{
case "ClassX":
BaseClass=new ClassX();break;
case "ClassY":
BaseClass=new ClassY();break;
case "ClassZ":
BaseClass=new ClassZ();break;
}

grid.DataSource =BaseClass().Ge tBrowsableData( );

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*****@REMOVE ITwp.plwrote in message
news:66******** *************** ***********@mic rosoft.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.Reflecti on.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
InstanceOfClass 1.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("C lass1");

but it returns null.

Jan 3 '07 #4
"Milosz Skalecki [MCAD]" <mi*****@REMOVE ITwp.plwrote in message
news:EA******** *************** ***********@mic rosoft.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.Comp ilation.BuildMa nager.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("C lass1");

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*****@REMOVE ITwp.plwrote in message
news:26******** *************** ***********@mic rosoft.com...
Hi Saber,

There's another method to obtain type loaded from dynamically loaded
assemblies:
System.Web.Comp ilation.BuildMa nager.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("C lass1");

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
3619
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 of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and Option along with a small helper function. In the code are comments describing my problem, look closely...
4
17675
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 to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
5
3780
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 List<RoleData> GetRoles() { return GetRoles(null, false); }
9
17532
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() == "3"; Convert<HelloWorld>::Get() == "HelloWorld"; The reason I need this is that in our design every class of a certain
0
10752
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8425
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
8326
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
8743
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
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
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
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
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.