Skip to content

I already know how UnknownDeclaringType happen #552

Open
@CreateAndInject

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.

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,
}

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));
}

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));
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions