In a razor section
@{
Like This
}
I need to be able to call the c# function
static T Cast<T>(object o)
{
return (T)o;
}
In order to dynamically cast an object. How can I access this code?
I would like to be able to do this
someObject = Cast<someType>(someObject);
Edit
I am trying to implement a generic view model.
Here is the model:
public class GenericVM<TEntity> where TEntity : class
{
public List<TEntity> Entities { get; set; }
public object Entity { get; set; }
public Type Type { get; set; }
}
I am filling the GenericVM object in the controller then passing it in to a view with @model dynamic.
Once in the view, I am attempting to use the Type from the object to dynamically cast the model back to the proper type.
Can you put it into an Html extension?
Then just:
From your view. I use a string here as a usage example, just change for your own model type.