在 C# 中是否有任何 shorthand 来使 setter 额外设置一个脏标志
Is there any shorthand in C# to make a setter additionally set a dirty flag
目前为了使 setter 也设置 dirty property 我必须做这样的事情:
private bool _isDirty;
private int _lives;
public int Lives{
get { return _lives; }
set {
if (_lives != value){
_lives = value;
_isDirty = true;
}
}
}
写起来不是很痛苦,但如果我在我的项目中使用相当多的这种模式,那么它是一段非常垂直和重复的代码。
在 C# 中是否有任何 shorthand 或更短的替代语法?
我特别想要完成的是某些变量的改变应该触发一个脏标志,在代码的渲染阶段可以用来刷新渲染对象。
不 - 您可以创建自己的结构来封装此行为,即。一个包含值的通用结构,一个 IsDirty 属性 和一种清除标志的方法。
创建一个 Class 来实现辅助方法。
class DirtyClass
{
protected bool IsDirty { get; set;}
protected void ChangeProperty<T>(ref T backing, T Value)
{
if(!backing.Equals(value))
{
backing = value;
IsDirty = true;
}
}
}
使用 setter
中的辅助方法
class LivesCounter : DirtyClass
{
private int _lives;
public int Lives
{
get { return _lives; }
set { ChangeProperty(ref _lives, value); }
}
}
处理空元素留作练习。
正如 jdl134679 所提到的,查看 INotifyPropertyChanged 接口。
如果您不喜欢 类 方式,您也可以创建一个片段。
信息:https://msdn.microsoft.com/en-us/library/ms165396.aspx
手动创建片段:https://msdn.microsoft.com/en-us/library/ms165394.aspx
或
安装片段设计器 https://visualstudiogallery.msdn.microsoft.com/B08B0375-139E-41D7-AF9B-FAEE50F68392
创建一个片段并将其命名为 propisd(属性 快捷方式)
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Properties with Dirty</Title>
<Author>Myself</Author>
<Description>Creates a property which ties to isDrty</Description>
<Shortcut>propisd</Shortcut>
</Header>
<Snippet>
<Code Language="C#">
<![CDATA[private $TYPE$ $PRIVATENAME$;
public $TYPE$ $PROPERTYNAME$
{
get { return $PRIVATENAME$; }
set
{
if ($PRIVATENAME$ != value)
{
$PRIVATENAME$ = value;
_isDirty = true;
}
}
}]]>
</Code>
<Declarations>
<Literal>
<ID>TYPE</ID>
<ToolTip>replace with the type</ToolTip>
<Default>"TYPE"</Default>
</Literal>
<Literal>
<ID>PRIVATENAME</ID>
<ToolTip>replace with the private name</ToolTip>
<Default>"PRIVATENAME"</Default>
</Literal>
<Literal>
<ID>PROPERTYNAME</ID>
<ToolTip>replace with the property name</ToolTip>
<Default>"PROPERTYNAME"</Default>
</Literal>
</Declarations>
</Snippet>
</CodeSnippet>
</CodeSnippets>
将此文件另存为 propisd.snippet 并通过代码段管理器添加它。
只要您键入 propisd 后跟 TAB,您就会看到该代码段。通过使用 Tab,您将完成替换。就像您键入 'prop' 然后执行 TAB。
目前为了使 setter 也设置 dirty property 我必须做这样的事情:
private bool _isDirty;
private int _lives;
public int Lives{
get { return _lives; }
set {
if (_lives != value){
_lives = value;
_isDirty = true;
}
}
}
写起来不是很痛苦,但如果我在我的项目中使用相当多的这种模式,那么它是一段非常垂直和重复的代码。
在 C# 中是否有任何 shorthand 或更短的替代语法?
我特别想要完成的是某些变量的改变应该触发一个脏标志,在代码的渲染阶段可以用来刷新渲染对象。
不 - 您可以创建自己的结构来封装此行为,即。一个包含值的通用结构,一个 IsDirty 属性 和一种清除标志的方法。
创建一个 Class 来实现辅助方法。
class DirtyClass
{
protected bool IsDirty { get; set;}
protected void ChangeProperty<T>(ref T backing, T Value)
{
if(!backing.Equals(value))
{
backing = value;
IsDirty = true;
}
}
}
使用 setter
中的辅助方法class LivesCounter : DirtyClass
{
private int _lives;
public int Lives
{
get { return _lives; }
set { ChangeProperty(ref _lives, value); }
}
}
处理空元素留作练习。
正如 jdl134679 所提到的,查看 INotifyPropertyChanged 接口。
如果您不喜欢 类 方式,您也可以创建一个片段。 信息:https://msdn.microsoft.com/en-us/library/ms165396.aspx
手动创建片段:https://msdn.microsoft.com/en-us/library/ms165394.aspx
或
安装片段设计器 https://visualstudiogallery.msdn.microsoft.com/B08B0375-139E-41D7-AF9B-FAEE50F68392
创建一个片段并将其命名为 propisd(属性 快捷方式)
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Properties with Dirty</Title>
<Author>Myself</Author>
<Description>Creates a property which ties to isDrty</Description>
<Shortcut>propisd</Shortcut>
</Header>
<Snippet>
<Code Language="C#">
<![CDATA[private $TYPE$ $PRIVATENAME$;
public $TYPE$ $PROPERTYNAME$
{
get { return $PRIVATENAME$; }
set
{
if ($PRIVATENAME$ != value)
{
$PRIVATENAME$ = value;
_isDirty = true;
}
}
}]]>
</Code>
<Declarations>
<Literal>
<ID>TYPE</ID>
<ToolTip>replace with the type</ToolTip>
<Default>"TYPE"</Default>
</Literal>
<Literal>
<ID>PRIVATENAME</ID>
<ToolTip>replace with the private name</ToolTip>
<Default>"PRIVATENAME"</Default>
</Literal>
<Literal>
<ID>PROPERTYNAME</ID>
<ToolTip>replace with the property name</ToolTip>
<Default>"PROPERTYNAME"</Default>
</Literal>
</Declarations>
</Snippet>
</CodeSnippet>
</CodeSnippets>
将此文件另存为 propisd.snippet 并通过代码段管理器添加它。
只要您键入 propisd 后跟 TAB,您就会看到该代码段。通过使用 Tab,您将完成替换。就像您键入 'prop' 然后执行 TAB。