I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 156, comments - 1394, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

.Net Cast/Convert object to a specified type at runtime

I've been doing a "bit" of .net development lately and i had a problem where i would get
a variable of type object and it's type in a string. So I wondered how to convert it to proper type at runtime.

The solutions is one of those things that is unbeliveably hard to find because you don't really know what
to search for, but when you find it, it seems unbeliveably simple. :)
Here it is:
string sType = "System.Int32"; 
object o1 = "123"; 
object o2 = Convert.ChangeType(o1, Type.GetType(sType)); 
Type t = o2.GetType(); // this returns Int32 Type

Print | posted on Friday, June 30, 2006 11:27 AM

Feedback

# re: .Net Cast/Convert object to a specified type at runtime

Yes, but...
Getting the clr type in a string variable seems to be a flawed design to me!

rockmoose
7/2/2006 11:04 PM | rockmoose

# re: .Net Cast/Convert object to a specified type at runtime

i agree.
but sometimes one doesn't have a choice. :)

but as a lot of other things it seems not needed just until the point of when you need it :)
7/2/2006 11:30 PM | Mladen

# re: .Net Cast/Convert object to a specified type at runtime

Thanks for sharing, you dirty hacker ;)

rockmoose
7/2/2006 11:59 PM | rockmoose

# re: .Net Cast/Convert object to a specified type at runtime

Thanks, that's very nice, but it only works with types that implement IConvertible (int, double and so on). My problem is that I want to dynamically cast a DataTable to the corresponding typed DataTable from a typed DataSet...

Thanks for sharing anyway!
7/23/2006 6:46 AM | Arnaud Weil

# re: .Net Cast/Convert object to a specified type at runtime

Great bit of code.
Really does help
5/22/2007 11:46 AM | Neale

# re: .Net Cast/Convert object to a specified type at runtime

It only works if object o1 implements IConvertible Interface.
6/6/2007 8:06 PM | Vadim

# re: .Net Cast/Convert object to a specified type at runtime

I have a problem and i see part of the solution in your code above,

To do:
1. call a function from a webservice e.g.
genericRead("GetProject.AddNewProject", arrayParams)
//BusinessLayer.GetProject is the name of the namespace.class within the BusinessLayer residing on the Application Server
//arrayParams is the array consisting the records, field names and values from the event that was triggered on the UI WebServer
2. inside the function genericRead in the webservice is some coding e.g
Public Function GenericRead(ByVal ClassName as string, ByVal Params as ArrayList) as DataSet
'at this point i need to call the class from within this scope as
'provided in the ClassName variable to access the functions e.g

Dim obj as BusinessLayer.GetProject = new BusinessLayer.GetProject
Dim ds as DataSet
ds = obj.AddNewProject(Params(0), Params(n))
return ds
End Function

if anyone can help in this respect its will be appriciated

Thanks bye bye
6/21/2007 1:12 PM | sabata

# re: .Net Cast/Convert object to a specified type at runtime

I was not cler as to what i want:

I need to convert the string that gets sent through as a parameter to a class in the application

if you need more information please shout out!
6/21/2007 1:16 PM | sabata

# re: .Net Cast/Convert object to a specified type at runtime


It really helps. Thanks.
10/17/2007 10:23 PM | Ay

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 4 and type the answer here:

Powered by: