[Proposal] C# Feature Support Dynamic base class functionality #7960
Unanswered
wangronghua
asked this question in
Language Ideas
Replies: 1 comment 10 replies
-
This can be covered by the interface-implementing extension feature. It will allow you to "attach" interface implementation to types from other assembly. Then you can add a common interface for those types. See #7928. |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Dynamic base class functionality
For example, there is now a base class library called BaseAssembly that includes the base classes Animal and Plant. As they are intended for use in multiple projects, these two base classes are generic and cannot be changed arbitrarily.
In Project A, there are types Tiger and Elephant that inherit from Animal, and types Apple and Banana that inherit from Plant. Currently, some common properties and methods based on the current project are universal to Animal and Plant and are encapsulated in the type ProjectAOrganism
The current issue is that Tiger and Elephant not only inherit Animal but also ProjectAOrganism, while Apple and Banana not only inherit Plant but also ProjectAOrganism. Because ProjectAOrganism is strongly related to Project A, it should not be written in the base class library BaseAssembly as the base class for Animal and Plant.
The only method that comes to mind now is this:
Write two duplicate copies of ProjectAOrganismAnimal and ProjectAOrganismPlant for ProjectAOrganism,
The inheritance relationship is as follows
Tiger, Elephant=>ProjectAOrganismAnimal=>Animal
Apple, Banana=>ProjectAOrganismPlant=>Plant
The disadvantage is that two copies of the same code need to be written, and if there are other base classes that need to be used with ProjectAOrganism, another copy needs to be written, which is very redundant. Although methods can solve the redundancy problem through extension methods, it seems that there is no extension method for attributes, and they can only be implemented through redundancy as mentioned here.
If there is the following dynamic inheritance function, it can perfectly solve this problem
Beta Was this translation helpful? Give feedback.
All reactions