smdn / Smdn.Reflection.ReverseGenerating

.NET libraries and tools to reverse-generate C# code from type and member information using reflection
https://smdn.jp/works/libs/Smdn.Reflection.ReverseGenerating/
MIT License
1 stars 0 forks source link

Generator.GenerateXxx fails with TypeLoadException (does not have an implementation) #31

Open smdn opened 1 year ago

smdn commented 1 year ago

Attempting to access MethodInfo of a method that has "no implementation" causes a TypeLoadException, and Generator.GenerateXxx fails.

At least, a usage of System.Text.Json.Utf8JsonWriter.DisposeAsync in the .NET Standard 2.1 assembly raises such an exception.

Reproduction steps

Build a .NET Standard 2.1 assembly with the configuration like below:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>netstandard2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Text.Json" Version="6.0.*" />
  </ItemGroup>
</Project>
public interface ITest {
  void Test(System.Text.Json.Utf8JsonWriter writer);
}

Run list-api with the assembly built above emits the outputs like below:

$ list-api ./bin/Debug/netstandard2.1/lib.dll 
fail: Smdn.Reflection.ReverseGenerating.ListApi[1] generator error on member 'ITest.Test' System.TypeLoadException: Method 'DisposeAsync' in type 'System.Text.Json.Utf8JsonWriter' from assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' does not have an implementation.    at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)    at System.Reflection.RuntimeMethodInfo.<get_Signature>g__LazyCreateSignature|24_0()    at System.Reflection.RuntimeMethodInfo.FetchReturnParameter()    at System.Reflection.RuntimeMethodInfo.get_ReturnParameter()    at Smdn.Reflection.ReverseGenerating.Generator.GenerateMethodOrDelegateDeclaration(MethodBase m, Boolean asDelegateDeclaration, ISet`1 referencingNamespaces, GeneratorOptions options) in /_/src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs:line 701    at Smdn.Reflection.ReverseGenerating.Generator.GenerateMethodBaseDeclaration(MethodBase m, ISet`1 referencingNamespaces, GeneratorOptions options) in /_/src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs:line 657    at Smdn.Reflection.ReverseGenerating.Generator.GenerateMemberDeclaration(MemberInfo member, ISet`1 referencingNamespaces, GeneratorOptions options) in /_/src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs:line 274    at Smdn.Reflection.ReverseGenerating.ListApi.ApiListWriter.GenerateTypeContentDeclarations(Int32 nestLevel, Assembly assm, Type t, ISet`1 referencingNamespaces, ApiListWriterOptions options, ILogger logger) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs:line 506
Smdn.Reflection.ReverseGenerating.ListApi.ApiListWriter+MemberDeclarationException: generator error on member 'ITest.Test'
 ---> System.TypeLoadException: Method 'DisposeAsync' in type 'System.Text.Json.Utf8JsonWriter' from assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' does not have an implementation.
   at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeMethodInfo.<get_Signature>g__LazyCreateSignature|24_0()
   at System.Reflection.RuntimeMethodInfo.FetchReturnParameter()
   at System.Reflection.RuntimeMethodInfo.get_ReturnParameter()
   at Smdn.Reflection.ReverseGenerating.Generator.GenerateMethodOrDelegateDeclaration(MethodBase m, Boolean asDelegateDeclaration, ISet`1 referencingNamespaces, GeneratorOptions options) in /_/src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs:line 701
   at Smdn.Reflection.ReverseGenerating.Generator.GenerateMethodBaseDeclaration(MethodBase m, ISet`1 referencingNamespaces, GeneratorOptions options) in /_/src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs:line 657
   at Smdn.Reflection.ReverseGenerating.Generator.GenerateMemberDeclaration(MemberInfo member, ISet`1 referencingNamespaces, GeneratorOptions options) in /_/src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs:line 274
   at Smdn.Reflection.ReverseGenerating.ListApi.ApiListWriter.GenerateTypeContentDeclarations(Int32 nestLevel, Assembly assm, Type t, ISet`1 referencingNamespaces, ApiListWriterOptions options, ILogger logger) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs:line 506
   --- End of inner exception stack trace ---
   at Smdn.Reflection.ReverseGenerating.ListApi.ApiListWriter.GenerateTypeContentDeclarations(Int32 nestLevel, Assembly assm, Type t, ISet`1 referencingNamespaces, ApiListWriterOptions options, ILogger logger) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs:line 512
   at Smdn.Reflection.ReverseGenerating.ListApi.ApiListWriter.GenerateTypeAndMemberDeclarations(Int32 nestLevel, Assembly assm, Type t, ISet`1 referencingNamespaces, ApiListWriterOptions options, Boolean enableNullableAnnotationsOnlyOnTypes, Boolean enableNullableAnnotationsOnlyOnMembers, ILogger logger) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs:line 416
   at Smdn.Reflection.ReverseGenerating.ListApi.ApiListWriter.GenerateTypeAndMemberDeclarations(Int32 nestLevel, Assembly assm, IEnumerable`1 types, ISet`1 referencingNamespaces, ApiListWriterOptions options, ILogger logger) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs:line 316
   at Smdn.Reflection.ReverseGenerating.ListApi.ApiListWriter.WriteExportedTypes() in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs:line 203
   at Smdn.Reflection.ReverseGenerating.ListApi.RootCommandImplementation.<>c.<CommandMain>b__20_0(Assembly assm, ValueTuple`4 arg) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi/RootCommandImplementation.cs:line 223
   at Smdn.Reflection.ReverseGenerating.ListApi.AssemblyLoader.UsingAssembly[TArg,TResult](AssemblySource assemblySource, TArg arg, Func`3 actionWithLoadedAssembly, WeakReference& context, ILogger logger) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyLoader.netcore.cs:line 163
   at Smdn.Reflection.ReverseGenerating.ListApi.AssemblyLoader.UsingAssemblyCore[TArg,TResult](AssemblySource assemblySource, Boolean loadIntoReflectionOnlyContext, TArg arg, Func`3 actionWithLoadedAssembly, WeakReference& context, ILogger logger) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyLoader.netcore.cs:line 47
   at Smdn.Reflection.ReverseGenerating.ListApi.AssemblyLoader.UsingAssembly[TArg,TResult](FileInfo assemblyFile, Boolean loadIntoReflectionOnlyContext, TArg arg, Func`3 actionWithLoadedAssembly, WeakReference& context, ILogger logger) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyLoader.cs:line 38
   at Smdn.Reflection.ReverseGenerating.ListApi.RootCommandImplementation.CommandMain(ParseResult parseResult, IConsole console) in /_/src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi/RootCommandImplementation.cs:line 186

List of similar exceptions

 ---> System.TypeLoadException: Method 'DisposeAsync' in type 'System.Text.Json.Utf8JsonWriter' from assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' does not have an implementation.
   at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeMethodInfo.<get_Signature>g__LazyCreateSignature|24_0()
   at System.Reflection.RuntimeMethodInfo.FetchReturnParameter()
   at System.Reflection.RuntimeMethodInfo.get_ReturnParameter()
 ---> System.TypeLoadException: Method 'DisposeAsync' in type 'System.Text.Json.Utf8JsonWriter' from assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' does not have an implementation.
   at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeMethodInfo.<get_Signature>g__LazyCreateSignature|24_0()
   at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
   at System.Reflection.RuntimeMethodInfo.GetParameters()
 ---> System.TypeLoadException: Method 'DisposeAsync' in type 'System.Text.Json.Utf8JsonWriter' from assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' does not have an implementation.
   at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeMethodInfo.<get_Signature>g__LazyCreateSignature|24_0()
   at System.Reflection.RuntimeMethodInfo.get_ReturnType()
   at Smdn.Reflection.MethodInfoExtensions.TryGetEventFromAccessorMethod(MethodInfo accessor, Boolean expectAdd, Boolean expectRemove, EventInfo& ev) in /_/src/Smdn.Fundamental.Reflection/Smdn.Reflection/MethodInfoExtensions.cs:line 144
   at Smdn.Reflection.MethodInfoExtensions.IsEventAccessorMethod(MethodInfo m) in /_/src/Smdn.Fundamental.Reflection/Smdn.Reflection/MethodInfoExtensions.cs:line 113
 ---> System.TypeLoadException: Method 'DisposeAsync' in type 'System.Text.Json.Utf8JsonWriter' from assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' does not have an implementation.
   at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeMethodInfo.<get_Signature>g__LazyCreateSignature|24_0()
   at System.Reflection.RuntimeMethodInfo.get_ReturnType()
   at Smdn.Reflection.MethodInfoExtensions.TryGetEventFromAccessorMethod(MethodInfo accessor, Boolean expectAdd, Boolean expectRemove, EventInfo& ev) in /_/src/Smdn.Fundamental.Reflection/Smdn.Reflection/MethodInfoExtensions.cs:line 144
   at Smdn.Reflection.MethodInfoExtensions.TryGetEventFromAccessorMethod(MethodInfo accessor, EventInfo& ev) in /_/src/Smdn.Fundamental.Reflection/Smdn.Reflection/MethodInfoExtensions.cs:line 127
   at Smdn.Reflection.MemberInfoExtensions.IsHidingInheritedMember(MemberInfo member, Boolean nonPublic) in /_/src/Smdn.Fundamental.Reflection/Smdn.Reflection/MemberInfoExtensions.cs:line 108