Eri*_*Yin 5 c# json partial-classes
我定义了一个类:
public class Sample{
public string name {get;set;}
public int price {get;set}
}
Run Code Online (Sandbox Code Playgroud)
然后
Sample sampleA = new Sample();
sampleA.name = "test";
Sample sampleB = new Sample();
sampleB.price = 100;
Run Code Online (Sandbox Code Playgroud)
我这样做是因为我将JSONed sampleA和JSONed sampleB保存到Azure表,以呈现完整的示例对象.在其他代码中,有时只需要名称,有时只需要价格,所以我只需要提取每次需要的数据.当然,在实际代码中,样本的结构要复杂得多.
我的问题是:是否有任何简单的方法可以做到:
Sample sampleC = sampleA + sampleB;
Run Code Online (Sandbox Code Playgroud)
和sampleC应包含:
sampleC.name = "test";
sampleC.price = 100;
Run Code Online (Sandbox Code Playgroud)
这实际上与部分类无关.部分类是单个类,它在"地理上"声明多于一个文件.
File1.cs:
public partial class File { public string Prop2 { get;set; } }
Run Code Online (Sandbox Code Playgroud)
File2.cs:
public partial class File { public int Prop1 { get;set; } }
Run Code Online (Sandbox Code Playgroud)
这将在编译时产生:
public partial class File
{
public string Prop2 { get;set; }
public int Prop1 { get;set; }
}
Run Code Online (Sandbox Code Playgroud)
对于你的要求.
没有这样的方法,它将两个不同的实例合二为一.你应该自己写.
更新:
您可能会问,为什么没有这样的方法.但它如何处理如下情况:
Sample sampleA = new Sample();
sampleA.name = "test";
sampleA.price = 200;
Sample sampleB = new Sample();
sampleB.price = 100;
Sample sampleC = sampleA + sampleB; // what would be the value of price here: from sampleA or sampleB?
Run Code Online (Sandbox Code Playgroud)
你只是超载怎么样+ operator?
public static Sample operator +(Sample left, Sample right)
{
left.price = right.price;
return left;
}
Run Code Online (Sandbox Code Playgroud)