如何将对象属性传递给一个通用函数 [英] How to pass objects properties into a generic function

查看:108
本文介绍了如何将对象属性传递给一个通用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,它接受一个对象属性并返回对象属性。
如何获取属性值到这个特定的函数中,这样这个函数只需要取得对象的特定属性而不是整个对象?

  class Program 
{

public T MapFrom< T,V>(T SourceObject.property,V DestinationObject.Property)
//无法实现这个//
{
//代码映射属性
}


//这里我想特别传递Object的一个属性,而不是全部
ProgramClassObject.MapFrom< Details,Person>(Details.FirstName,Person.FName)

}
}

// Class包含属性
class详细信息
{
public string FirstName {get; set;}
}

//包含属性的类
类Person
{
public string FName {get; set;}
}


解决方案

它手动,或使用一些库(见评论,有人提到它)。



如果仍然想实现自己:

准备一些有用的表达式扩展:

  public static B GetProperty< T,B>(这个表达式< Func< T,B>> propertySelector,T target)其中T:class 
{
if(target == null)
{
抛出新的ArgumentNullException(target);


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

var memberExpression = propertySelector.Body as MemberExpression;
if(memberExpression == null)
{
throw new NotSupportedException(仅支持成员表达式。
}

var propertyInfo = memberExpression.Member as PropertyInfo;
if(propertyInfo == null)
{
throw new NotSupportedException(您可以只选择属性,当前选定的成员是:+
memberExpression.Member);
}

return(B)propertyInfo.GetValue(target);


public static void SetProperty< T,B>(此表达式< Func< T,B>> propertySelector,T target,B value)
{
SetObjectProperty(target,propertySelector,value);

$ b 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(Can not identify property。);
}

var propertyInfo = memberExpression.Member as PropertyInfo;
if(propertyInfo == null)
{
throw new NotSupportedException(您只能选择属性,当前所选成员为:+ memberExpression.Member);
}

propertyInfo.SetValue(target,value);

code $

$ MapFrom
执行:

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

用法:

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


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

解决方案

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

这篇关于如何将对象属性传递给一个通用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆