0

I want to create a new instance of a class type only known at runtime, and cast it, based on a given object. I have an "if" statement to make it, since the possible class types are known. However this doesn't seem like a good way to make mantainable or even efficient code. Is there a better way to do this in c# ?

if (car.Color.GetType().ToString() == "My.Domain.ColorBlue") 
{
    ColorBlue color = new ColorBlue();
}
else if (car.Color.GetType().ToString() == "My.Domain.ColorRed")
{
    ColorRed color = new ColorRed();
}


car.Color = color;   
return PartialView("XPTO.cshtml", car);

I want to pass the derived class to a MVC View, so I really need it to be casted to the correct type. The view then calls one of many other possible partial views:

           if (viewDatatype == "My.Domain.ColorBlue")

           {

               Html.RenderPartial("XPTOBlue.cshtml", @Model.Color);

           }
else if(viewDatatype == "My.Domain.ColorRed")
(...)

and these partial views are strongly typed to one of the derived classes, for example:

@model My.Domain.ColorBlue
PF_learning
  • 25
  • 13

1 Answers1

1

For all but the casting part, you can just use:

object newInstance = Activator.CreateInstance(car.Color.GetType());

If that will always implement a particular interface or have a particular base class, you could cast to that - but you can't cast to the actual type, as casting involves knowing the type at compile-time.

So you might have:

Color newColor = (Color) Activator.CreateInstance(car.Color.GetType());

You should always be able to cast to the compile-time type of car.Color, for starters... For example, if you're just trying to replace the existing Color property value in car with a new value of the same type, and if the compile-time type of car.Color is MyColor, you can use:

car.Color = (MyColor) Activator.CreateInstance(car.Color.GetType());
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hmm but I want to pass the derived class to a MVC View, so I really need it to be casted to the correct type.. :/ – PF_learning Dec 10 '14 at 09:22
  • How are you passing it to the view? How do you know which view to pass it to? The object will *be* of the right type - you just won't know that type at compile time. More context would be useful here. (It's possible that `dynamic` would help, but we can't really tell at the moment.) – Jon Skeet Dec 10 '14 at 09:23
  • `car.Color = color; return PartialView("XPTO.cshtml", car);` The view then calls one of many other possible partial views: `if (viewDatatype == "My.Domain.ColorBlue") { Html.RenderPartial("XPTOBlue.cshtml", @Model.Color); } (...)` and these partial views are strongly typed to one of the derived classes, for example: `@model My.Domain.ColorBlue` – PF_learning Dec 10 '14 at 09:30
  • @PF_learning: You should put that into the *question* - ideally with more context still, to be honest. (It's not clear how that snippet relates to the one you've asked about.) But if you're going to have to change view based on the type as well, it's not clear how much benefit you're getting from building the instance dynamically. Again, `dynamic` *may* help you, but we haven't really got enough context. – Jon Skeet Dec 10 '14 at 09:32
  • I want to provide all the necessary information but I don't know what more to post :/ – PF_learning Dec 10 '14 at 09:38
  • @PF_learning: Well you've posted two different snippets but we have no idea how they're related to each other. If you're *just* trying to assign a new value to `car.Color` then you don't need to know the actual type involved. You can just cast to the compile-time type of the property. See my edit for an example. – Jon Skeet Dec 10 '14 at 09:39