How Can I dynamically cast at runtime.That is I am passing a child class object in the parent class object in a function.In that function i have to cast to passed child class object at runtime.
How Can I Cast It.
protected
void Page_Load(object sender, EventArgs e){
Circle circleObj = new Circle();circleObj.Radius = 5;
Response.Write(chkRefrence(circleObj)+
"<br \\>After ");}
private string chkRefrence(Shape shapeObject){
//Type t = shapeObject.GetType(); ((t) shapeObject).Radius = 15; //It is here I want's to cast to CircleshapeObject.Id =
"TestObj "; return shapeObject.ToString();}

Dynamic Casting in C# at runtime
SkiesOfBordom
Mark J.
That's conversion, not casting. Since the type of o2 is object, you still can't access any members that are specific to the type you convert to without casting or using reflection.
diane06
will this help:
http://weblogs.sqlteam.com/mladenp/archive/2006/06/30/10430.aspx
http200
It is just a example .
I want's to ask about dynamic casting
i.e.
((t) shapeObject).Radius = 15; //It is here I want's to cast to Circle or what ever my class
Please if any body can answer to the point then help me
Fster
((Square) shapeObject).Radius = 15;
But Squares don't have a Radius property.
n0n4m3's example is what you really want.
PawanSingh
it depends on what you want to cast and to what type. If you have a shapeObject and you want to cast it safely to a circle then first check if the object is indeed a circle. Don't know if I understand you correctly but check the example:
Circle c = shapeObject as Circle;
if(c != null)
c.Radius = 15;
// or
if(shapeObject is Circle)
((Circle)c).Radius = 15;
George Shear
How about providing a realistic example then, one which shows what you're actually trying to accomplish. There's no such thing as "dynamic casting". Your options are basically
- Declare the members you want to use in a base class or interface and cast to that.
- Use generics.
- Use Reflection.
johngccfc
If you know there is a Radius property to set, you must also know that the type is Circle (or something else that has a Radius property - define an interface for it if there are other possible types). So just cast to that.
rternier
David Beavon
thedewd
But the problem isn't how to get the possible values of the enum, that's easy. It isn't even telling which or those values equates to the value string (again easy). The problem is how to set the value of an enumeration object that you don't know the type of at design time. I'm starting with an object which has a property which is an Enum of an unknown Type (unkown at deisgn time), and a string which represents the value to go into it. I need to end up with an object of a specified Type (known only at runtime) with the values of its properties set.
As I've said, a conversion between types exists for primitive types: Convert.ChangeType. So we can quibble over whether or not that is truly "dynamic casting", but that is academic. It is changing the Type of an object at runtime and "dynamic casting" seems like a good way to describe that to me. The bottom line is that I'm looking for something which works like Convert.ChangeType, but for Enums.
Anu Beniwal
Hi,
A big thank you to MladenP, coz he has displayed the link to the site on which i got the following code,
string sType = "System.Int32";
object o1 = "123";
object o2 = Convert.ChangeType(o1, Type.GetType(sType));
Type t = o2.GetType(); // this returns Int32 Type
So i guess u can dynamically cast at run-time!
Cheers,
Senaka.
kusanagihk
JFoushee
I know this is an old thread, but I am having the exact same problem and I can give a very concrete example.
I am writing a utility that dynamically creates a proxy class from a WSDL and then calls web methods on an instance of the proxy class. I have no idea what types exist in the class, but I need to create instances of them complete with all their properties (which may be custom classes themselves). So I am looping through the fields in the method's parameters using reflection and I have user supplied values that I need to insert into the parameters' fields.
Here's what I know at runtime:
1. The Object (representing the parameter) that I need to insert the value into.
2. The Type of the parameter field (from the FieldInfo class).
3. The value (as a string) that I need to insert into the parameter field.
Here's what I know at design time:
1. Jack squat. The parameter field could be any value type or a custom enum declared in the proxy class.
The Convert.ChangeType() thing works great for value types, but blows it on Enums. So the question stands:
Is there a way to dynamically cast an object if you know the type at runtime Here is the line of code I need to make work (assume the o = the Object, t = the Type, fi = the FieldInfo, value = the String value)
fi.SetValue(o, value)
The Type of the string value needs to match the Type of the field in the Object o.