C# 使用属性生成自定义setter
在使用对象数据库持久化其实例的类中,我必须继续执行以下操作:C# 使用属性生成自定义setter,c#,.net,attributes,boo,automatic-properties,C#,.net,Attributes,Boo,Automatic Properties,在使用对象数据库持久化其实例的类中,我必须继续执行以下操作: private string _name; public string Name { get { return this._name; } set { _name = value; this.Save(); } } 而我更愿意键入以下内容: [PersistedProperty(Name)] private string _name; 其中PersistedProperty属性生成一个Getter和
private string _name;
public string Name
{
get { return this._name; }
set { _name = value; this.Save(); }
}
而我更愿意键入以下内容:
[PersistedProperty(Name)]
private string _name;
其中PersistedProperty属性生成一个Getter和Setter,就像默认的[Property()]属性一样,只是我想向生成的Setter添加一行代码
有没有一种方法可以创建一个这样做的属性?希望它能与Intellisense一起工作
默认的[Property()]属性是如何实现其功能的?如果我看到代码,我可以移植它
注意:我实际上是在Boo中这样做的,但是我想我会给出c代码,因为更多的人可能会愿意回答这个问题,但是,如果有一个特定于Boo的解决方案,我洗耳恭听
更新:
我的目标只是减少打字和杂乱无章。事实证明,实现这一点的最简单方法是使用一个脚本,该脚本基于我的类中的标记生成分部类
从标记自动生成源代码(与分部类一起)很容易,而且实际上看起来是一种非常有前途的方法,可以解决我们通常试图用继承和泛型类型解决的一些问题。这需要。虽然.NET中不直接支持,但可以通过第三方工具(例如)来完成
然而,要使intellisense工作,这必须在库中完成,因为(最终)编译的代码将展开到完整的属性getter/setter中。使用属性IMO不容易实现。 也许您可以使用另一种方法,例如扩展方法:
// Extension method that allows updating a property
// and calling .Save() in a single line of code.
public static class ISaveableExtensions
{
public static void UpdateAndSave<T>(
this ISaveable instance,
Expression<Func<T>> propertyExpression, T newValue)
{
// Gets the property name
string propertyName = ((MemberExpression)propertyExpression.Body).Member.Name;
// Updates its value
PropertyInfo prop = instance.GetType().GetProperty(propertyName);
prop.SetValue(instance, newValue, null);
// Now call Save
instance.Save();
}
}
...
// Some interface that implements the Save method
public interface ISaveable
{
void Save();
}
...
// Test class
public class Foo : ISaveable
{
public string Property { get; set; }
public void Save()
{
// Some stuff here
Console.WriteLine("Saving");
}
public override string ToString()
{
return this.Property;
}
}
...
public class Program
{
private static void Main(string[] args)
{
Foo d = new Foo();
// Updates the property with a new value, and automatically call Save
d.UpdateAndSave(() => d.Property, "newValue");
Console.WriteLine(d);
Console.ReadKey();
}
}
//允许更新属性的扩展方法
//并在一行代码中调用.Save()。
公共静态类IsAvableExtensions
{
公共静态void UpdateAndSave(
这是一个不可否认的例子,
表达式属性Expression,T newValue)
{
//获取属性名
字符串propertyName=((MemberExpression)propertyExpression.Body).Member.Name;
//更新其值
PropertyInfo prop=instance.GetType().GetProperty(propertyName);
prop.SetValue(实例,newValue,null);
//现在呼叫Save
Save();
}
}
...
//实现Save方法的某些接口
公共接口是可更换的
{
作废保存();
}
...
//测试班
公共类Foo:ISaveable
{
公共字符串属性{get;set;}
公共作废保存()
{
//这里有些东西
控制台写入线(“保存”);
}
公共重写字符串ToString()
{
归还此财产;
}
}
...
公共课程
{
私有静态void Main(字符串[]args)
{
food=新的Foo();
//使用新值更新属性,并自动调用Save
d、 UpdateAndSave(()=>d.属性,“newValue”);
控制台写入线(d);
Console.ReadKey();
}
}
它是类型安全的,对自动完成友好,但它需要的代码不止这些。
Save()
在所有setter中,所以不确定我是否会实际使用它…您想这样做的具体原因是什么?或者你只是想节省一点时间?什么是“default[Property()]属性”?@SamIam它是为了节省时间和减少混乱。我经常使用Excel或Regex find replace为字段创建属性。这需要我调用一个方法,然而,我希望它发生在setter中,因为实例将在DataGridView等中编辑。总结:代码: