If you've got T
, you can create Nullable<T>
using Type.MakeGenericType
:
ModelBinders.Binders.Add(t, new CommandModelBinder(t));
var n = typeof(Nullable<>).MakeGenericType(t);
ModelBinders.Binders.Add(n, new CommandModelBinder(n));
I don't know how your CommandModelBinder
works and what the appropriate constructor argument is, you may need
ModelBinders.Binders.Add(n, new CommandModelBinder(t));
instead.
Note: MakeGenericType
will throw an exception if called with the wrong type. I haven't added error checking, since you're already filtering to only get the types for which this makes sense. Keep this in mind if you change your filtering though.