[英]Can I have partial classes in nuget package?
I am building a nuget
package that will be shared across many projects in the enterprise. 我正在构建一个nuget
包,该包将在企业中的许多项目之间共享。 It's essentially Entity Framework Model. 它本质上是实体框架模型。 At some point these classes were scaffolded from the database and the classes that were generated were all defined as partial (all examples are for illustration purposes only): 在某些时候,这些类已从数据库中被搭建起来,并且所生成的类都被定义为部分类(所有示例仅出于说明目的):
public partial class Customer { ... }
public partial class Order { ... }
Some projects took advantage of this partial
and developed additional functions that are beyond what database cares for: 一些项目利用了这一partial
功能,并开发了数据库所不支持的其他功能:
public partial class Customer {
// get distance from the office to customer home
public decimal Distance { get { ... } }
}
Which allows using this calculated value in LINQ statements: 允许在LINQ语句中使用此计算值:
from c in Customer where Distance < 100 select c
But now that the "core" set of classes are in nuget package, the classes are no longer partial. 但是,既然“核心”类集位于nuget包中,则这些类不再是局部的。 Is there a way to force nuget to respect partial
attribute? 有没有办法强迫nuget尊重partial
属性? I realize that it's too tight coupling; 我意识到耦合太紧密了。 but it would greatly simplify transition from "embedded" model to nuget 但这将大大简化从“嵌入式”模型到nuget的过渡
In C# you can not take advantage of partial mechanic to split the definition of a class into different assembly. 在C#中,您不能利用局部机制将类的定义拆分为不同的程序集。
Please see msdn explanation. 请参阅msdn说明。
What about extensions ? 那扩展呢? Define your method in an extension class with your methods in the same namespace. 使用相同名称空间中的方法在扩展类中定义您的方法。
Yes, its not possible. 是的,不可能。 addition to the answer of @dbraillon 除了@dbraillon的答案
partials are turned into whole classes when the compiler compiles your project. 当编译器编译您的项目时,局部变量会变成整个类。 so there is no notion of partial classes
in a dll. 因此,在dll中没有分部partial classes
概念。
So: 所以:
public partial class MyPartial {
public int FieldA;
}
public partial class MyPartial {
public int FieldB;
}
becomes the following when compiled: 编译后变为以下内容:
public class MyPartial {
public int FieldA;
public int FieldB;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.