C# Partial Classes

回眸只為那壹抹淺笑 提交于 2019-11-30 17:19:46

Partials are not for spanning assemblies. If you need to add to your class for a more specific type of usage, you should create a derived class:

public class MyFoo
{
    public string BasicProperty {get;set;}
}

public class MySpecificFoo : MyFoo
{
    public string AnotherProperty {get;set;}
}

In your project requiring the more specific type of MyFoo, utilize MySpecificFoo instead. Since it inherits/derives from MyFoo, it will have all of the properties and functionality of MyFoo, with the additional properties as well. This is part of Polymorphism, which is where real power of OOP lies.

Nick Craver

In short, you can't use partial classes across projects. All the source must be compiled at the same time, and that's done per project.

Here's a full discussion on SO about this: Should you use a partial class across projects?

For what you're trying to do, you should instead try to use base classes and inheritance. Or even better object composition.

I think this is more along the lines of what you're trying to achieve.

Put all of your common classes into a class library project and compile it to a DLL.

You can then reference that DLL in external projects. Anytime you need to add a property to it for the external project you can then inherit the class and add the property there.

all wrong. You should consider partial methods instead. Look 'em up. They're exactly what you asked for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!