M
Madhu Gopinathan
Hi,
I hope this is the right forum for this question.
I am extending ICollection to create a Collection Type (say
MyCollection) wherein I can control the types of objects being added to the
collection. Thus, my interface now looks like this
public interface IMyCollection : ICollection
{
void Add (string toBeAdded);
void Add (IMyObject toBeAdded);
}
The implementation (MyCollection) internally stores the collection
members as types of object IMyObject, i.e. it has the ability to convert the
string (which can be added by means of the first method) to an instance of
IMyObject.
Now when, external members wish to access members of the collection
using a foreach, I wish to have the ability to return the object depending
upon the type wished by the client. In other words, if a client added a
member to a collection using the "stringized" method, she should be able to
access the member back as a string.
In other words
IMyCollection collection = <Somehow get this>;
collection.Add ("Member");
foreach (string collectionMember in collection)
{
// Process your stuff
}
The IEnumerator implementation of MyCollection cannot handle this as I
do not know the type specified for acceptance in the foreach loop. Thus, I
am always returning an object of type IMyObject as an enumerated item.
I tried using a TypeConverter implementation on the class that
implements IMyObject so that IMyObject can be converted back to a string.
However this does not seem to work.How do I make the loop check for type
convertibility?
Am I missing something here?
Thanks in advance,
Madhu
I hope this is the right forum for this question.
I am extending ICollection to create a Collection Type (say
MyCollection) wherein I can control the types of objects being added to the
collection. Thus, my interface now looks like this
public interface IMyCollection : ICollection
{
void Add (string toBeAdded);
void Add (IMyObject toBeAdded);
}
The implementation (MyCollection) internally stores the collection
members as types of object IMyObject, i.e. it has the ability to convert the
string (which can be added by means of the first method) to an instance of
IMyObject.
Now when, external members wish to access members of the collection
using a foreach, I wish to have the ability to return the object depending
upon the type wished by the client. In other words, if a client added a
member to a collection using the "stringized" method, she should be able to
access the member back as a string.
In other words
IMyCollection collection = <Somehow get this>;
collection.Add ("Member");
foreach (string collectionMember in collection)
{
// Process your stuff
}
The IEnumerator implementation of MyCollection cannot handle this as I
do not know the type specified for acceptance in the foreach loop. Thus, I
am always returning an object of type IMyObject as an enumerated item.
I tried using a TypeConverter implementation on the class that
implements IMyObject so that IMyObject can be converted back to a string.
However this does not seem to work.How do I make the loop check for type
convertibility?
Am I missing something here?
Thanks in advance,
Madhu