Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't do it this way. First, lambda expressions can be converted only to delegate types or <code>Expression&lt;T&gt;</code>.</p> <p>If you change the signature of the method (for now ignoring its implementation) to <code>public void RunAndRaise(Expression&lt;Action&gt; Exp)</code>, the compiler complains that “An expression tree may not contain an assignment operator”. </p> <p>You could do it by specifying the property using lambda and the value you want to set it to in another parameter. <s>Also, I didn't figure out a way to access the value of <code>vm</code> from the expression, so you have to put that in another parameter (you can't use <code>this</code> for that, because you need the proper inherited type in the expression)</s>:<sup>see edit</sup></p> <pre><code>public static void SetAndRaise&lt;TViewModel, TProperty&gt;( TViewModel vm, Expression&lt;Func&lt;TViewModel, TProperty&gt;&gt; exp, TProperty value) where TViewModel : ViewModelBase { var propertyInfo = (PropertyInfo)((MemberExpression)exp.Body).Member; propertyInfo.SetValue(vm, value, null); vm.PropertyChanged(propertyInfo.Name); } </code></pre> <p>Another possibility (and one I like more) is to raise the event from setter specifically using lambda like this:</p> <pre><code>private int m_value; public int Value { get { return m_value; } set { m_value = value; RaisePropertyChanged(this, vm =&gt; vm.Value); } } static void RaisePropertyChanged&lt;TViewModel, TProperty&gt;( TViewModel vm, Expression&lt;Func&lt;TViewModel, TProperty&gt;&gt; exp) where TViewModel : ViewModelBase { var propertyInfo = (PropertyInfo)((MemberExpression)exp.Body).Member; vm.PropertyChanged(propertyInfo.Name); } </code></pre> <p>This way, you can use the properties as usual, and you could also raise events for computed properties, if you had them.</p> <p><strong>EDIT:</strong> While reading through <a href="http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx" rel="nofollow">Matt Warren's series about implementing <code>IQueryable&lt;T&gt;</code></a>, I realized I can access the referenced value, which simplifies the usage of <code>RaisePropertyChanged()</code> (although it won't help much with your <code>SetAndRaise()</code>):</p> <pre><code>private int m_value; public int Value { get { return m_value; } set { m_value = value; RaisePropertyChanged(() =&gt; Value); } } static void RaisePropertyChanged&lt;TProperty&gt;(Expression&lt;Func&lt;TProperty&gt;&gt; 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)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload