简体   繁体   English

如何在C#中组合部分类对象?

[英]How to combine a Partial Class Object in C#?

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. 我这样做是因为我将JSONed sampleA和JSONed sampleB保存到Azure表,以呈现完整的示例对象。 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应包含:

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

This has actually nothing to do with partial classes. 这实际上与部分类无关。 Partial class is a single class, which is "geographically" declared more than in one file. 部分类是单个类,它在“地理上”声明多于一个文件。

File1.cs: File1.cs:

public partial class File { public string Prop2 { get;set; } }

File2.cs: File2.cs:

public partial class File { public int Prop1 { get;set; } }

which will, when compiled, produce: 这将在编译时产生:

public partial class File 
{
     public string Prop2 { get;set; } 
     public int Prop1 { get;set; } 
}

For what you are asking. 对于你的要求。
There is no such method, which would combine two different instances into one. 没有这样的方法,它将两个不同的实例合二为一。 You should write it by your own. 你应该自己写。

UPDATE: 更新:
You may ask, why there is no such method. 你可能会问,为什么没有这样的方法。 But how would it handle situation like: 但它如何处理如下情况:

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?

How about you just overload the + operator ? 你怎么过载+ operator

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

You could accomplish this by overloading the + operator on your class. 您可以通过重载类上的+运算符来实现此目的。 Something like this 像这样的东西

public static Sample operator +(Sample sampleA, Sample sampleB){
    return new Sample() { Name = sampleA.Name, Price = sampleB.Price };
}

As Eric Yin mentioned in his answer, partial class is an inappropriate name to use as it refers to something else entirely. 正如Eric Yin在他的回答中所提到的部分类是一个不合适的名称,因为它完全指的是其他东西。

It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. 可以在两个或多个源文件上拆分类或结构,接口或方法的定义。 Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled. 每个源文件都包含类型或方法定义的一部分,并且在编译应用程序时将所有部分组合在一起。

First of all you should consider whether using the addition operator would be a conceptually good choice. 首先,您应该考虑使用加法运算符是否是概念上的好选择。 Is it obvious/uambiguous what the resulting object will be? 产生的对象是明显的/不明确的是什么?

At first sight, I would interpret the behavior you are looking for as combining two objects by using all their non-null values. 乍一看,我会通过使用所有非空值来解释您正在寻找的行为。 When both objects have the same field with a non-null value, how would you handle that? 当两个对象具有非空值的相同字段时,您将如何处理?

Considering this is the behavior you are after, you could overload the addition operator as in Gabe's answer . 考虑到这是你所追求的行为,你可以像Gabe的回答一样重载加法运算符 When your class/struct only contains nullable values you could let the addition copy all values from one object to the other where one contains a value and the other a null value. 当您的类/结构只包含可空值时,您可以让添加将所有值从一个对象复制到另一个对象,其中一个包含值,另一个包含null值。 I'd throw an exception when both objects contain a value. 当两个对象都包含值时,我会抛出异常。

As a last point, reflect on how you ended up with having to implement this behavior. 最后一点,请反思您最终必须如何实现此行为。 It's my guess there is an underlying design issue you could address instead. 我猜你可以解决一个潜在的设计问题 You probably shouldn't use objects which only store partial data. 您可能不应该使用仅存储部分数据的对象。


As another alternative the new dynamic keyword in C# also comes to mind. 作为另一种选择,C#中的新动态关键字也会浮现在脑海中。 It might be worthwhile to check that out as well. 也可能值得检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM