我有一个可以应用于类或属性的属性。这个类有两个构造函数。一个接受类型,一个接受类型和字符串。
[AttributeUsage(
AttributeTargets.Property |
AttributeTargets.Class,
AllowMultiple = true)]
class DcProxyAttribute : Attribute{
public DcProxyAttribute(Type type){ /*...*/ }
public DcProxyAttribute(Type type, string str){ /*...*/ }
}我可以像这样使用它
[DcProxy(typeof(SomeType)]
class MyClass {
[DcProxy(typeof(SomeType, "Hello World")]
public string SomeProperty { get; set;}
}我想把它限制在这种用法上。只有这样使用它才有意义。
以下用法现在被认为是有效的,但它们不应该是有效的。
[DcProxy(typeof(SomeType, "Hello world")]
class MyClass {
[DcProxy(typeof(SomeType)]
public string SomeProperty { get; set;}
}这可以做到吗?
我可以为它创建两个单独的类。一个DcProxyForClass和一个DcProxyForProperty。并像这样使用它
[DcProxyForClass(typeof(SomeType)]
class MyClass {
[DcProxyForProperty(typeof(SomeType, "Hello World")]
public string SomeProperty { get; set;}
}但这会降低代码的可读性,因为从语义上讲,它们做的也是一样的。
发布于 2019-02-07 18:33:58
不,基本上你不能这么做。
选项:
中标记它
https://stackoverflow.com/questions/54570611
复制相似问题