<p>I have a class with a bunch of properties that look like this:</p>

<pre><code>public string Name
{
    get { return _name; }
    set { IsDirty = true; _name = value; }
}
</code></pre>

<p>It would be a lot easier if I could rely on C# 3.0 to generate the backing store for these, but is there any way to factor out the IsDirty=true; so that I can write my properties something like this and still get the same behaviour:</p>

<pre><code>[MakesDirty]
public string Name { get; set; }
</code></pre>

[{'answer_id': 63564, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': '<p>You could try setting up a code snippet to make it easy to create those.</p>\n'}, {'answer_id': 63600, 'author': 'LepardUK', 'author_id': 44247, 'author_profile': 'https://Stackoverflow.com/users/44247', 'pm_score': 0, 'selected': False, 'text': '<p>The other alternative might be a code generator such as codesmith to automate creating the properties. This would be especially useful if the properties you are creating are columns in a database table</p>\n'}, {'answer_id': 63623, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>ContextBound object. If you create a class that extends context bound object and you create a ContextAttribute you can intercept the calls made to such a property and set the IsDirty. .NET will create a proxy to your class so all calls go over something like a remoting sink.</p>\n\n<p>The problem with such an approach though is that your proxy will only be invoked when called externally. I'll give you an example.</p>\n\n<pre><code>class A\n{\n    [Foo]\n    public int Property1{get; set;}\n    public int Property2{get {return variable;} set{ Property1 = value; variable = value; }\n}\n</code></pre>\n\n<p>When property1 is called from another class, your proxy would be invoked. But if another class calls property2, even though the set of property2 will call into property1 no proxy will be invoked, (a proxy isn't necessary when you're in the class itself).</p>\n\n<p>There is a lot of sample code out there of using ContextBoundObjects, look into it.</p>\n"}, {'answer_id': 63628, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 4, 'selected': True, 'text': '<p><strong>No. Not without writing considerably more (arcane?) code than the original version</strong> (You\'d have to use reflection to check for the attribute on the property and what not.. did I mention it being \'slower\').. This is the kind of duplication I can live with.</p>\n\n<p>MS has the same need for <a href="http://msdn.microsoft.com/en-us/library/ms229614.aspx" rel="nofollow noreferrer">raising events when a property is changed</a>. INotifyPropertyChanged that is a vital interface for change notifications. Every implementation I\'ve seen yet\ndoes</p>\n\n<pre><code>set\n{ \n  _name = value; \n  NotifyPropertyChanged("Name"); \n}\n</code></pre>\n\n<p>If it was possible, I\'d figure those smart guys at MS would already have something like that in place.. </p>\n'}, {'answer_id': 63647, 'author': 'JRoppert', 'author_id': 6777, 'author_profile': 'https://Stackoverflow.com/users/6777', 'pm_score': 0, 'selected': False, 'text': '<p>I can recommend to use <a href="http://www.codeplex.com/entlib" rel="nofollow noreferrer">Enterprise Library</a> for that purpose. Policy Application Block delivers the infrastructure to do "something" (something = you can code that on your own) whenever you enter/exit a method for example. You can control the behavior with attributes. Take that as a hint an go into detail with the documentation of enterprise library.</p>\n'}, {'answer_id': 63670, 'author': 'Rob', 'author_id': 7872, 'author_profile': 'https://Stackoverflow.com/users/7872', 'pm_score': 0, 'selected': False, 'text': '<p>There\'s a DefaultValueAttribute that can be assigned to a property, this is mainly used by the designer tools so they can indicate when a property has been changed, but, it might be a "tidy" way of describing what the default value for a property is, and thus being able to identify if it\'s changed.</p>\n\n<p>You\'d need to use Reflection to identify property changes - which isn\'t actually <em>that</em> expensive unless you\'re doing lots of it!</p>\n\n<p>Caveat: You wouldn\'t be able to tell if a property had been changed BACK from a non-default value to the default one.</p>\n'}, {'answer_id': 63677, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 2, 'selected': False, 'text': "<p>No, when you use automatic properties you don't have any control over the implementation. The best option is to use a templating tool, code snippets or create a private SetValue<code>&lt;T</code>>(ref T backingField, T value) which encapsulates the setter logic.</p>\n\n<pre><code>private void SetValue&lt;T&gt;(ref T backingField, T value)\n{\n   if (backingField != value)\n   {\n      backingField = value;\n      IsDirty = true;\n   }\n}\n\npublic string Name\n{\n   get\n   {\n      return _name;\n   }\n   set\n   {\n      SetValue(ref _name, value);\n   }\n}\n</code></pre>\n"}, {'answer_id': 63703, 'author': 'Thomas Lundström', 'author_id': 3985, 'author_profile': 'https://Stackoverflow.com/users/3985', 'pm_score': 0, 'selected': False, 'text': '<p>I\'d say that the best way of solving this is to use Aspect-Oriented Programming (AOP). Mats Helander did a <a href="http://www.infoq.com/articles/aspects-of-domain-model-mgmt" rel="nofollow noreferrer">write up on this on InfoQ</a>. The article is a bit messy, but it\'s possible to follow. \nThere are a number of different products that does AOP in the .NET space, i recommend PostSharp.</p>\n'}, {'answer_id': 63745, 'author': 'Sean Hanley', 'author_id': 7290, 'author_profile': 'https://Stackoverflow.com/users/7290', 'pm_score': 0, 'selected': False, 'text': '<p>If you do go with Attributes, I\'m fairly certain you\'ll have to <a href="http://www.csharphelp.com/archives/archive119.html" rel="nofollow noreferrer">roll your own logic</a> to deduce what they mean and what to do about them.  Whatever is using your custom class objects will have to have a way of performing these attribute actions/checks, preferably at instantiation.</p>\n\n<p>Otherwise, you\'re looking at using maybe events.  You\'d still have to add the event to every set method, but the benefit there would be you\'re not hard-coding what to do about dirty sets on every property and can control, in one place, what is to be done.  That would, at the very least, introduce a bit more code re-use.</p>\n'}, {'answer_id': 63798, 'author': 'Pop Catalin', 'author_id': 4685, 'author_profile': 'https://Stackoverflow.com/users/4685', 'pm_score': 2, 'selected': False, 'text': '<p>If you really want to go that way, to modify what the code does using an attribute, there are some ways to do it and they all are related to AOP (Aspect oriented programming). Check out <a href="http://www.postsharp.org/" rel="nofollow noreferrer">PostSharp</a>, which is an aftercompiler that can modify your code in a after compilation step. For example you could set up one custom attribute for your properties (or aspect, how it is called in AOP) that injects code inside property setters, that marks your objects as dirty. If you want some examples of how this is achieved you can check out their <a href="http://www.postsharp.org/about/getting-started/" rel="nofollow noreferrer">tutorials</a>. </p>\n\n<p><em>But be careful with AOP and because you can just as easily create more problems using it that you\'re trying to solve if not used right.</em></p>\n\n<p>There are more AOP frameworks out there some using post compilation and some using method interception mechanisms that are present in .Net, the later have some performance drawbacks compared to the first.</p>\n'}]
['https://Stackoverflow.com/questions/63556', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1404/']