只能在具有另一个属性的 class 中的方法上的 C# 属性
C# attribute that can only be on a method within a class with another attribute
在 C# 中,是否可以对属性进行限制,使其只能用于具有另一个属性的 class 中的方法?
[MyClassAttribute]
class Foo
{
[MyMethodAttribute]
public string Bar()
}
其中 "MyMethodAttribute" 只能在具有 "MyClassAttribute".
的 class 内
这可能吗?如果可以,怎么办?
您不能对用户定义的属性执行此操作。但我相信编译器有这样的机制,内置的FieldOffsetAttribute
使用这个。
struct MyStruct
{
[FieldOffset(1)] //compile error, StructLayoutAttribute is required
private int _num;
}
编辑 我认为如果你注入构建过程是可行的,使用类似 PostSharp.
如果您要尝试对您的方法属性进行 运行 时间验证,您可以这样做:
public abstract class ValidatableMethodAttribute : Attribute
{
public abstract bool IsValid();
}
public class MyMethodAtt : ValidatableMethodAttribute
{
private readonly Type _type;
public override bool IsValid()
{
// Validate your class attribute type here
return _type == typeof (MyMethodAtt);
}
public MyMethodAtt(Type type)
{
_type = type;
}
}
[MyClassAtt]
public class Mine
{
// This is the downside in my opinion,
// must give compile-time type of containing class here.
[MyMethodAtt(typeof(MyClassAtt))]
public void MethodOne()
{
}
}
然后使用反射找到系统中所有的ValidatableMethodAttributes
,并对其调用IsValid()
。这不是很可靠并且相当脆弱,但是这种类型的验证可以实现您正在寻找的东西。
或者传递 class 的类型( Mine
),然后在 IsValid()
中使用反射查找 Mine
类型上的所有属性。
您或许可以使用 PostSharp 做到这一点:(see: Compile time Validation in this tutorial)
然后在您的属性中,将使用类似于以下的代码检查父 class:
public class MyCustomAttribute : Attribute
{
public MyCustomAttribute()
{
if (GetType().CustomAttributes.Count(attr => attr.AttributeType == typeof (MyCustomClassAttribute)) < 1)
{
throw new Exception("Needs parent attribute") //Insert Postsharp method of raising compile time error here
}
在 C# 中,是否可以对属性进行限制,使其只能用于具有另一个属性的 class 中的方法?
[MyClassAttribute]
class Foo
{
[MyMethodAttribute]
public string Bar()
}
其中 "MyMethodAttribute" 只能在具有 "MyClassAttribute".
的 class 内这可能吗?如果可以,怎么办?
您不能对用户定义的属性执行此操作。但我相信编译器有这样的机制,内置的FieldOffsetAttribute
使用这个。
struct MyStruct
{
[FieldOffset(1)] //compile error, StructLayoutAttribute is required
private int _num;
}
编辑 我认为如果你注入构建过程是可行的,使用类似 PostSharp.
如果您要尝试对您的方法属性进行 运行 时间验证,您可以这样做:
public abstract class ValidatableMethodAttribute : Attribute
{
public abstract bool IsValid();
}
public class MyMethodAtt : ValidatableMethodAttribute
{
private readonly Type _type;
public override bool IsValid()
{
// Validate your class attribute type here
return _type == typeof (MyMethodAtt);
}
public MyMethodAtt(Type type)
{
_type = type;
}
}
[MyClassAtt]
public class Mine
{
// This is the downside in my opinion,
// must give compile-time type of containing class here.
[MyMethodAtt(typeof(MyClassAtt))]
public void MethodOne()
{
}
}
然后使用反射找到系统中所有的ValidatableMethodAttributes
,并对其调用IsValid()
。这不是很可靠并且相当脆弱,但是这种类型的验证可以实现您正在寻找的东西。
或者传递 class 的类型( Mine
),然后在 IsValid()
中使用反射查找 Mine
类型上的所有属性。
您或许可以使用 PostSharp 做到这一点:(see: Compile time Validation in this tutorial)
然后在您的属性中,将使用类似于以下的代码检查父 class:
public class MyCustomAttribute : Attribute
{
public MyCustomAttribute()
{
if (GetType().CustomAttributes.Count(attr => attr.AttributeType == typeof (MyCustomClassAttribute)) < 1)
{
throw new Exception("Needs parent attribute") //Insert Postsharp method of raising compile time error here
}