Kamii47

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 Circle

shapeObject.Id = "TestObj ";

return shapeObject.ToString();

}




Re: Visual C# Language Dynamic Casting in C# at runtime

Mattias Sjogren

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.






Re: Visual C# Language Dynamic Casting in C# at runtime

Kamii47

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






Re: Visual C# Language Dynamic Casting in C# at runtime

Mattias Sjogren

Kamii47 wrote:

It is just a example .

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.






Re: Visual C# Language Dynamic Casting in C# at runtime

n0n4m3

Hi,
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;






Re: Visual C# Language Dynamic Casting in C# at runtime

Kamii47

I don't know which class to be cast at rutime.certtainly it will be a child class.




Re: Visual C# Language Dynamic Casting in C# at runtime

CWIZO

Read Mattias Sjogren's post then




Re: Visual C# Language Dynamic Casting in C# at runtime


Re: Visual C# Language Dynamic Casting in C# at runtime

James Curran

The problem is that you really don't know what you want to do. For example, your chkRefrence method won't work. I mean, even if it were to compile, it would do what you wanted it to do. For example, consider what would happen if you called it with a Square object. The important line would be:
((Square) shapeObject).Radius = 15;

But Squares don't have a Radius property.

n0n4m3's example is what you really want.





Re: Visual C# Language Dynamic Casting in C# at runtime

Kamii47

I have done it using generic and it were a very old issue.




Re: Visual C# Language Dynamic Casting in C# at runtime

senaka

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.





Re: Visual C# Language Dynamic Casting in C# at runtime

Mattias Sjogren

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.






Re: Visual C# Language Dynamic Casting in C# at runtime

gprayback

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.





Re: Visual C# Language Dynamic Casting in C# at runtime

micvos

Youre problem again has nothing to do with dynamic casting (it really doesn't exist, not in concept, not in any language). Youre problem has to do with how to use reflection. Use reflection on the Enums type to get the possible values. It's found in the field types of the enumeration.




Re: Visual C# Language Dynamic Casting in C# at runtime

gprayback

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.