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 960485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:11:23+00:00 2026-05-16T01:11:23+00:00

This question will probably take a while to explain, and I’ll need to provide

  • 0

This question will probably take a while to explain, and I’ll need to provide background…

This is just something I’m playing about with and isn’t for production, but at the moment I have some code which looks like this:

var myDataModel = new DataModel();

myDataModel.PropertyChanged += myDataModel_PropertyChanged;

myDataModel.ChangeProperty(t => t.TestValue, 2);

So, rather than using myDataModel.TestValue = 2 directly, I’m using a ChangeProperty extension method so that I can handle all of the change events and do anything I want to in one place. My extension method looks like this (and yes, I know it’s hacky):

public static class NotifyPropertyChangedExtensions
{
    public static void ChangeProperty<T, U>(
                                      this T instance, 
                                      Expression<Func<T, U>> propertyToChange, 
                                      U newValue)
    {
        var member = propertyToChange.Body as MemberExpression;

        if (member != null)
        {
            if (!propertyToChange.Compile().Invoke(instance).Equals(newValue))
            {
                var setProperty = instance.GetType().GetProperty(
                                      member.Member.Name, 
                                        BindingFlags.SetProperty | 
                                        BindingFlags.Public | 
                                        BindingFlags.Instance);

                if (setProperty != null)
                {
                    // actually set the property
                    setProperty.SetValue(instance, newValue, null);

                    // raise the property changed event
                    if (typeof(INotifyPropertyChanged).IsAssignableFrom(
                                                                 typeof(T)))
                    {
                        var delegatesToCall = 
                             instance.GetType().GetField("PropertyChanged", 
                                       BindingFlags.Instance | 
                                       BindingFlags.NonPublic)
                                  .GetValue(instance) as MulticastDelegate;


                        if (delegatesToCall != null)
                        {
                            var eventArgs = new PropertyChangedEventArgs(
                                                           setProperty.Name);
                            foreach (var @delegate in 
                                         delegatesToCall.GetInvocationList())
                            {
                                @delegate.Method.Invoke(
                                       @delegate.Target, 
                                       new object[] { instance, eventArgs });
                            }
                        }

                    }
                }
            }
        }
        else
        {
            throw new ArgumentException(
                    string.Format(
                           "Cannot determine the property to change {0}", 
                           propertyToChange));
        }
    }
}

With this architecture, my data model is quite clean:

public class DataModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public int TestValue { get; set; }
} 

That is, I can use auto-properties, and don’t need to worry about raising events etc.

Now, what I actually want to do is something closer to this:

var dataModel = new DataModel();

myDataModel.PropertyChanged += myDataModel_PropertyChanged;

myDataModel.TestValue.Set(2); // this is what I want...

So, I’m thinking I’ll basically need an extension method – but I can only see how to send the property itself (the TestValue in this case), and the new value. So then I wondered if it’s possible, given a property, to find out the instance of the class it belongs to?

  • 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. Editorial Team
    Editorial Team
    2026-05-16T01:11:23+00:00Added an answer on May 16, 2026 at 1:11 am
    1. Don’t do this. It breaks encapsulation.

    2. No. In myDataModel.TestValue.Set(2); the extension method will always be called on the value returned by the property. There is no way to get the class, instance or property that returned the value.

    3. You could do something like this:

      var t = new DataModel();
      ((Expression<Func<int>>)(() => t.Foo)).Set(100);
      

      with

      static class Extensions
      {
          public static void Set<T>(this Expression<Func<T>> expression, T value)
          { ... }
      }
      

      but this is ugly, almost unreadable, unclear, inefficient, and error prone.

    4. You’re looking for Aspect Oriented Programming (AOP).

      Have a look at PostSharp or LinFu.

    5. There is no really clean solution to implementing INotifyPropertyChanged yet. If typing all the property setters is too much work or too error prone, I’d generate them with a T4 template.

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

Sidebar

Related Questions

I don't know much about web development, so probably this question will sound exceptionally
I'm about to start a new C# application, which will probably take some while
This is confusing me, so this question will probably be confusing. I have a
This is a very specific question which will probably earn me the tumbleweed badge,
This is a very newbie question and i will probably get downvoted for it,
This will probably be a bot of a waffly question but ill try my
This will be probable quite odd question. But i thought I will give it
This question will be a little longer and I am sorry for that :)
This question will be short and sweet. I know an instruction can occur between
( Late edit: This question will hopefully be obsolete when Java 7 comes, because

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.