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

  • Home
  • SEARCH
  • 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 4038914
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:32:03+00:00 2026-05-20T12:32:03+00:00

I was looking at this post that describes a simple way to do databinding

  • 0

I was looking at this post that describes a simple way to do databinding between POCO properties: Data Binding POCO Properties

One of the comments by Bevan included a simple Binder class that can be used to accomplish such data binding. It works great for what I need but I would like to implement some of the suggestions that Bevan made to improve the class, namely:

  • Checking that source and target are
    assigned
  • Checking that the properties
    identified by sourcePropertyName and
    targetPropertyName exist
  • Checking for type compatibility
    between the two properties

Also, given that specifying properties by string is error prone, you could use Linq expressions and extension methods instead. Then instead of writing

Binder.Bind( source, "Name", target, "Name")

you could write

source.Bind( Name => target.Name);

I’m pretty sure I can handle the first three (though feel free to include those changes) but I have no clue how to use Linq expressions and extension methods to be able to write code without using property name strings.

Any tips?

Here is the original code as found in the link:

public static class Binder
{

    public static void Bind(
        INotifyPropertyChanged source,
        string sourcePropertyName,
        INotifyPropertyChanged target,
        string targetPropertyName)
    {
        var sourceProperty
            = source.GetType().GetProperty(sourcePropertyName);
        var targetProperty
            = target.GetType().GetProperty(targetPropertyName);

        source.PropertyChanged +=
            (s, a) =>
            {
                var sourceValue = sourceProperty.GetValue(source, null);
                var targetValue = targetProperty.GetValue(target, null);
                if (!Object.Equals(sourceValue, targetValue))
                {
                    targetProperty.SetValue(target, sourceValue, null);
                }
            };

        target.PropertyChanged +=
            (s, a) =>
            {
                var sourceValue = sourceProperty.GetValue(source, null);
                var targetValue = targetProperty.GetValue(target, null);
                if (!Object.Equals(sourceValue, targetValue))
                {
                    sourceProperty.SetValue(source, targetValue, null);
                }
            };
    }
}
  • 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-20T12:32:04+00:00Added an answer on May 20, 2026 at 12:32 pm

    The following will return a property name as a string from a lambda expression:

    public string PropertyName<TProperty>(Expression<Func<TProperty>> property)
    {
      var lambda = (LambdaExpression)property;
    
      MemberExpression memberExpression;
      if (lambda.Body is UnaryExpression)
      {
        var unaryExpression = (UnaryExpression)lambda.Body;
        memberExpression = (MemberExpression)unaryExpression.Operand;
      }
      else
      {
        memberExpression = (MemberExpression)lambda.Body;
      }
    
      return memberExpression.Member.Name;
    }
    

    Usage:

    public class MyClass
    {
      public int World { get; set; }
    }
    
    ...
    var c = new MyClass();
    Console.WriteLine("Hello {0}", PropertyName(() => c.World));
    

    UPDATE

    public static class Extensions
    {
        public static void Bind<TSourceProperty, TDestinationProperty>(this INotifyPropertyChanged source, Expression<Func<TSourceProperty, TDestinationProperty>> bindExpression)
        {
            var expressionDetails = GetExpressionDetails<TSourceProperty, TDestinationProperty>(bindExpression);
            var sourcePropertyName = expressionDetails.Item1;
            var destinationObject = expressionDetails.Item2;
            var destinationPropertyName = expressionDetails.Item3;
    
            // Do binding here
            Console.WriteLine("{0} {1}", sourcePropertyName, destinationPropertyName);
        }
    
        private static Tuple<string, INotifyPropertyChanged, string> GetExpressionDetails<TSourceProperty, TDestinationProperty>(Expression<Func<TSourceProperty, TDestinationProperty>> bindExpression)
        {
            var lambda = (LambdaExpression)bindExpression;
    
            ParameterExpression sourceExpression = lambda.Parameters.FirstOrDefault();
            MemberExpression destinationExpression = (MemberExpression)lambda.Body;
    
            var memberExpression = destinationExpression.Expression as MemberExpression;
            var constantExpression = memberExpression.Expression as ConstantExpression;
            var fieldInfo = memberExpression.Member as FieldInfo;
            var destinationObject = fieldInfo.GetValue(constantExpression.Value) as INotifyPropertyChanged;
    
            return new Tuple<string, INotifyPropertyChanged, string>(sourceExpression.Name, destinationObject, destinationExpression.Member.Name);
        }
    }
    

    Usage:

    public class TestSource : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public string Name { get; set; }        
    }
    
    public class TestDestination : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public string Id { get; set; }    
    }
    
    class Program
    {        
        static void Main(string[] args)
        {
            var x = new TestSource();
            var y = new TestDestination();
    
            x.Bind<string, string>(Name => y.Id);
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking for this information for my commercial desktop product, with no avail.
I'm looking at this as a baseline explanation of the SQL 2005 Enterprise partitioning.
I'm looking at this control, and it seems to be lacking the standard .net
I was looking at this article from 2005 and wanted to get some thoughts
I was looking over this code to calculate math.sqrt in Java. Why did they
I have a coworker looking for this, and I don't recall ever running into
I have a piece of code looking like this : TAxis *axis = 0;
I have a plain text file looking like this: some text containing line breaks
In a project we have text files looking like this: mv A, R3 mv
Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html I noticed that in some

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.