Skip to content
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

I already know how UnknownDeclaringType happen #552

Open
CreateAndInject opened this issue Mar 26, 2024 · 1 comment
Open

I already know how UnknownDeclaringType happen #552

CreateAndInject opened this issue Mar 26, 2024 · 1 comment

Comments

@CreateAndInject
Copy link
Contributor

CreateAndInject commented Mar 26, 2024

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

@wtfsck
Copy link
Contributor

wtfsck commented Mar 28, 2024

Renaming it would be a breaking change, not worth it. You can send a PR if you want to update the comment though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants