-
June 21st, 2009, 05:51 AM
#1
casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
hello
I need to cast KeyValuePair<X,Y> into KeyValuePair<Object, Object> ...
Trying to write simpler code... how can I reduce the number of the else-if blocks below?
Code:
if (o is DictionaryEntry)
{
DictionaryEntry oEntry = (DictionaryEntry)o;
object oKey = oEntry.Key;
object oValue = oEntry.Value;
// In this case lstAnotherCollection is a Hashtable
lstAnotherCollection.Add(oKey, oValue);
... more ...
}
else if (o is KeyValuePair<string, System.Drawing.Bitmap>)
{
// This would give error ...
// KeyValuePair<object, object> oEntry = (KeyValuePair<object, object>)o;
// To resolve this, I resorted to else-elseif blocks (That's obviously undersirable)
KeyValuePair<string, System.Drawing.Bitmap> oEntry = (KeyValuePair<string, System.Drawing.Bitmap>)o;
string oKey = oEntry.Key;
System.Drawing.Bitmap oValue = oEntry.Value;
// lstAnotherCollection = Dictionary<Object, Object>
lstAnotherCollection.Add(oKey, oValue);
... more ...
}
... many more else-if blocks ...
I suppose I cannot cast KeyValuePair<X,Y> into KeyValuePair<Object, Object> ... because it defeats purpose of Generics in the first place?
Thanks
Last edited by THY02K; June 21st, 2009 at 05:55 AM.
-
June 21st, 2009, 05:54 PM
#2
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
It's hard to give you advice without any context. If I knew what your intention was I could help more, but you are right in thinking that writing code like that is a bad idea. Why would you expect so many different types of objects there?
-
June 21st, 2009, 05:59 PM
#3
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
Usually when you find yourself having to do this kind of thing, it indicates a poor design.
-
June 21st, 2009, 07:26 PM
#4
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
yes, this is why I stated I want to eliminate this else if blocks and was looking for a solution - this is code generator code examining obj properties wiring interceptors/fabricating proxy
suggestion?
-
June 21st, 2009, 09:12 PM
#5
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
Have the KeyValuePair hold onto a base class. See if you can use polymorphism to eliminate the if/else statements.
-
June 21st, 2009, 10:33 PM
#6
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
yes - i tried casting KeyValuePair<X,Y> into KeyValuePair<Object, Object> (X and Y are Reference Type), no luck... I think I need to think of something...
-
June 22nd, 2009, 10:43 AM
#7
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
I'm not talking about down to the object level, I'm talking more on the lines of
Code:
KeyValuePair< string, MyBaseClass >
If you have too many variations for this, then you might think about changing the way you are receiving the data.
A 'catch-all' kind of approach works okay for very simple things, but doesn't work for slightly more complex things (as you have found out).
We don't have much context for what you are doing, but you mentioned code generation of proxies. Rather than building a proxy that does everything, consider making multiple proxies, specifically one proxy for group of similar types.
-
June 22nd, 2009, 07:19 PM
#8
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
Thanks... there's got to be a way... probably in arg of called method as supposed to else-if blocks as you've suggested...
-
June 22nd, 2009, 07:31 PM
#9
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
No, usually when you find yourself checking the type of an object as a condition it means that you could replace that check with polymorphic behavior. That is what Arjay was saying. For example, (this is simplistic, I know), you may have a base class for Planes:
Code:
abstract class Plane
{
abstract void Fly( );
}
All types of planes would inherit from this base class. The Fly( ) method is abstract because, although all planes fly, they do not all do so in the same manner. So, a Cessna would implement the Fly method differently than a Jumbo Jet would, but you would treat them all as instances of the Plane class and simply call the Fly( ) method on each. Each inherited type knows how to fly in their own way, but as a client of the class you don't need to know what that is.
Code:
Plane cessna = new Cessna( );
Plane jumbo = new JumboJet( );
censa.Fly( ); // calls Cessna.Fly( )
jumbo.Fly( ); // calls JumboJet.Fly( )
I know that this is overly simplistic, but you can see how this can be very powerful. Instead of checking the type of your object, each type will be encapsulated into a class and they all will share a common interface. Again, if we knew more about your situation we could tell you whether or not this was the right approach.
-
August 23rd, 2009, 04:47 AM
#10
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
Would this be an solution for your problem:
lstAnotherCollection.Add(new KeyValuePair<object, object>(o.Key, o.Value));
or
KeyValuePair<object, object> keyValuePair = new KeyValuePair<object, object>(o.Key, o.Value);
?
I dont see any reason why it should be casted, the above should work just fine.
-
August 23rd, 2009, 07:15 AM
#11
Re: casting KeyValuePair<X,Y> into KeyValuePair<Object, Object>?
.NET 4.0 allows this kind of casting via contra-variance and co-variance.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|