csharp 克隆 - ShallowCopy
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 克隆 - ShallowCopy相关的知识,希望对你有一定的参考价值。
/* PRODUCT */
public async Task InsertProductAsync(Product product)
{
await _productRepository.InsertAsync(product);
}
/* PRODUCT_COVERAGE_FACTOR */
public async Task<int> InsertProductCoverageFactorAndGetIdAsync(ProductCoverageFactor productCoverageFactor)
{
await _productCoverageFactorRepository.InsertAndGetIdAsync(productCoverageFactor);
return productCoverageFactor.Id;
}
public async Task InsertProductCoverageFactorsAsync(List<ProductCoverageFactor> productCoverageFactors)
{
foreach (var productCoverageFactor in productCoverageFactors)
{
var tempId = await InsertProductCoverageFactorAndGetIdAsync(productCoverageFactor);
}
}
@{
var cloneAction = Url.Action("CloneProduct", @controllerName, new { area = (string)@areaName });
}
@section PageScriptContent {
<script>
$("#clone").on("click", function () {
var data = {
__RequestVerificationToken: __RequestVerificationToken
};
var xhrParams = {
requestMethod: "POST",
url: "@cloneAction/@Model.Id/SSS",
dataType: "json",
data: data,
formToValidateObj: null,
blockElem: ".page-container",
iconOnly: false,
message: "Kopyalanıyor...",
unblockOnComplete: true
};
var xhrPromise = jqAjax(xhrParams);
xhrPromise.success(function (xhr) {
toastr.success(xhr.result.text, 'İşlem Başarılı');
//swal("Başarılı İşlem", xhr.result.text, "success");
});
});
</script>
}
[Route("~/Products/Product/CloneProduct/{sourceProductId?}/{targetProductId?}")]
[HttpPost]
[ValidateAntiForgeryToken]
[HandleException]
public async Task<ActionResult> CloneProduct(string sourceProductId, string targetProductId)
{
if (string.IsNullOrEmpty(sourceProductId) || string.IsNullOrEmpty(targetProductId))
{
throw new UserFriendlyException("Kayıt bulunamadı.");
}
Product product = await _productManager.GetProductAsync(sourceProductId);
var newProduct = Extensions.ShallowCopyEntity(product);
newProduct.Id = targetProductId;
newProduct.Plans = null;
/* clone Plans */
newProduct.Plans = new List<Plan>();
foreach (var plan in product.Plans)
{
var newPlan = Extensions.ShallowCopyEntity(plan);
newPlan.ProductId = targetProductId;
newPlan.Product = null;
newPlan.Id = 0;
/* clone PlanCoverages */
newPlan.PlanCoverages = new List<PlanCoverage>();
foreach (var planCoverage in plan.PlanCoverages)
{
var newPlanCoverage = Extensions.ShallowCopyEntity(planCoverage);
newPlanCoverage.Coverage = null;
newPlanCoverage.Plan = null;
newPlanCoverage.PlanCoverageLimits = null;
newPlanCoverage.PlanCoverageNetworks = null;
newPlanCoverage.Id = 0;
newPlanCoverage.PlanId = 0;
/* clone PlanCoverageLimits */
newPlanCoverage.PlanCoverageLimits = new List<PlanCoverageLimit>();
foreach (var planCoverageLimit in planCoverage.PlanCoverageLimits)
{
var newPlanCoverageLimit = Extensions.ShallowCopyEntity(planCoverageLimit);
newPlanCoverageLimit.PlanCoverage = null;
newPlanCoverageLimit.Id = 0;
newPlanCoverage.PlanCoverageLimits.Add(newPlanCoverageLimit);
}
/* clone PlanCoverageNetworks */
newPlanCoverage.PlanCoverageNetworks = new List<PlanCoverageNetwork>();
foreach (var planCoverageNetwork in planCoverage.PlanCoverageNetworks)
{
var newPlanCoverageNetwork = Extensions.ShallowCopyEntity(planCoverageNetwork);
newPlanCoverageNetwork.PlanCoverage = null;
newPlanCoverageNetwork.Id = 0;
newPlanCoverage.PlanCoverageNetworks.Add(newPlanCoverageNetwork);
}
newPlan.PlanCoverages.Add(newPlanCoverage);
}
newProduct.Plans.Add(newPlan);
}
/* clone ProductCoverages */
newProduct.ProductCoverages = new List<ProductCoverage>();
foreach (var coverage in product.ProductCoverages)
{
var newCoverage = Extensions.ShallowCopyEntity(coverage);
newCoverage.ProductId = targetProductId;
newCoverage.Product = null;
newCoverage.Coverage = null;
newCoverage.Id = 0;
newProduct.ProductCoverages.Add(newCoverage);
}
/* insert the Product super object */
await _productManager.InsertProductAsync(newProduct);
/* Clone ProductCoverageFactors */
List<ProductCoverageFactor> productCoverageFactors =
await _productManager.GetProductCoverageFactorsByProductIdAsync(sourceProductId);
var newProductCoverageFactors = new List<ProductCoverageFactor>();
foreach (var productCoverageFactor in productCoverageFactors)
{
var newProductCoverageFactor = Extensions.ShallowCopyEntity(productCoverageFactor);
newProductCoverageFactor.ProductId = targetProductId;
newProductCoverageFactor.Product = null;
newProductCoverageFactor.Coverage = null;
newProductCoverageFactor.Factor = null;
newProductCoverageFactors.Add(newProductCoverageFactor);
}
/* insert the list of ProductCoverageFactors */
await _productManager.InsertProductCoverageFactorsAsync(newProductCoverageFactors);
var resultDict = new Dictionary<string, string>
{
{ "text", $"{targetProductId} kodlu ürün kaydı başarıyla yaratıldı." },
{ "id", targetProductId }
};
return Json(resultDict);
}
public static TEntity ShallowCopyEntity<TEntity>(TEntity source) where TEntity : class, new()
{
// Get properties from EF that are read/write and not marked witht he NotMappedAttribute
var sourceProperties = typeof(TEntity)
.GetProperties()
.Where(p => p.CanRead && p.CanWrite
//&& p.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.NotMappedAttribute), true).Length == 0)
);
var newObj = new TEntity();
foreach (var property in sourceProperties)
{
// Copy value
property.SetValue(newObj, property.GetValue(source, null), null);
}
return newObj;
}
以上是关于csharp 克隆 - ShallowCopy的主要内容,如果未能解决你的问题,请参考以下文章