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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:29:14+00:00 2026-05-21T14:29:14+00:00

I’m playing around with the idea of passing a property assignment to a method

  • 0

I’m playing around with the idea of passing a property assignment to a method as an expression tree. The method would Invoke the expression so that the property gets assigned properly, and then sniff out the property name that was just assigned so I can raise the PropertyChanged event. The idea is that I’d like to be able to use slim auto-properties in my WPF ViewModels and still have the PropertyChanged event fired off.

I’m an ignoramus with ExpressionTrees, so I’m hoping someone can point me in the right direction:

public class ViewModelBase {
    public event Action<string> PropertyChanged = delegate { };

    public int Value { get; set; }

    public void RunAndRaise(MemberAssignment Exp) {
        Expression.Invoke(Exp.Expression);
        PropertyChanged(Exp.Member.Name);
    }
}

The problem is I’m not sure how to call this. This naive attempt was rejected by the compiler for reasons that I’m sure will be obvious to anyone who can answer this:

        ViewModelBase vm = new ViewModelBase();

        vm.RunAndRaise(() => vm.Value = 1);

EDIT

Thank you @svick for the perfect answer. I moved one little thing around and made it into an extension method. Here’s the complete code sample with unit test:

[TestClass]
public class UnitTest1 {
    [TestMethod]
    public void TestMethod1() {
        MyViewModel vm = new MyViewModel();
        bool ValuePropertyRaised = false;
        vm.PropertyChanged += (s, e) => ValuePropertyRaised = e.PropertyName == "Value";

        vm.SetValue(v => v.Value, 1);

        Assert.AreEqual(1, vm.Value);
        Assert.IsTrue(ValuePropertyRaised);
    }
}


public class ViewModelBase : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void OnPropertyChanged(string propertyName) {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class MyViewModel : ViewModelBase {
    public int Value { get; set; }
}

public static class ViewModelBaseExtension {
    public static void SetValue<TViewModel, TProperty>(this TViewModel vm, Expression<Func<TViewModel, TProperty>> exp, TProperty value) where TViewModel : ViewModelBase {
        var propertyInfo = (PropertyInfo)((MemberExpression)exp.Body).Member;
        propertyInfo.SetValue(vm, value, null);
        vm.OnPropertyChanged(propertyInfo.Name);
    }
}
  • 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-21T14:29:14+00:00Added an answer on May 21, 2026 at 2:29 pm

    You can’t do it this way. First, lambda expressions can be converted only to delegate types or Expression<T>.

    If you change the signature of the method (for now ignoring its implementation) to public void RunAndRaise(Expression<Action> Exp), the compiler complains that “An expression tree may not contain an assignment operator”.

    You could do it by specifying the property using lambda and the value you want to set it to in another parameter. Also, I didn’t figure out a way to access the value of vm from the expression, so you have to put that in another parameter (you can’t use this for that, because you need the proper inherited type in the expression):see edit

    public static void SetAndRaise<TViewModel, TProperty>(
        TViewModel vm, Expression<Func<TViewModel, TProperty>> exp, TProperty value)
        where TViewModel : ViewModelBase
    {
        var propertyInfo = (PropertyInfo)((MemberExpression)exp.Body).Member;
        propertyInfo.SetValue(vm, value, null);
        vm.PropertyChanged(propertyInfo.Name);
    }
    

    Another possibility (and one I like more) is to raise the event from setter specifically using lambda like this:

    private int m_value;
    public int Value
    {
        get { return m_value; }
        set
        {
            m_value = value;
            RaisePropertyChanged(this, vm => vm.Value);
        }
    }
    
    static void RaisePropertyChanged<TViewModel, TProperty>(
        TViewModel vm, Expression<Func<TViewModel, TProperty>> exp)
        where TViewModel : ViewModelBase
    {
        var propertyInfo = (PropertyInfo)((MemberExpression)exp.Body).Member;
        vm.PropertyChanged(propertyInfo.Name);
    }
    

    This way, you can use the properties as usual, and you could also raise events for computed properties, if you had them.

    EDIT: While reading through Matt Warren’s series about implementing IQueryable<T>, I realized I can access the referenced value, which simplifies the usage of RaisePropertyChanged() (although it won’t help much with your SetAndRaise()):

    private int m_value;
    public int Value
    {
        get { return m_value; }
        set
        {
            m_value = value;
            RaisePropertyChanged(() => Value);
        }
    }
    
    static void RaisePropertyChanged<TProperty>(Expression<Func<TProperty>> exp)
    {
        var body = (MemberExpression)exp.Body;
        var propertyInfo = (PropertyInfo)body.Member;
        var vm = (ViewModelBase)((ConstantExpression)body.Expression).Value;
        vm.PropertyChanged(vm, new PropertyChangedEventArgs(propertyInfo.Name));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I need a function that will clean a strings' special characters. I do NOT

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.