C# Partial Classes

99封情书 提交于 2019-12-30 05:44:27

问题


I currently have a solution with multiple projects that mostly use the same classes. As a result, it appeared to me that it would be a good idea to add a class library containing these classes in the solution instead of repeating the class in each project.

However, one project I have requires a few additional properties to some of the classes that are specific to that project and will not be used anywhere else. As a result, I thought that I should use partial classes to add these properties. So I have done something like this:

In the class library:

namespace MyClassLibrary
{
    public partial class Book
    {
        public string Title { get; set; }
        public string AuthorLast { get; set; }
        public string AuthorFirst { get; set; }
        public string Publisher { get; set; }
        public string Edition { get; set; }
        public string ISBN10 { get; set; }
        public string ISBN13 { get; set; }
    }
}

In the project (MyProject):

namespace MyClassLibrary
{
    public partial class Book
    {
        public string AdditionalProperty { get; set; }
    }
}

I have referenced MyClassLibrary in MyProject (a C# windows form app), however when I try to use this class in the codebehind for the form I receive the following error:

class MyClassLibrary.Book

Warning: The type 'MyClassLibrary.Book' in 'C:... (Project)' conflicts with the imported type 'MyClassLibrary.Book' in 'C:... (Class Library DLL)'. Using the type defined in 'C:...(project)'.

Any idea what I am doing wrong or if my whole approach is bad and I should be doing something else?


回答1:


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.




回答2:


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?




回答3:


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




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/2263712/c-sharp-partial-classes

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