Thursday, 15 May 2014

c# - Setting a read-only object from a derived class -


I am writing a library which will use other developers of our company. In the State Machine Base Class, a ReadOnlyCollection & lt; T & gt; Allowed States, etc. Developers need to be successors of this class and set the allowed states.

I want to limit them to ReadOnlyCollection & gt; T & gt; Start in the manufacturer of your derivative classes and may not be able to modify it later.

If I read ReadOnlyCollection < T & gt; as a property to read only in the base class, which does not work because it can not be modified in the derivative constructor class.

I think this is not a very unusual situation. ReadOnlyCollection & lt; T & gt; ?

Do not start them automatically Make your base class constructor to take the collection as an argument:

  public class base class {safe readonly collection someObject; Public base class (collection object) {someObject = object}}   

So now when called a derived class constructor, then it should call the base object constructor also with the collection object or else it's a Compile time will be error This will ensure that the collection has been started in the constructor and nobody else.

  Public category Derivedclass: BaseClass {public DerivedClass (): base (/ * archive object pass here / /) {}}   

If you get a reference to a collection, then you can modify the collection even after adding or removing the collection method. The class is the only thing, if you can only restart it for reading .

No comments:

Post a Comment