Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to create a function that takes in a object property and returns back object property. How to get the property values into this specific Function, so that this function only takes takes in the object's specific property and not the entire object?

class Program
{

  public T MapFrom<T,V>(T SourceObject.property, V DestinationObject.Property) 
  //Not able to achieve this//
  {
    // Code to Map Property
  }


  // Here I want to specifically pass only one property of Object , not the entire one
  ProgramClassObject.MapFrom<Details,Person>(Details.FirstName,Person.FName)

  }  
}

// Class Containing Property
class Details
{
  public string FirstName { get; set;}           
}

// Class Containing Property
class Person
{
  public string FName { get; set;}           
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
240 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can do it manually, or use some library (see comments, someone mentetioned about it).

If still want to implement yourself:

Prepare some useful Expression extensions:

public static B GetProperty<T, B>(this Expression<Func<T, B>> propertySelector, T target) where T : class
{
    if (target == null)
    {
        throw new ArgumentNullException("target");
    }

    if (propertySelector == null)
    {
        throw new ArgumentNullException("propertySelector");
    }

    var memberExpression = propertySelector.Body as MemberExpression;
    if (memberExpression == null)
    {
        throw new NotSupportedException("Only member expression is supported.");
    }

    var propertyInfo = memberExpression.Member as PropertyInfo;
    if (propertyInfo == null)
    {
        throw new NotSupportedException("You can select property only. Currently, selected member is: " +
                                        memberExpression.Member);
    }

    return (B)propertyInfo.GetValue(target);
}

public static void SetProperty<T, B>(this Expression<Func<T, B>> propertySelector, T target, B value)
{
    SetObjectProperty(target, propertySelector, value);
}

public static void SetObjectProperty<T, B>(T target, Expression<Func<T, B>> propertySelector, object value)
{
    if (target == null)
    {
        throw new ArgumentNullException("target");
    }

    if (propertySelector == null)
    {
        throw new ArgumentNullException("propertySelector");
    }

    var memberExpression = propertySelector.Body as MemberExpression;
    if (memberExpression == null)
    {
        throw new NotSupportedException("Cannot recognize property.");
    }

    var propertyInfo = memberExpression.Member as PropertyInfo;
    if (propertyInfo == null)
    {
        throw new NotSupportedException("You can select property only. Currently, selected member is: " + memberExpression.Member);
    }

    propertyInfo.SetValue(target, value);
}

MapFrom implementation:

public static void MapFrom<TObject, TTarget, TProp>(TObject source, TTarget dest,
    Expression<Func<TObject, TProp>> sourceSelector, Expression<Func<TTarget, TProp>> targetSelector)
          where TObject : class where TTarget : class
{
    var sourceValue = sourceSelector.GetProperty(source);
    targetSelector.SetProperty(dest, sourceValue);
}

Usage:

programClassObject.MapFrom(details, person, det => det.FirstName, per => per.FName);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...