简体   繁体   中英

Visual Studio/C#: Include/merge files

In a C# project, I need to add many classes that each have 10 common properties, each one adding several specific properties. It is expected that the number of common properties will grow up to 20.

I would like to:

  1. Have a base class / file that would hold all the common properties
  2. Create those many classes without inheriting the base class and the new classes have the common properties from the base class/file.
  3. In the "derived" classes I can add new properties.
  4. In the future, when the list of common properties will grow, I would just edit the base class/file and have them inserted automatically in all "descendant" classes.

Basically, I need a placeholder inserted somewhow in my files that links to an external file and add the content into the classes at compile time, just as #include directive works in C++ .

So I need inheritance without deriving the classes, it that makes sense.

Probably the closet you can get is interface properties . You will still need to implement the properties in the "descendant" classes. It would be fairly easy to write a script yourself that implements them as auto-properties when the master file changes though.

You can easily generate your classes with CodeDOM . See that example.

You can use a compilation-time T4 text template. There you can add all your properties and from there the code is multiplied and you can even change it as you like. If you need individual code for these classes, you can just make the classes partial and add the individual code in manual files.

BUT: You will hardly get any IDE features for T4. There are some extensions that at kleast give you some syntax highlighting, but basically thats it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM