title: Any way to avoid Property inline optimization in C#?
body: <p>So I have a PropertyBag class that is intended to implement INotifyPropertyChanged.  In order to make this code work as cleanly as possible and to avoid user error, I am using the stack to get the property name.  See, if the property name doesn't match the actual property exactly, then you will have a failure and I am trying to protect from that. </p>
  
  <p>So, here is an example usage of the class:</p>
  
  <pre><code>public class MyData : PropertyBag
  {
      public MyData()
      {
          Foo = -1;
      }
  
      public int Foo
      {
          get { return GetProperty&lt;int&gt;(); }
          set { SetProperty(value); }
      }
  }
  </code></pre>
  
  <p>The important code for the base PropertyBag is here:</p>
  
  <pre><code>public abstract class PropertyBag : INotifyPropertyChanged
  {
      protected T GetProperty&lt;T&gt;()
      {
          string propertyName = PropertyName((new StackTrace()).GetFrame(1));
          if (propertyName == null)
              throw new ArgumentException("GetProperty must be called from a property");
  
          return GetValue&lt;T&gt;(propertyName);
      }
  
      protected void SetProperty&lt;T&gt;(T value)
      {
          string propertyName = PropertyName((new StackTrace()).GetFrame(1));
          if (propertyName == null)
              throw new ArgumentException("SetProperty must be called from a property");
  
          SetValue(propertyName, value);
      }
  
      private static string PropertyName(StackFrame frame)
      {
          if (frame == null) return null;
          if (!frame.GetMethod().Name.StartsWith("get_") &amp;&amp;
             !frame.GetMethod().Name.StartsWith("set_"))
              return null;
  
          return frame.GetMethod().Name.Substring(4);
      }
  }
  </code></pre>
  
  <p>So now that you have seen my code, I can tell you the problem... In some cases under release build, the "Foo" setter in the "MyData" constructor appears to be getting optimized to inline as SetProperty(-1).  Unfortunately, this inline optimization fails out my SetProperty method because I am no longer calling it from a property!  FAIL.  It appears that I cannot rely on the StackTrace in this way.</p>
  
  <p>Can anyone 
  A: Figure out a better way to do this but still avoid passing in "Foo" to GetProperty and SetProperty?<br>
  B: Figure out a way to tell the compiler to not optimize in this case?</p>