C# Partial Classes

后端 未结 5 1147
盖世英雄少女心
盖世英雄少女心 2021-01-04 01:04

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 cont

5条回答
  •  别那么骄傲
    2021-01-04 01:45

    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.

提交回复
热议问题