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

ReadOperand throws exception when reading type reference to an open generic #25

Open
llebron opened this issue Oct 15, 2024 · 0 comments

Comments

@llebron
Copy link

llebron commented Oct 15, 2024

This example throws an exception reliably.

        class TestGeneric<T> { }

        class OpenGenericRef
        {
            public void Foo()
            {
                System.Type type = typeof(TestGeneric<>);
            }
        }

        class ReflectionRunner
        {
            public static void RunReflection()
            {
                MethodInfo methodInfo = typeof(OpenGenericRef).GetMethod("Foo");
                try
                {
                    List<Instruction> instructions = MethodBodyReader.GetInstructions(methodInfo);
                }
                catch (Exception e)
                {
                    string exceptionMessage = e.Message;
                    string exceptionStackTrace = e.StackTrace;
                }
            }
        }

Exception message:
VAR 0 (T) cannot be expanded in this context with 0 instantiations

Exception stack trace:

"  at (wrapper managed-to-native) System.Reflection.RuntimeModule.ResolveMemberToken(intptr,int,intptr[],intptr[],System.Reflection.ResolveTokenError&)  
at System.Reflection.RuntimeModule.ResolveMember (System.Reflection.Module module, System.IntPtr monoModule, System.Int32 metadataToken, System.Type[] genericTypeArguments, System.Type[] genericMethodArguments) [0x0000f] in <dcf6bec1c5764d29a37432d4b047f650>:0 \n  
at System.Reflection.RuntimeModule.ResolveMember (System.Int32 metadataToken, System.Type[] genericTypeArguments, System.Type[] genericMethodArguments) [0x00000] in <dcf6bec1c5764d29a37432d4b047f650>:0 \n  at
Mono.Reflection.MethodBodyReader.ReadOperand (Mono.Reflection.Instruction instruction) [0x00216] in Mono.Reflection/MethodBodyReader.cs:194 \n  at Mono.Reflection.MethodBodyReader.ReadInstructions (System.Int32 metadataToken) [0x00021]

I have traced it to this line, in MethodBodyReader, as the instruction's operand type is InlineTok:

				case OperandType.InlineTok:
				case OperandType.InlineType:
				case OperandType.InlineMethod:
				case OperandType.InlineField:
					instruction.Operand = module.ResolveMember(il.ReadInt32(), type_arguments, method_arguments);
					break;
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

No branches or pull requests

1 participant