【发布时间】:2025-12-02 08:40:01
【问题描述】:
我不知道我是否在问不可能的事情,我试图通过 Google 来查看我能找到什么,但也许我没有用正确的搜索词表达我在寻找什么。如果它在那里,请原谅我!
我希望在执行属性的 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;}
}
}
很少有像伪代码这样简单的东西,所以我准备做一些工作,如果可以的话,我只需要一些指导:)
谢谢!
【问题讨论】:
标签: c# properties attributes