.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
Legacy Comments
rockmoose
2006-07-02 |
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 |
Mladen
2006-07-02 |
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 :) |
rockmoose
2006-07-02 |
re: .Net Cast/Convert object to a specified type at runtime Thanks for sharing, you dirty hacker ;) rockmoose |
Arnaud Weil
2006-07-23 |
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! |
Neale
2007-05-22 |
re: .Net Cast/Convert object to a specified type at runtime Great bit of code. Really does help |
Vadim
2007-06-06 |
re: .Net Cast/Convert object to a specified type at runtime It only works if object o1 implements IConvertible Interface. |
sabata
2007-06-21 |
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 |
sabata
2007-06-21 |
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! |
Ay
2007-10-17 |
re: .Net Cast/Convert object to a specified type at runtime It really helps. Thanks. |
kazim
2009-08-03 |
re: .Net Cast/Convert object to a specified type at runtime great thanks :) kazim |
Felipe M
2009-11-25 |
re: .Net Cast/Convert object to a specified type at runtime Very Good. Help me a lot, Thanks! |
Serenya
2010-02-25 |
re: .Net Cast/Convert object to a specified type at runtime Thanks a lot! Just in time:) |
knyazs
2010-03-30 |
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 |