I want to get the model during BindModel and cast it to a type specified in the bindingContext:
var reportFormTypeName = bindingContext.ValueP开发者_高级运维rovider.GetValue(bindingContext.ModelName + ".TableInputModelTypeName");
Type reportFormType = Type.GetType("MyNameSpace.ViewModels." + reportFormTypeName.AttemptedValue);
var model = (reportFormType)bindingContext.ModelMetadata.Model;
This wont work however - i'm guessing it's a simple reflection thing that i can't get my tired brain to sort out - anyone got any clues?
:)
You need to instantiate it:
var reportFormTypeName = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".TableInputModelTypeName");
Type reportFormType = Type.GetType("MyNameSpace.ViewModels." + reportFormTypeName.AttemptedValue);
var model = Activator.CreateInstance(reportFormType);
You can't do that. You're asking the compiler to work with a type that's known only at run time, which is simply not possible. If you know the type at compile time, just cast to it. If you don't, how do you expect to work with it?
精彩评论