如何在C#中将值从一种类类型复制到另一种类

  • 本文关键字:种类 类型 复制 c#
  • 更新时间 :
  • 英文 :


我正在使用fileHelpers从文本文件中获取数据。我有两种文本文件类型,因此我创建了两个类:

    [DelimitedRecord("t")]
    public sealed class dataFromFileType1
    {
        //Declare double type for wavelength
        [FieldTrim(TrimMode.Both)]
        public Double field1;
        //Declare double type for reflectance
        [FieldTrim(TrimMode.Both)]
        public Double field2;
    }
    [DelimitedRecord(",")]
    public sealed class dataFromFileType2
    {
        //Declare double type for wavelength
        [FieldTrim(TrimMode.Both)]
        public Double field1;
        //Declare double type for reflectance
        [FieldTrim(TrimMode.Both)]
        public Double field2;
    }

最后,我想将这两个类类型转换为一个统一的类类型,这样我就可以使用一个方法来处理这两个数据。我定义:

    public sealed class unifiedData
    {
        public Double field1;
        public Double field2;
    }

问题是如何将类dataFromFileType1和类dataFromFileType2的对象数组复制(或转换)到类unifiedData的对象数组?

例如,您可以使用AutoMapper:https://github.com/AutoMapper/AutoMapper

Mapper.CreateMap<dataFromFileType1, unifiedData>();
Mapper.CreateMap<dataFromFileType2, unifiedData>();

最新更新