Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用泛型函数将一个集合的内容复制到另一个集合_C#_Generics - Fatal编程技术网

C# 使用泛型函数将一个集合的内容复制到另一个集合

C# 使用泛型函数将一个集合的内容复制到另一个集合,c#,generics,C#,Generics,假设我有两个集合,即列表和下面的列表 private List<PersonOld> GetOldPersonRecord() { var sourceList = new List<PersonOld>(); for (int i = 1; i <= 10; i++) sourceList.Add(new PersonOld { PersonId = i, PersonName = "

假设我有两个集合,即列表和下面的列表

private List<PersonOld> GetOldPersonRecord()
{
            var sourceList = new List<PersonOld>();
            for (int i = 1; i <= 10; i++)
                sourceList.Add(new PersonOld { PersonId = i, PersonName = "Name" + i.ToString() });
            return sourceList;
}
PersonOld

public class PersonOld
{
        public int PersonId { get; set; }
        public string PersonName { get; set; }
}
我可能不得不使用反射

提前谢谢

您可以看一下


就实用程序方法而言,必须声明泛型参数:

public class Utility
{
    public static List<T2> Fill<T1, T2>(List<T1> Source, List<T2> Destination)
    {
        return null;
    }
}
公共类实用程序
{
公共静态列表填充(列表源、列表目标)
{
返回null;
}
}

如果使用反射,则迭代源集合,实例化目标类的实例。将它们插入新创建的列表中

接下来,对源类型使用GetProperties来获取PropertyInfo类的集合。遍历这些属性,选择每个属性的名称,并使用Type.GetProperty查看目标类上是否有同名属性。如果是,请使用PropertyInfo.SetValue设置每个目标对象上的值

NB如果属性是引用类型,则需要做更多的工作——您需要考虑是否要复制这些类型,或者复制引用


如果对象是相同的,另一种方法是与XML进行序列化。

下面是一个工作示例:

主要部分是CreateMapping方法,它只提供一个用于从一种类型转换到另一种类型的委托。一旦有了这些,将源对象复制到目标对象列表就变得很简单了,如下面我的Fill方法所示

    public static Func<T1, T2> CreateMapping<T1, T2>()
        where T2 : new()
    {
        var typeOfSource = typeof(T1);
        var typeOfDestination = typeof(T2);

        // use reflection to get a list of the properties on the source and destination types
        var sourceProperties = typeOfSource.GetProperties();
        var destinationProperties = typeOfDestination.GetProperties();

        // join the source properties with the destination properties based on name
        var properties = from sourceProperty in sourceProperties
                         join destinationProperty in destinationProperties
                         on sourceProperty.Name equals destinationProperty.Name
                         select new { SourceProperty = sourceProperty, DestinationProperty = destinationProperty };

        return (x) =>
        {
            var y = new T2();

            foreach (var property in properties)
            {
                var value = property.SourceProperty.GetValue(x, null);
                property.DestinationProperty.SetValue(y, value, null);
            }

            return y;
        };
    }

    public static void Fill<T1, T2>(List<T1> Source, List<T2> Destination)
        where T2 : new()
    {
        Destination.AddRange(Source.Select(CreateMapping<T1, T2>()));
    }
public static Func CreateMapping()
其中T2:new()
{
var typeOfSource=typeof(T1);
var typeOfDestination=typeof(T2);
//使用反射获取源和目标类型的属性列表
var sourceProperties=typeOfSource.GetProperties();
var destinationProperties=typeOfDestination.GetProperties();
//基于名称将源属性与目标属性联接
var properties=来自sourceProperties中的sourceProperty
在destinationProperties中加入destinationProperty
在sourceProperty.Name上等于destinationProperty.Name
选择新建{SourceProperty=SourceProperty,DestinationProperty=DestinationProperty};
返回(x)=>
{
var y=新的T2();
foreach(属性中的var属性)
{
var value=property.SourceProperty.GetValue(x,null);
property.DestinationProperty.SetValue(y,value,null);
}
返回y;
};
}
公共静态空白填充(列表源、列表目标)
其中T2:new()
{
Destination.AddRange(Source.Select(CreateMapping());
}

询问TeamCity用户名和密码pwd@user1025901,您可以通过NuGet软件包安装它:
install package AutoMapper
或从GitHub下载:那很好……如果我想自己编程,请您提供初步指导……实用程序函数声明给出了一些建议problem@user1025901,你想自己做什么?您不想使用AutoMapper?错误:找不到类型或命名空间名称“T2”。。找不到类型或命名空间名称“T1”。尝试使用此公共列表填充(列表源,列表目标){type type1=typeof(T1);var type1List=type1.GetProperties();type type2=typeof(T2);var type2List=type2.GetProperties();foreach(type1List中的PropertyInfo prop){var name=prop.name;}}。。你能帮个忙吗嗨,我已经更新了我的答案。但是我还没有找到解决方案。你能帮点忙吗?使用“动态”是一个可行的选择吗?
public class PersonOld
{
        public int PersonId { get; set; }
        public string PersonName { get; set; }
}
public class Utility
{
    public static List<T2> Fill<T1, T2>(List<T1> Source, List<T2> Destination)
    {
        return null;
    }
}
    public static Func<T1, T2> CreateMapping<T1, T2>()
        where T2 : new()
    {
        var typeOfSource = typeof(T1);
        var typeOfDestination = typeof(T2);

        // use reflection to get a list of the properties on the source and destination types
        var sourceProperties = typeOfSource.GetProperties();
        var destinationProperties = typeOfDestination.GetProperties();

        // join the source properties with the destination properties based on name
        var properties = from sourceProperty in sourceProperties
                         join destinationProperty in destinationProperties
                         on sourceProperty.Name equals destinationProperty.Name
                         select new { SourceProperty = sourceProperty, DestinationProperty = destinationProperty };

        return (x) =>
        {
            var y = new T2();

            foreach (var property in properties)
            {
                var value = property.SourceProperty.GetValue(x, null);
                property.DestinationProperty.SetValue(y, value, null);
            }

            return y;
        };
    }

    public static void Fill<T1, T2>(List<T1> Source, List<T2> Destination)
        where T2 : new()
    {
        Destination.AddRange(Source.Select(CreateMapping<T1, T2>()));
    }