是否可以通过属性添加到类扩展方法?

时间:2013-05-14 23:33:53

标签: c# .net .net-4.0 attributes

假设我有一个简单的类树:

public class Operation {
    public int callback;
}

public class OperationResponse : Operation {
}

现在我想添加一个新的类OperationRequest,它有一个简单的扩展方法,如:

public static class Extensions
{
    public static T GetResponse<T>(this Operation source) where T : Operation {
        return new T{ callback = source.callback };
    } 
}

我很乐意用以下内容来定义它:

[Response(OperationResponse)]
public class OperationRequest {
}

并将其扩展名指定为GetResponse<OperationResponse>。 .net4中的属性是如此可能,以及如何做到这一点?

1 个答案:

答案 0 :(得分:0)

从技术上讲,你可以在运行时使用复杂的结构。但是扩展可以提供的设计时间智能感知是不可能的。 属性更多是运行时行为。

相关问题