Is there an abbreviation in C # to force the setter to additionally set the dirty flag

Currently, a dirty property is also set to create the setter . I need to do something like this:

private bool _isDirty;
private int _lives;
public int Lives{
    get { return _lives; }
    set {
        if (_lives != value){
            _lives = value;
            _isDirty = true;
        }
    }
}

This is not a huge pain to write, but it is a very vertically spacious and repeating piece of code for writing, if I use quite a lot of this template in my project.

Is there an abbreviated or alternative, shorter syntax for this in C #?

What I'm specifically trying to accomplish is that some variable changes should trigger a dirty flag, which, at the stage of code visualization, can be used to update rendering properties. p>

+4
3

, .

class DirtyClass
{
  protected bool IsDirty { get; set;}

  protected void ChangeProperty<T>(ref T backing, T Value)
  { 
      if(!backing.Equals(value))
      {
           backing = value;
           IsDirty = true;
      }
  }
}

class LivesCounter : DirtyClass
{
   private int _lives;
   public int Lives  
   {
      get { return _lives; }
      set { ChangeProperty(ref _lives, value); }
   }
}

.

jdl134679, INotifyPropertyChanged.

+7

, . info: https://msdn.microsoft.com/en-us/library/ms165396.aspx

: https://msdn.microsoft.com/en-us/library/ms165394.aspx

snippet 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.

+1

No - you can create your own structure to encapsulate this behavior, i.e. The general structure containing the value, the IsDirty property, and the way the flag is cleared.

0
source

Source: https://habr.com/ru/post/1658288/


All Articles