Is there an elegant (or any) way to achieve following in C#?
- Let's have a class
ItemBase
(further derivable toItem1
,Item2
...), which does not allow direct instantiation (non-public construction) - to prevent user to create any 'untracked' instance ofItem*
. - Let's have a non-static class
Manager
, whose instances (multiple ones allowed) only can create and provide instances ofItem*
(because they keep track of produced instances and do some additional work). - Let's have an optional requirement: The
Manager
instances would like to manipulate non-public members of the managedItem
instances (similar like theManager
would be afriend
ofItem*
). - It would be nice if the
Manager
is not forced to be derivation ofItem*
. - It would be nice if there is as little reflection as possible.
Notes:
If possible, please consider this as a question raising from process of thinking how to implement particular problem solution in a best and elegant way. I would like it to be general and no, I don't have sources and yes, I have already tried some variants, but none of them satisfied my needs. Thank you.
As far as I know, there is no acceptable
friend
alternative (any ofinternal
andInternalsVisibleToAttribute
seems to be good), so theItemBase
just provides the 'special' (but public) modification methods and the user must be aware, these methods are not for him :o(I like this solution, but I'm not able to invent, how to allow multiple
Manager
instances using it.