I have very simple question that I cannot find the answer to.
In the razor view there’s a Model of interface type. That interface have multiple subclasses implementing it.
The question is: How can I cast the model to its actual type?
I MUST have variable of actual type but don’t know how to get it from the reflection
I.E.
@model IInterface
@{
var actualType1 = Model as Model.GetType(); // doesn't work :(
var actualType2 = (Model.GetType()) Model; // doesn't work :(
}
As you can imagine I am not interested in Model as ActualType or (ActualType) Model because I don’t know what actual type is coming in advance. Its a question about the technique rather than architecture.
Thanks in advance!
You are really on the wrong path here. You can’t really do what you’re looking to do here, because you are not really thinking it through. A statement like this:
Won’t work because var is not a dynamic type, it’s a static type that is typed at compile time, not at runtime. So even if you could somehow manage to get it to work, it would only work for one type, ever (until it was recompiled again).
In addition to that, interfaces are a poor choice for this, because interface attributes are seperate from implementation attributes. So regardless of what attributes you have on your classes, you will not get them via an interface to that class.