SpookyET
It is stupid to have to do something like this;
Agreed!
First you should not throw a System.Exception directly, you should derive
your exceptions from System.ApplicationException or throw existing
exceptions.
set { throw new System.Exception("Property or indexer cannot be assigned
to -- it is read only");}
I would suggest throwing a System.NotSupportedException.
get { throw new System.Exception("Property or indexer cannot be read --
it is write only");}
Again I would suggest throwing a System.NotSupportedException.
Should MetaDataFileReader & MetaDataFileWriter inherit from MetaDataFile?
I would expect reader & writer to accept a MetaDataFile as a parameter,
similar to how StreamReader & StreamWriter accept a Stream as a parameter,
not inherit from MetaDataFile!
As to your original question, polymorphism just doesn't work that way.
Remember that when you define a base class you are saying that all classes
derived from this base class support these features. Which allows you to
define variables of the base class, while working with objects of the
derived class. If the base class does not "support" either getting or
setting the property, then it sounds like the property does not belong in
the base class, it sounds like it belongs in each derived class, as either
get or set.
Such as:
MetaDataFile file = New MetaDataFileReader();
Because MetaDataFile defines Path, you can use file.Path above. Because you
only have a MetaDataFile variable the compiler does not know & you should
not care that you actually have a MetaDataFileReader, MetaDataFileWriter, or
SomeOtherGreatAndWonderMetaDataFile object. As long as they inherit from
MetaDataFile polymorphism says things will work consistently. Which is where
I may throw a NotSupportedException when a derived class does not (can not)
fully support the base "contract".
Hope this helps
Jay