I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 219, comments - 2287, trackbacks - 33

My Links

Advertisement

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 also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


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 | Filed Under [ .Net ]

Feedback

Gravatar

# 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
Gravatar

# 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
Gravatar

# 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
Gravatar

# 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
Gravatar

# 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
Gravatar

# 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
Gravatar

# 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
Gravatar

# 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
Gravatar

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


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

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

great thanks :)


kazim
8/3/2009 1:41 PM | kazim
Gravatar

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

Very Good. Help me a lot, Thanks!
11/25/2009 11:02 AM | Felipe M
Gravatar

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

Thanks a lot! Just in time:)
2/25/2010 11:31 AM | Serenya
Gravatar

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

Hi there,

I stucked all day with something:

Somewhere in code I have class named lets say Class1 and it look similar to:

public class Class1
{
public int iSomeInteger;
public double dSomeDouble;

public Class1()
{
}
}

In other class, lets say Class2, I have a method with string parameter (name of the class to be created). It should like this:

DoSomethingWhenSomething(string pClassName)
{
object o = Activator.CreateInstance(Type.GetType(pClassName));
}

and this code works just fine. What I want to do is to access public variables (iSomeInteger, dSomeDouble) and mehods from Class1 but I can't see these variables and methods! :(

My code that does not work is:

DoSomethingWhenSomething("Class1")
{
object o = Activator.CreateInstance(Type.GetType(pClassName));

// Here I want to call o.iSomeInteger but I can't! I just see standard object methods: Equals, GetHashCode, GetType, ToString
o.iSomeInteger = 1; // Error raises here!
}



Please help :(

Miljan
3/30/2010 7:36 PM | knyazs
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET