inversionhourglass / Rougamo

Compile-time AOP component. Works with any method, whether it is async or sync, instance or static. Uses an aspectj-like pattern to match methods.
MIT License
393 stars 47 forks source link

特性不支持Type参数传入 #65

Closed kimdiego2098 closed 5 months ago

kimdiego2098 commented 6 months ago

提示System.NotSupportedException

编译报错堆栈:

1>MSBUILD : error : Fody: An unhandled exception occurred: 1>MSBUILD : error : Exception: 1>MSBUILD : error : Failed to execute weaver C:\Users\Diego.nuget\packages\rougamo.fody\2.3.0\build..\weaver\Rougamo.Fody.dll 1>MSBUILD : error : Type: 1>MSBUILD : error : System.Exception 1>MSBUILD : error : StackTrace: 1>MSBUILD : error : 在 InnerWeaver.ExecuteWeavers() 位置 C:\projects\fody\FodyIsolated\InnerWeaver.cs:行号 219 1>MSBUILD : error : 在 InnerWeaver.Execute() 位置 C:\projects\fody\FodyIsolated\InnerWeaver.cs:行号 111 1>MSBUILD : error : Source: 1>MSBUILD : error : FodyIsolated 1>MSBUILD : error : TargetSite: 1>MSBUILD : error : Void ExecuteWeavers() 1>MSBUILD : error : Parametertype: System.Type 1>MSBUILD : error : Type: 1>MSBUILD : error : System.NotSupportedException 1>MSBUILD : error : StackTrace: 1>MSBUILD : error : 在 Rougamo.Fody.ModuleWeaver.LoadValueOnStack(TypeReference parameterType, Object value) 1>MSBUILD : error : 在 Rougamo.Fody.ModuleWeaver.LoadAttributeArgumentIns(Collection1 arguments) 1>MSBUILD : error : 在 Rougamo.Fody.ModuleWeaver.InitMo(MethodDefinition methodDef, Mo mo, Boolean boxing) 1>MSBUILD : error : 在 Rougamo.Fody.ModuleWeaver.StateMachineInitMoFields(MethodDefinition methodDef, Mo[] mos, VariableDefinition stateMachineVariable, FieldReference[] moFields) 1>MSBUILD : error : 在 Rougamo.Fody.ModuleWeaver.StateMachineInitMos(RouMethod rouMethod, IStateMachineFields fields, IStateMachineVariables variables) 1>MSBUILD : error : 在 Rougamo.Fody.ModuleWeaver.AsyncTaskMethodWeave(RouMethod rouMethod) 1>MSBUILD : error : 在 Rougamo.Fody.ModuleWeaver.WeaveMos() 1>MSBUILD : error : 在 Rougamo.Fody.ModuleWeaver.Execute() 1>MSBUILD : error : 在 InnerWeaver.ExecuteWeavers() 位置 C:\projects\fody\FodyIsolated\InnerWeaver.cs:行号 196 1>MSBUILD : error : Source: 1>MSBUILD : error : Rougamo.Fody 1>MSBUILD : error : TargetSite: 1>MSBUILD : error : System.Collections.Generic.IList1[Mono.Cecil.Cil.Instruction] LoadValueOnStack(Mono.Cecil.TypeReference, System.Object) 1>MSBUILD : error :

inversionhourglass commented 6 months ago

可以提供一下相关代码吗

inversionhourglass commented 5 months ago

复现了,该问题需要满足下面几个条件才会出现:

  1. Type对应的类型为泛型类型
  2. 泛型类型中包含值类型,且该值类型不是内建类型也不是在当前程序集中定义
// 程序集1
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class TestAttribute : MoAttribute
{
    public TestAttribute(Type type) { }
}

public class Cls { }
public class Cls<T> { }
public struct St { }
public struct St<T> { }

// 程序集2
public struct StInAssembly { }
public struct StInAssembly<T> { }

public class C
{
    // 以下使用方式会报错
    [Test(typeof(Cls<St>))]               // 泛型参数为程序集1中自定义的值类型
    [Test(typeof(Cls<DateTime>))]         // 泛型参数为非内建值类型
    [Test(typeof(St<Cls>))]               // 泛型类型为程序集1中自定义的值类型

    // 以下使用方式不会报错
    [Test(typeof(Cls<StInAssembly>))]     // 泛型参数是值类型,但StInAssembly在当前程序集中定义
    [Test(typeof(Cls<int>))]              // 泛型参数是值类型,但是却是内建类型
    [Test(typeof(StInAssembly<Cls>))]     // 泛型类型是值类型,但StInAssembly在当前程序集中定义
    [Test(typeof(Cls<Cls>))]              // 存在泛型类型,但不包含值类型
    public void M() { }
}
inversionhourglass commented 5 months ago

已在3.0版本中修复