如何将父对象复制到子对象 [英] how to copy parent object to child object

查看:103
本文介绍了如何将父对象复制到子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我的模型结构不足

hi friends,
I have below model structure

public class CollectionGroupViewModel
{
        public int TenantID { get; set; }

        public string Market { get; set; }

        public string GroupName { get; set; }

        public string GTS { get; set; }

        public string ProcessingCenter { get; set; }

}

public class EditCollectionGroupViewModel : CollectionGroupViewModel
{
        public IList<SelectListItem> ProcessingCenter { get; set; }
}



我有类创建Mock数据如下:


I have class to create Mock data as follows:

public static List<CollectionGroupViewModel> GetCollectionGroupViewList()
        {
            List<CollectionGroupViewModel> modelList = new List<CollectionGroupViewModel>();

            for (int i = 0; i < 10; i++)

            {

                CollectionGroupViewModel model = new CollectionGroupViewModel();



                model.TenantID = i + 1000;

                model.Market = "UK";

                model.GroupName = "Collection Group 1";

                model.GTS = "yes";

                model.ProcessingCenter = "Processing Centre 1";



                modelList.Add(model);

            }



            return modelList;

        }



        public static CollectionGroupViewModel GetEditCollectionGroupViewModel(int tenantID)

        {

            List<CollectionGroupViewModel> modelList = GetCollectionGroupViewList();

            EditCollectionGroupViewModel model = modelList.Where(x => x.TenantID == tenantID).Single();
            model.ProcessingCenter = ?;
            return model;
        }



我想要的是,我想将以下属性从父类复制到子类。

TenantID,Market,GroupName,GTS



我想在发送结果之前编辑以下属性。 />
model.ProcessingCenter =?;



任何想法怎么做?



提前致谢


What I want is, I want to copy the following properties from the parent class to the child class.
TenantID, Market, GroupName, GTS

And I want to edit the following property before sending the result.
model.ProcessingCenter = ?;

Any idea how to do it?

Thanks in advance

推荐答案

嗯...子类 EditCollectionGroupViewModel 派生自父 CollectionGroupViewModel 类 - 因此它已包含父级的所有属性。



除非您尝试基于父级创建新子级,否则不需要复制值。在这种情况下,我将创建一个子构造函数,它接受父项作为参数并复制值。



Um...the child class EditCollectionGroupViewModel derives from the parent CollectionGroupViewModel class - so it already contains all the properties for the parent.

You don't need to copy values, unless you are trying to create a new child based on the parent. In that case, I'd create a child constructor which accepted a parent as the parameter and copied the values.

public EditCollectionGroupViewModel(CollectionGroupViewModel parent)
   {
   TenantID = parent.TenantID; 
   ...
   }

但更好的方法是在第一个位置而不是父级创建一个子类的实例:

But a better way would be to create an instance of teh child class in teh first place instead of the parent:

for (int i = 0; i < 10; i++)
{
    CollectionGroupViewModel model = new EditCollectionGroupViewModel();

    model.TenantID = i + 1000;
    model.Market = "UK";
    model.GroupName = "Collection Group 1";
    model.GTS = "yes";
    model.ProcessingCenter = "Processing Centre 1";

    modelList.Add(model);
}


这篇关于如何将父对象复制到子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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