gpt4 book ai didi

c# - 在一个泛型中转换 3 个方法 (C#)

转载 作者:行者123 更新时间:2023-12-02 06:12:57 25 4
gpt4 key购买 nike

我有 3 种方法可以做同样的事情。一个区别是,它们使用不同的模型。

方法代码如下:

public IEnumerable<PropertyImportDto> ImportStandardCsv(string fileData)
{
List<PropertyImportDto> data;
using (var memoryStream =
new MemoryStream(Convert.FromBase64String(fileData.Substring(fileData.IndexOf(',') + 1))))
{
using (var streamWriter = new StreamReader(memoryStream))
using (var csvReader = new CsvReader(streamWriter))
{
var records = csvReader.GetRecords<PropertyImportDto>();
data = records.ToList();
}
}

return data;
}

public IEnumerable<ArthurPropertiesImportDto> ImportArthurCsv(string fileData)
{
List<ArthurPropertiesImportDto> data;
using (var memoryStream =
new MemoryStream(Convert.FromBase64String(fileData.Substring(fileData.IndexOf(',') + 1))))
{
using (var streamWriter = new StreamReader(memoryStream))
using (var csvReader = new CsvReader(streamWriter))
{
var records = csvReader.GetRecords<ArthurPropertiesImportDto>();
data = records.ToList();
}
}

return data;
}


public IEnumerable<LandlordVisionImportDto> ImportLandlordVision(string fileData)
{
List<LandlordVisionImportDto> data;
using (var memoryStream =
new MemoryStream(Convert.FromBase64String(fileData.Substring(fileData.IndexOf(',') + 1))))
{
using (var streamWriter = new StreamReader(memoryStream))
using (var csvReader = new CsvReader(streamWriter))
{
var records = csvReader.GetRecords<LandlordVisionImportDto>();
data = records.ToList();
}
}

return data;
}

我是这样调用它的

public async Task ImportProperties(PM101ImportDto input)
{
switch (input.fileTypeId)
{
case 1:
{
var properties = _csvAppService.ImportStandardCsv(input.FileBase64);
foreach (var item in properties)
{
var property = new Property
{
PropertyTypeId = 1,
BuildingTypeId = 12,
PhoneNumber = item.PhoneNumber,
PropertyTitleId = 1,
AccountManagerId = AbpSession.TenantId,
Addresses = new List<PropertyAddress>
{
new PropertyAddress
{
Line1 = item.Line1,
Line2 = item.Line2,
Line3 = item.Line3,
PostCode = item.PostalCode,
PostTown = item.Town,
Country = item.Country,
County = item.County,
Latitude = item.Latitude,
Longitude = item.Longitude
},
},
Sites = new List<Site>
{
new Site
{
NumberOfWindows = 0,
NumberOfBedRooms = 0,
ApproximateYearBuilt = 1970,
PropertyAge = 50,
FuelTypeId = 1,
ParkingTypeId = 1,
FurnishingTypeId = 1
}
},
MarketingInformation = new List<PropertyMarketingInformation>
{
new PropertyMarketingInformation
{
MarketingDescription = "", IsBillsIncluded = false
}
}
};
await _propertyRepository.InsertAsync(property);
}

break;
}
case 2:
{
var properties = _csvAppService.ImportLandlordVision(input.FileBase64);
foreach (var item in properties)
{
var property = new Property
{
PropertyTypeId = 1,
BuildingTypeId = 12,
PropertyTitleId = 1,
AccountManagerId = AbpSession.TenantId,
Addresses = new List<PropertyAddress>
{
new PropertyAddress
{
Line1 = item.Line1,
Line2 = item.Line2,
PostCode = item.PostCode,
PostTown = item.Town,
County = item.County
}
},
Sites = new List<Site>
{
new Site
{
NumberOfWindows = 0,
NumberOfBedRooms = 0,
ApproximateYearBuilt = 1970,
PropertyAge = 50,
FuelTypeId = 1,
ParkingTypeId = 1,
FurnishingTypeId = 1
}
},
MarketingInformation = new List<PropertyMarketingInformation>
{
new PropertyMarketingInformation
{
MarketingDescription = "", IsBillsIncluded = false
}
}
};
await _propertyRepository.InsertAsync(property);
}

break;
}
case 3:

{
var properties = _csvAppService.ImportArthurCsv(input.FileBase64);
foreach (var item in properties)
{
var property = new Property
{
PropertyTypeId = 1,
BuildingTypeId = 12,
PropertyTitleId = 1,
AccountManagerId = AbpSession.TenantId,
Addresses = new List<PropertyAddress>
{
new PropertyAddress
{
Line1 = item.Line1,
Line2 = item.Line2,
Line3 = item.Line3,
PostCode = item.PostCode,
PostTown = item.City,
County = item.County,
Country = item.Country,
Latitude = item.Latitude,
Longitude = item.Longitude
}
},
Sites = new List<Site>
{
new Site
{
NumberOfWindows = 0,
NumberOfBedRooms = 0,
ApproximateYearBuilt = 1970,
PropertyAge = 50,
FuelTypeId = 1,
ParkingTypeId = 1,
FurnishingTypeId = 1
}
},
MarketingInformation = new List<PropertyMarketingInformation>
{
new PropertyMarketingInformation
{
MarketingDescription = "", IsBillsIncluded = false
}
}
};
await _propertyRepository.InsertAsync(property);
}
}

break;
}
}

如何将这 3 个方法重写为一个通用方法?

最佳答案

泛型类型参数的典型用例

public IEnumerable<T> ImportStandardCsv<T>(string fileData)
{
var bytes = Convert.FromBase64String(fileData.Substring(fileData.IndexOf(',') + 1));
using (var memoryStream = new MemoryStream(bytes))
using (var streamReader = new StreamReader(memoryStream))
using (var csvReader = new CsvReader(streamReader))
return csvReader.GetRecords<T>();
}

打电话

IEnumerable<PropertyImportDto> result = ImportStandardCsv<PropertyImportDto>(input.FileBase64);

关于c# - 在一个泛型中转换 3 个方法 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59782891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com