Repository navigation
Closed
Milestone
Description
Description
When using DispatchProxy for an interface which have an init property, it will throw TypeLoadException.
Reproduction Steps
- base
class ClassTestI : DispatchProxy
{
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
{
throw new NotImplementedException();
}
}
[Test]
public void Test(){
DispatchProxy.Create<TestI, ClassTestI>();
}
- success
interface TestI
{
public int MyProperty { get; set; }
}
- error
interface TestI
{
public int MyProperty { get; init }
}
Expected behavior
It Should success to create an object which implement the TestI interface
Actual behavior
System.TypeLoadException : Signature of the body and declaration in a method implementation do not match. Type: 'generatedProxy_1'. Assembly: 'ProxyBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Reflection.Emit.RuntimeTypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.RuntimeTypeBuilder.CreateTypeInfoImpl()
at System.Reflection.Emit.TypeBuilder.CreateType()
at System.Reflection.DispatchProxyGenerator.ProxyBuilder.CreateType()
at System.Reflection.DispatchProxyGenerator.ProxyAssembly.GenerateProxyType(Type baseType, Type interfaceType, String interfaceParameter, String proxyParameter)
at System.Reflection.DispatchProxyGenerator.ProxyAssembly.GetProxyType(Type baseType, Type interfaceType, String interfaceParameter, String proxyParameter)
at System.Reflection.DispatchProxyGenerator.CreateProxyInstance(Type baseType, Type interfaceType, String interfaceParameter, String proxyParameter)
at System.Reflection.DispatchProxy.Create[T,TProxy]()
Regression?
No response
Known Workarounds
Change the Property from {get; init;} to {get; set;} in the interface
Configuration
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Windows11
Other information
No response
Activity
dotnet-policy-service commentedon Mar 23, 2024
Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
Copy custom modifiers in `DispatchProxy` overrides (dotnet#135567)