如何在C#中的部分类对象结合? [英] How to combine a Partial Class Object in C#?

查看:160
本文介绍了如何在C#中的部分类对象结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义一个类:

 公共类样品{
公共字符串名称{;设置;}
酒店的公共INT价格{获取;设置}
}

然后

 样品sampleA =新样品(); 
sampleA.name =测试;

样品sampleB =新样品();
sampleB.price = 100;

我这样做,因为我会JSONed sampleA和JSONed sampleB保存到Azure的表,到目前一个完整的样本对象。在其他的代码,有时只需要姓名,有时只需要代价,所以我只需要拉,每次需要的数据。在真正的代码当然,样品的结构是非常复杂的多。



我的问题是,:,有任何简单的方法,可以做到:

 样品sampleC = sampleA + sampleB; 

和sampleC应包含以下内容:

  sampleC.name =测试; 
sampleC.price = 100;


解决方案

你怎么样只是重载 + 运营商

 公共静态样品运营商+(左样品,样品右)
{
left.price = right.price;
返回离开;
}


I defined a class:

public class Sample{
    public string name {get;set;}
    public int price {get;set}
}

Then

 Sample sampleA = new Sample();
 sampleA.name = "test";

  Sample sampleB = new Sample();
  sampleB.price = 100;

I do this because I will save JSONed sampleA and JSONed sampleB to Azure table, to present a full sample object. In other code, sometime only need name, sometime only need price, so I only need to pull data that needed each time. Of course in real code, the structure of the sample is much much more complex.

My question is:, is there any easy method that can do:

  Sample sampleC = sampleA + sampleB;

and sampleC should contain:

 sampleC.name = "test";
  sampleC.price = 100;

解决方案

How about you just overload the + operator ?

    public static Sample operator +(Sample left, Sample right)
    {
        left.price = right.price;
        return left;
    }

这篇关于如何在C#中的部分类对象结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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