简体   繁体   中英

Can I have partial classes in nuget package?

I am building a nuget package that will be shared across many projects in the enterprise. 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:

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:

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. Is there a way to force nuget to respect partial attribute? I realize that it's too tight coupling; but it would greatly simplify transition from "embedded" model to nuget

In C# you can not take advantage of partial mechanic to split the definition of a class into different assembly.

Please see msdn explanation.

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

partials are turned into whole classes when the compiler compiles your project. so there is no notion of partial classes in a dll.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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