-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
InvalidOperationException when accessing property from dynamically generated class-type in Blazor WASM .NET 8 #106469
Labels
Milestone
Comments
dotnet-policy-service
bot
added
the
untriaged
New issue has not been triaged by the area owner
label
Aug 15, 2024
StefH
changed the title
InvalidOperationException when accessing field or properties from dynamically generated class-type in Blazor WASM .NET 8
InvalidOperationException when accessing property from dynamically generated class-type in Blazor WASM .NET 8
Aug 15, 2024
Tagging subscribers to this area: @cston |
@cston This code works:// Define a dynamic assembly and module
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");
// Define a public class named "DynamicType"
TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType", TypeAttributes.Public);
// Define a public field of type string named "MyField"
typeBuilder.DefineField("MyField", typeof(string), FieldAttributes.Public);
// Create the type
Type dynamicType = typeBuilder.CreateType();
// Create an instance of the dynamic type
dynamic dynamicObject = Activator.CreateInstance(dynamicType)!;
// Set the value of the field using reflection
FieldInfo fieldInfo = dynamicType.GetField("MyField", BindingFlags.Public | BindingFlags.Instance)!;
fieldInfo.SetValue(dynamicObject, "Hello, World!");
// Output the value
Console.WriteLine("Test Field Value: " + (string)fieldInfo.GetValue(dynamicObject));
Console.WriteLine("Test Field Value Dynamic: " + dynamicObject.MyField); This code does
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
When a type is dynamically created using System.Reflection.Emit code, accessing a field or property throws InvalidOperationException.
Reproduction Steps
Create a .NET 8 WASM Blazor project.
Include System.Linq.Dynamic.Core
Add this code to the default Counter.cs code which creates dynamically a type and a instance of that type:
Now when casting this to
dynamic
and accessing the "Name" property throws exception:Not that accessing the properties using reflection works fine:
See this issue + fully working example project to show this error:
zzzprojects/System.Linq.Dynamic.Core#836
Expected behavior
The "Name" property should be accessible.
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8
Blazor WAM
Other information
No response
The text was updated successfully, but these errors were encountered: