使用Property属性调用方法/执行代码 [英] Call method/execute piece of code using Property attribute
本文介绍了使用Property属性调用方法/执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只是想知道是否可以使用property属性调用方法.基本上,我想在任何公共属性发生更改时设置实体的状态.
Just wondering if it's possible to call a method using property attribute. Basically, I want to set state of the entity when any of the public property changes.
就像我们有以下代码一样
Like we have following code
public class Contact : EntityBase
{
[NotifyChange]
public string FirstName { get; set; }
private void ChangeState()
{
EntityState = EntityState.Modified;
}
}
当我们打电话时
var c = new Contact();
c.FirstName = "John";
在设置FirstName的值之前(或之后),它将调用EntityState()函数.
before (or after) setting value of FirstName, it call EntityState() function.
有什么主意吗?
推荐答案
如果将属性重写为:
public class Contact : EntityBase
{
private string _firstName;
[NotifyChange]
public string FirstName
{
get
{
return _firstName;
}
set
{
ChangeState();
_firstName = value;
}
}
private void ChangeState()
{
EntityState = EntityState.Modified;
}
}
这篇关于使用Property属性调用方法/执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文