-
Notifications
You must be signed in to change notification settings - Fork 607
Description
It happen when the developer call DynamicILInfo.GetTokenFor(RuntimeFieldHandle field) rather than DynamicILInfo.GetTokenFor(RuntimeFieldHandle field, RuntimeTypeHandle contextType) to build DynamicMethod, .NET doesn't crash without declaring type, it can work fine also.
Especially, .NET2.0 doesn't support a public overload with RuntimeTypeHandle parameter, that's why so many protectors don't pass in this parameter.
As the reason is clear, such name/comment could be updated if necessary.
dnlib/src/DotNet/Emit/DynamicMethodBodyReader.cs
Lines 16 to 28 in 0b2dc95
| public enum DynamicMethodBodyReaderOptions { | |
| /// <summary> | |
| /// No option is enabled | |
| /// </summary> | |
| None = 0, | |
| /// <summary> | |
| /// Some fields/methods have an unknown declaring type and don't have a context with | |
| /// that information. If this is enabled, the reader will try to guess it but it doesn't | |
| /// always work. If you get an <see cref="ArgumentException"/>, try enabling this option. | |
| /// </summary> | |
| UnknownDeclaringType = 0x00000001, | |
| } |
dnlib/src/DotNet/Emit/DynamicMethodBodyReader.cs
Lines 458 to 462 in 0b2dc95
| if ((options & DynamicMethodBodyReaderOptions.UnknownDeclaringType) != 0) { | |
| // Sometimes it's a generic type but obj != `GenericMethodInfo`, so pass in 'default' and the | |
| // runtime will try to figure out the declaring type. https://github.com/0xd4d/dnlib/issues/298 | |
| return importer.Import(SR.MethodBase.GetMethodFromHandle((RuntimeMethodHandle)obj, default)); | |
| } |
dnlib/src/DotNet/Emit/DynamicMethodBodyReader.cs
Lines 458 to 462 in 0b2dc95
| if ((options & DynamicMethodBodyReaderOptions.UnknownDeclaringType) != 0) { | |
| // Sometimes it's a generic type but obj != `GenericMethodInfo`, so pass in 'default' and the | |
| // runtime will try to figure out the declaring type. https://github.com/0xd4d/dnlib/issues/298 | |
| return importer.Import(SR.MethodBase.GetMethodFromHandle((RuntimeMethodHandle)obj, default)); | |
| } |