【问题标题】:Get the generic arguments of a PostSharp OnMethodBoundaryAspect applied to Generic Method获取应用于通用方法的 PostSharp OnMethodBoundaryAspect 的通用参数
【发布时间】:2013-02-08 08:13:07
【问题描述】:

鉴于以下设置的 PostSharp OnMethodBoundaryAspect,我如何在 OnEntryOnExit 期间获得 typeof(T1)

不使用反射的奖励积分。

方面:

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

方法:

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

}

【问题讨论】:

    标签: c# postsharp


    【解决方案1】:

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

    如果执行的方法是泛型的或其声明类型是泛型的,则当前属性包含正在执行的泛型实例。

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

    【讨论】:

    • 这是正确的。 args.Method 将包含调用方法的正确泛型实例。
    • 谢谢。我刚刚改用 Pro 许可证,Method 为 null 并且没有填充,因为我还没有在我的代码中引用它。就像这里,我刚刚发现的:stackoverflow.com/questions/6559589/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多