Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

c# - How to pass objects properties into a generic function

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

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

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...