Skip to content

A Bug when DispatchProxy.Create #100188

Description

@YooRarely

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

  1. dotnet-policy-service commented on Mar 23, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-reflection
    See info in area-owners.md if you want to be subscribed.

  2. added this to the 9.0.0 milestone on Mar 27, 2024
  3. added and removed
    untriagedNew issue has not been triaged by the area owner
    on Mar 27, 2024
  4. modified the milestones: 9.0.0, 10.0.0 on Jul 26, 2024
  5. modified the milestones: 10.0.0, Future on Jul 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions