対応する MVC の質問に沿って、ASP.NET Web API でジェネリック型のモデル バインダーを作成する方法はありますか?
はいの場合、バインダーでの型チェックとインスタンス化をどのように処理しますか? モデル バインダーが URL パラメーター用であると仮定します。
[ModelBinder(typeof(MyTypeModelBinder))]
public class MyType<T>
{
//...
}
と
public class MyTypeModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
// check if type if valid? Something like this...
if (!(bindingContext.ModelType.IsGenericType && bindingContext.ModelType.GetGenericTypeDefinition() == typeof(MyType<>)))
{
return false;
}
// create instance...using Activator?
}
}