C#Partial Classes [英] C# Partial Classes

查看:181
本文介绍了C#Partial Classes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个解决方案,多个项目,大多使用相同的类。因此,在我看来,在解决方案中添加一个包含这些类的类库是一个好主意,而不是在每个项目中重复类。



但是,一个项目我需要一些特定于该项目的类的一些额外的属性,不会在任何其他地方使用。因此,我认为我应该使用部分类来添加这些属性。所以我做了这样的事情:



在类库中:

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



在项目(MyProject)中:

  namespace MyClassLibrary 
{
public partial class Book
{
public string AdditionalProperty {get ;组;我已经在MyProject中引用了MyClassLibrary(一个C#窗口)。我在MyProject中引用了MyClassLibrary(一个C#中的窗口)表单应用程序),但是当我尝试使用这个类在代码的形式我收到以下错误:


class MyClassLibrary.Book



警告:'C:...
(Project)'类型
'MyClassLibrary.Book'与导入的
在'C:...
(类库DLL)中键入'MyClassLibrary.Book'。使用'C:...(project)'中定义的
类型。


解决方案

部分不是跨越程序集。如果您需要为您的类添加更具体的使用类型,您应该创建一个派生类:

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

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



在您的项目中需要更具体的MyFoo类型,利用MySpecificFoo。因为它继承/派生自MyFoo,它将拥有MyFoo的所有属性和功能,以及附加属性。这是 多态性的一部分,这是OOP的真正力量所在。


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?

解决方案

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.

这篇关于C#Partial Classes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆