Partial classes in separate dlls

2019-01-17 18:37发布

Is it possible to have two parts (same namespace, same class name) to a partial class in separate DLLs?

6条回答
Viruses.
2楼-- · 2019-01-17 19:16

No it is not possible. When the assembly is compiled the class needs to be finished.

查看更多
Juvenile、少年°
3楼-- · 2019-01-17 19:16

The question is why would you want to make a partial class in another assembly? You can define abstract classes and interfaces across assemblies, maybe you need to look into that.

查看更多
Anthone
4楼-- · 2019-01-17 19:18

From MSDN -Partial Classes and Methods:

All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.

查看更多
Fickle 薄情
5楼-- · 2019-01-17 19:21

You can use extension methods when you want to add a method to a class in a different dll. The one drawback of this method is that you cant add static methods.

查看更多
我只想做你的唯一
6楼-- · 2019-01-17 19:26

No. Partial classes are a purely language feature. When an assembly is compiled, the files are combined to create the type. It isn't possible to spread the files out into different assemblies.

Depending on what you want to do, though, you might be able to use extension methods to accomplish what you need.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-17 19:32

While other answers do provide the unpleasant "No" that anyone landing on this page didn't want to see or hear, I was struck by another thought that hasn't been mentioned here yet. If partial classes were allowed across assemblies, one would get access to private members of existing types that were not written by him, thus allowing him to manipulate them in ways that were not intended by the original author, thus jeopardizing the functionality of all inheriting classes too.

Not only that, those classes in other assemblies (and their children) would need to be recompiled to make it work. Thus it is logically not possible to allow splitting a class over different assemblies.

查看更多
登录 后发表回答