简体   繁体   English

我可以在nuget包中使用局部类吗?

[英]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.

相关问题 我可以调试NuGet软件包吗? - Can I Debug a NuGet Package? 鉴于 Current SendGrid Nuget 包已损坏,我如何告诉 teamCity 使用我指定的版本 - Given that the Current SendGrid Nuget package is broken, how Can I tell teamCity to use the version I have specified 我如何才能将.NET Core程序的入口点放在它引用的NuGet包中 - How can I have the entry point to a .NET Core program be in a NuGet package it references 我可以发布私有NuGet包吗? - Can I publish a private NuGet package? 排除来自 Nuget package 的类 - Excluding classes from Nuget package 我们可以在Nuget包创建中仅使用界面吗? - Can we have just interface in Nuget Package Creation? 无法安装NuGet包 - Can not install NuGet package 有一个 NuGet 包输出一个文件 - Have a NuGet package output a file 如何在未安装 Nuget 或 Visual Studio 的 Web 服务器上安装、更新、注册 Nuget package? - How do I Install, Update, Register a Nuget package on the Web Server that does not have Nuget or Visual Studio installed? 每当我在NetStandard Library Project中使用Nuget包时,是否都必须将DLL从Nuget包复制到输出目录中? - Do I have to copy the DLLs from the Nuget Package to the output directory everytime I use the Nuget Package in NetStandard Library Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM