-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Reflection.Emit value type mismatch trying to create a value type #103796
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection |
Tagging subscribers to this area: @dotnet/area-system-reflection-emit |
The problem is likely caused by wrong IsValueType value here: runtime/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/SignatureHelper.cs Line 293 in f06bc4d
cc @buyaa-n |
Here's a full example. It only happens when using the MetadataLoadContext's CoreAssembly. When I remove that part it works fine. using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Loader;
var resolver =
new PathAssemblyResolver(Directory.GetFiles("C:\\Program Files\\dotnet\\sdk\\9.0.100-preview.5.24307.3\\ref",
"*.dll"));
using var context = new MetadataLoadContext(resolver);
var coreAssembly = context.CoreAssembly!;
var valueTypeType = coreAssembly.GetType("System.ValueType")!;
var ab = new PersistedAssemblyBuilder(new AssemblyName("MyAssembly"), coreAssembly);
var mob = ab.DefineDynamicModule("MyModule");
var valueTb = mob.DefineType("MyValueType", 0, valueTypeType);
valueTb.CreateType();
var tb = mob.DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class);
var meb = tb.DefineMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Static);
var il = meb.GetILGenerator();
il.BeginScope();
il.DeclareLocal(valueTb);
il.EndScope();
il.Emit(OpCodes.Ret);
tb.CreateType();
using var stream = new MemoryStream();
ab.Save(stream);
stream.Seek(0, SeekOrigin.Begin);
var assembly = AssemblyLoadContext.Default.LoadFromStream(stream);
var method = assembly.GetType("MyType")!.GetMethod("MyMethod");
method!.Invoke(null, null); |
My local hack/fix for this was to add |
Description
I'm trying to create a value type using the .NET 9 System.Reflection.Emit API. I wasn't able to find an example, so I tried to just define a type with a parent type of System.ValueType. Unfortunately, I get the following error when I run an assembly that uses the type. My guess is that there is a different, intended way to define a value type.
Unhandled Exception: System.TypeLoadException: Could not load type 'myValueType' from assembly 'MyAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' due to value type mismatch.
Reproduction Steps
Define and create a type extending System.ValueType, then use it by declaring a local.
Expected behavior
Either extending from System.ValueType works, or there is some other way to define a value type using System.Reflection.Emit.
Actual behavior
Unhandled Exception: System.TypeLoadException: Could not load type 'myValueType' from assembly 'MyAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' due to value type mismatch.
Regression?
No response
Known Workarounds
No response
Configuration
Using .NET 9.0.100-preview.5.24307.3 and also using the reference assemblies from that version of .NET for my MetaDataLoadContext resolver.
Other information
No response
The text was updated successfully, but these errors were encountered: