Type conversions question

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
 
M

Mattias Sjögren

Am I missing something here?

You can have multiple methods returning IEnumerators on a class. You
could for exaple have something like this

public interface IMyCollection : ICollection
{
void Add (string toBeAdded);
void Add (IMyObject toBeAdded);
IEnumerator GetStringEnumerator();
}

foreach (string collectionMember in collection.GetStringEnumerator())
{
// Process your stuff
}



Mattias
 
M

Madhu Gopinathan

Hi Mattias,
I understand what you are trying to point. However, what I would like to
achieve is to make the client unaware of the fact that the collection stores
the data using a type that is not what the user expected, i.e. in this case,
IMyObject instead of string.

Basically, I have the capability of converting a "IMyObject" to a
string. I want this capability exploited during the foreach loop wherein the
client is basically asking for a type conversion from IMyObject to string

i.e code wise at this location

-----> foreach (string collectionMember in collection)

Here, the IEnumerator will return an object of type IMyObject. The CLR
will attempt to convert this type to a string. This is where I would like to
apply my conversion. Is this possible? I would like to achieve it this way
because I do not want to add an enumerator type method (like
GetStringEnumerator) for every possible type that can be added to the
collection in the future.

Many thanks,
Madhu
 
M

Michael S

I think your trying to invent generics through interfaces.

Have a look at C#2.0, but if you're going to do your application in 1.1 I
would not try to build a smart system based on interfaces, but keep it
simple but somewhat dirty by using classes and probe their type and
downcast.

Happy Coding
- Michael S
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Do you have an implicit convertor:

public static implicit = string ( IMyObject o ) {}


cheers?


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Madhu Gopinathan said:
Hi Mattias,
I understand what you are trying to point. However, what I would like
to
achieve is to make the client unaware of the fact that the collection
stores
the data using a type that is not what the user expected, i.e. in this
case,
IMyObject instead of string.

Basically, I have the capability of converting a "IMyObject" to a
string. I want this capability exploited during the foreach loop wherein
the
client is basically asking for a type conversion from IMyObject to string

i.e code wise at this location

-----> foreach (string collectionMember in collection)

Here, the IEnumerator will return an object of type IMyObject. The CLR
will attempt to convert this type to a string. This is where I would like
to
apply my conversion. Is this possible? I would like to achieve it this way
because I do not want to add an enumerator type method (like
GetStringEnumerator) for every possible type that can be added to the
collection in the future.

Many thanks,
Madhu
 
M

Madhu Gopinathan

Hi,
I cannot have that as IMyObject is an interface and implicit converter
requires the same type in the method. In other words, the implementing class
cannot have implicit type conversions that way using the interface as its
based type.

Thanks,
Madhu
 
M

Mattias Sjögren

I understand what you are trying to point. However, what I would like to
achieve is to make the client unaware of the fact that the collection stores
the data using a type that is not what the user expected, i.e. in this case,
IMyObject instead of string.

Well how you store it internally is irrelevant as long asn you can
convert all items to the type expected by the client.

You could first convert strings (and for example store in a string
array) and then return an enumerator to that instead.


Here, the IEnumerator will return an object of type IMyObject. The CLR
will attempt to convert this type to a string. This is where I would like to
apply my conversion. Is this possible?

No, any conversion you do must happen before the item is returned by
the enumerator. You can't detect the type of the enumeration variable
the client uses, and type converters or conversion operators will not
help you here since the enumerator returns a reference of type Object.



Mattias
 
M

Madhu Gopinathan

Hi Mattias,
Well how you store it internally is irrelevant as long asn you can
convert all items to the type expected by the client.

You could first convert strings (and for example store in a string
array) and then return an enumerator to that instead.

That is exactly what I wish to achieve. Conversion of the types to what the
client expects. However, I do not wish to handle this using separate
enumerators
No, any conversion you do must happen before the item is returned by
the enumerator. You can't detect the type of the enumeration variable
the client uses, and type converters or conversion operators will not
help you here since the enumerator returns a reference of type Object.

Well, not necessarily. Rightly said by you, IEnumerator returns a reference
of a type "Object". However, the client would perform a conversion of type
in the for loop for accepting that return into some type.

In normal cases, I would return a string from IEnumerator as a ref to type
Object and the loop would re-convert the type back to string without
hassles. In my particular case, the types do not match, which would cause
the CLR to fail in conversion. Is there a way I can register a handler for
custom type conversion here before or after CLR attempts the conversion to
the type asked by the client?

I am asking this because I am aware of the existence of handlers that help
in resolution of assemblies when LoadWithPartialName() fails etc.? Is there
something on those lines for type conversions?

Thanks,
Madhu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top