Last active
June 13, 2020 21:02
-
-
Save ankitvijay/18fbbe16bf1cd5c26244c409f50471a1 to your computer and use it in GitHub Desktop.
Part 3 Enumeration classes - EnumerationQueryParameterModelBinderProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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