-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Fix: Incorrect order of checks for TypeBuilder GetConstructor
and GetField
method
#53645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
d115433
22cf90a
f2efe72
4940fc1
3472615
fd50ec9
6fcfbf5
f039dc9
863b984
f806283
70f45eb
b2c2b4a
98036aa
474a10b
23795a0
f7ed1ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1835,35 +1835,6 @@ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] nam | |
return generic_params; | ||
} | ||
|
||
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", | ||
Justification = "Linker thinks Type.GetConstructor(ConstructorInfo) is one of the public APIs because it doesn't analyze method signatures. We already have ConstructorInfo.")] | ||
public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor) | ||
{ | ||
/*FIXME I would expect the same checks of GetMethod here*/ | ||
if (type == null) | ||
throw new ArgumentException("Type is not generic", nameof(type)); | ||
|
||
if (!type.IsGenericType) | ||
throw new ArgumentException("Type is not a generic type", nameof(type)); | ||
|
||
if (type.IsGenericTypeDefinition) | ||
throw new ArgumentException("Type cannot be a generic type definition", nameof(type)); | ||
|
||
if (constructor == null) | ||
throw new NullReferenceException(); //MS raises this instead of an ArgumentNullException | ||
|
||
if (!constructor.DeclaringType!.IsGenericTypeDefinition) | ||
throw new ArgumentException("constructor declaring type is not a generic type definition", nameof(constructor)); | ||
if (constructor.DeclaringType != type.GetGenericTypeDefinition()) | ||
throw new ArgumentException("constructor declaring type is not the generic type definition of type", nameof(constructor)); | ||
|
||
ConstructorInfo res = type.GetConstructor(constructor); | ||
if (res == null) | ||
throw new ArgumentException("constructor not found"); | ||
|
||
return res; | ||
} | ||
|
||
private static bool IsValidGetMethodType(Type type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put this method back where it was originally declared? That will make reviewing the change much easier. Also when people look at source history, will be able to see what change was made here easily. #Closed |
||
{ | ||
if (type is TypeBuilder || type is TypeBuilderInstantiation) | ||
|
@@ -1885,6 +1856,34 @@ private static bool IsValidGetMethodType(Type type) | |
return false; | ||
} | ||
|
||
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", | ||
Justification = "Linker thinks Type.GetConstructor(ConstructorInfo) is one of the public APIs because it doesn't analyze method signatures. We already have ConstructorInfo.")] | ||
public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor) | ||
{ | ||
if (!IsValidGetMethodType(type)) | ||
throw new ArgumentException("type is not TypeBuilder but " + type.GetType(), nameof(type)); | ||
|
||
if (type == null) | ||
throw new ArgumentException("Type is not generic", nameof(type)); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs the same code that if (type is TypeBuilder && type.ContainsGenericParameters)
type = type.MakeGenericType(type.GetGenericArguments()); (and same for GetField) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried, but it won't build. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you also need
|
||
if (!type.IsGenericType) | ||
throw new ArgumentException("Type is not a generic type", nameof(type)); | ||
|
||
if (constructor.DeclaringType != type.GetGenericTypeDefinition()) | ||
throw new ArgumentException("constructor declaring type is not the generic type definition of type", nameof(constructor)); | ||
|
||
if (!constructor.DeclaringType!.IsGenericTypeDefinition) | ||
throw new ArgumentException("constructor declaring type is not a generic type definition", nameof(constructor)); | ||
if (constructor == null) | ||
throw new NullReferenceException(); //MS raises this instead of an ArgumentNullException | ||
|
||
ConstructorInfo res = type.GetConstructor(constructor); | ||
if (res == null) | ||
throw new ArgumentException("constructor not found"); | ||
|
||
return res; | ||
} | ||
|
||
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern", | ||
Justification = "Type.MakeGenericType is used to create a typical instantiation")] | ||
public static MethodInfo GetMethod(Type type, MethodInfo method) | ||
|
@@ -1914,18 +1913,18 @@ public static MethodInfo GetMethod(Type type, MethodInfo method) | |
|
||
public static FieldInfo GetField(Type type, FieldInfo field) | ||
{ | ||
if (!IsValidGetMethodType(type)) | ||
throw new ArgumentException("type is not TypeBuilder but " + type.GetType(), nameof(type)); | ||
|
||
if (!type.IsGenericType) | ||
throw new ArgumentException("Type is not a generic type", nameof(type)); | ||
|
||
if (type.IsGenericTypeDefinition) | ||
throw new ArgumentException("Type cannot be a generic type definition", nameof(type)); | ||
if (field.DeclaringType != type.GetGenericTypeDefinition()) | ||
throw new ArgumentException("field declaring type is not the generic type definition of type", nameof(field)); | ||
|
||
if (field is FieldOnTypeBuilderInst) | ||
throw new ArgumentException("The specified field must be declared on a generic type definition.", nameof(field)); | ||
|
||
if (field.DeclaringType != type.GetGenericTypeDefinition()) | ||
throw new ArgumentException("field declaring type is not the generic type definition of type", nameof(field)); | ||
|
||
FieldInfo res = type.GetField(field); | ||
if (res == null) | ||
throw new System.Exception("field not found"); | ||
|
Uh oh!
There was an error while loading. Please reload this page.