是否有可能用重复参数抽象出一个密封的属性?

时间:2018-03-26 18:29:10

标签: c# attributes

在我的代码库中,有一个属性在提供相同参数时多次出现。

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

我想简化此属性为......

[User32Attribute] // Or perhaps a better name

我尝试使用继承来实现这一目标,但由于DllImportAttribute已密封,因此无法实现。

以下是我的尝试:

using System.Runtime.InteropServices;

class User32Attribute : DllImportAttribute {
    public User32Attribute() : base("user32.dll", CharSet = CharSet.Auto, SetLastError = true) {}
}

当然,这失败了:

error CS0509: `User32Attribute': cannot derive from sealed type `System.Runtime.InteropServices.DllImportAttribute'

我能做些什么来抽象出这个属性的重复性吗?

1 个答案:

答案 0 :(得分:1)

回答你的问题,不,不是没有创建额外的预处理工具,这会破坏“更简单”的整个目的。

相关问题