Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Part 3 Enumeration classes - EnumerationQueryParameterModelBinderProvider
public class EnumerationQueryStringModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var fullyQualifiedAssemblyName = context.Metadata.ModelType.FullName;
if (fullyQualifiedAssemblyName == null)
{
return null;
}
var enumType = context.Metadata.ModelType.Assembly.GetType
(fullyQualifiedAssemblyName, false);
if (enumType == null || !enumType.IsSubclassOf(typeof(Enumeration)))
{
return null;
}
var methodInfo = typeof(EnumerationQueryStringModelBinder)
.GetMethod("CreateInstance"
, BindingFlags.Static | BindingFlags.Public);
if (methodInfo == null)
{
throw new InvalidOperationException("Invalid operation");
}
var genericMethod = methodInfo.MakeGenericMethod(enumType);
var invoke = genericMethod.Invoke(null, null);
return invoke as IModelBinder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.