Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 128937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:42:51+00:00 2026-05-11T05:42:51+00:00

So I have a PropertyBag class that is intended to implement INotifyPropertyChanged. In order

  • 0

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.

So, here is an example usage of the class:

public class MyData : PropertyBag {     public MyData()     {         Foo = -1;     }      public int Foo     {         get { return GetProperty<int>(); }         set { SetProperty(value); }     } } 

The important code for the base PropertyBag is here:

public abstract class PropertyBag : INotifyPropertyChanged {     protected T GetProperty<T>()     {         string propertyName = PropertyName((new StackTrace()).GetFrame(1));         if (propertyName == null)             throw new ArgumentException('GetProperty must be called from a property');          return GetValue<T>(propertyName);     }      protected void SetProperty<T>(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_') &&            !frame.GetMethod().Name.StartsWith('set_'))             return null;          return frame.GetMethod().Name.Substring(4);     } } 

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.

Can anyone A: Figure out a better way to do this but still avoid passing in ‘Foo’ to GetProperty and SetProperty?
B: Figure out a way to tell the compiler to not optimize in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:42:52+00:00Added an answer on May 11, 2026 at 5:42 am

    Using the stack here is slow and unnecessary; I would simply use:

    get { return GetProperty<int>('Foo'); } set { SetProperty('Foo', value); } 

    (hint: I’ve done a lot of work with custom property models; I know that this works well…)

    Another alternative is an object key (use reference equality to compare) – a lot of ComponentModel works this way, as do some of the properties in WF/WPF:

    static readonly object FooKey = new object(); ... get { return GetProperty<int>(FooKey); } set { SetProperty(FooKey, value); } 

    Of course, you could declare a type for the keys (with a Name property), and use that:

    static readonly PropertyKey FooKey = new PropertyKey('Foo'); 

    etc; however, to answer the question: mark it (but don’t do this) with:

    [MethodImpl(MethodImplOptions.NoInlining)] 

    or

    [MethodImpl(MethodImplOptions.NoOptimization)] 

    or

    [MethodImpl(MethodImplAttributes.NoOptimization     | MethodImplAttributes.NoInlining)] 

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some user-specific data that I need to store in SharePoint and make
I have a scenario where I have a propertybag of key/values that would look
I have a class that is used as part of a financial application. The
I have a base class and several concrete classes that derive from it. Lets
Is it possible to do something like this in WPF? Presuming I have a
I have a class that contains a property-value (property bag) dictionary beside normal properties.
I have an object that maintains a property bag of various properties. This is
I have a Request class that is used to retrieve a file resource from
I have a Rhodes (RhoMobile) PropertyBag Model that I would like to convert to
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.