Skip to main content
 首页 » 编程设计

c#之获取应用于通用方法的 PostSharp OnMethodBoundaryAspect 的通用参数

2024年02月20日13lori

给定以下设置的 PostSharp OnMethodBoundaryAspect,如何在 OnEntry 或 OnExit 期间获取 typeof(T1):

不使用反射的奖励积分。

看点:

public class MyOnMethodBoundaryAspect : OnMethodBoundaryAspect 
{ 
    public override void OnEntry(MethodExecutionArgs args) 
    {    
        //args.??? -- How to get typeof(T1) 
    } 
} 

方法:

[MyOnMethodBoundaryAspect] 
public void Foo<T1>() 
{ 
 
} 

请您参考如下方法:

我已经很久没有使用 PostSharp,但看起来您只需要使用 Method属性:

If the executed method is generic or if its declaring type is generic, the current property contains the generic instance being executed.

所以你应该可以使用MethodBase.GetGenericArguments获取类型参数。