我不知道我是否在问不可能的事情,我试图看看我可以通过谷歌找到什么,但也许我没有用正确的搜索词表达我正在寻找的东西。如果有的话请原谅我!
我希望在执行属性的 set 方法时使用自定义属性来执行附加代码。
这是一些伪代码:
public class MyAttribute : System.Attribute{
AttributedProperty.Get(Property p){
Console.WriteLine("The value of the Property '{0}' has just been retrieved!", p.Name);
}
AttributedProperty.Set(Property p){
Console.WriteLine("The value of the Property '{0}' has just been set!", p.Name);
}
}
public class MyClass{
private string _someProperty;
[MyAttribute]
public string SomeProperty{
get{ return _someProperty;}
set{ someProperty = value;}
}
}
很少有像伪代码这样简单的事情,所以我准备做一些工作,如果可以的话,我只需要一些指导:)
谢谢!
最佳答案
属性
不适合这种方式,它们只是添加关于您的成员的元信息,您可以通过Reflection
读取它们。
相反,您可以使用诸如 CaSTLe.Proxy
之类的东西来对您的方法和属性进行拦截器。
关于c# - 使用 Attributes 为属性的 set/get 方法添加装饰代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23075492/