From: Sheng Jiang[MVP] on 18 Jul 2007 13:49 in Managed C++, you do not have to lose static type information when you box a value. This is something that C# does not provide. -- Sheng Jiang Microsoft MVP in VC++ "Jon Skeet [C# MVP]" <skeet(a)pobox.com> wrote in message news:MPG.2107c3ce9af85eaf2f1(a)msnews.microsoft.com... > Sheng Jiang[MVP] <sheng_jiang(a)hotmail.com.discuss> wrote: > > It seems he did not specify the type for the generics > > this program runs fine > > That still doesn't mean that boxing loses type information as you > claimed. > > > static void Main(string[] args) > > { > > IMinMax<int> intMinMax = new MinMax<int>(0, 100); // percentage > > range > > IMinMax<DateTime> dateMinMax = new MinMax<DateTime>(new > > DateTime(1973, 10, > > 4), DateTime.Now); // date range > > DoSomething<int>(intMinMax); > > DoSomething<DateTime>(dateMinMax); > > > > } > > public static void DoSomething<T>(object o) > > { > > IMinMax<T> mm = (IMinMax<T>) o; > > > > // do something with mm.Min and mm.Max here > > } > > Yes, but if he changed DoSomething<T> to accept IMinMax<T> directly, > there'd be no need for the cast, he'd gain more compile-time type > safety, and he wouldn't have to specify the type parameter at the call > site. > > -- > Jon Skeet - <skeet(a)pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too
From: Sheng Jiang[MVP] on 18 Jul 2007 13:50 http://msdn2.microsoft.com/en-us/library/ms379617(VS.80).aspx#vs05cplus_topic7 not related to this case though -- Sheng Jiang Microsoft MVP in VC++ "Jon Skeet [C# MVP]" <skeet(a)pobox.com> wrote in message news:MPG.2107c3ce9af85eaf2f1(a)msnews.microsoft.com... > Sheng Jiang[MVP] <sheng_jiang(a)hotmail.com.discuss> wrote: > > It seems he did not specify the type for the generics > > this program runs fine > > That still doesn't mean that boxing loses type information as you > claimed. > > > static void Main(string[] args) > > { > > IMinMax<int> intMinMax = new MinMax<int>(0, 100); // percentage > > range > > IMinMax<DateTime> dateMinMax = new MinMax<DateTime>(new > > DateTime(1973, 10, > > 4), DateTime.Now); // date range > > DoSomething<int>(intMinMax); > > DoSomething<DateTime>(dateMinMax); > > > > } > > public static void DoSomething<T>(object o) > > { > > IMinMax<T> mm = (IMinMax<T>) o; > > > > // do something with mm.Min and mm.Max here > > } > > Yes, but if he changed DoSomething<T> to accept IMinMax<T> directly, > there'd be no need for the cast, he'd gain more compile-time type > safety, and he wouldn't have to specify the type parameter at the call > site. > > -- > Jon Skeet - <skeet(a)pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too
From: Jon Skeet [C# MVP] on 18 Jul 2007 13:54 Sheng Jiang[MVP] <sheng_jiang(a)hotmail.com.discuss> wrote: > in Managed C++, you do not have to lose static type information when you box > a value. This is something that C# does not provide. Ah, you mean that C++ exposes the boxed type separately from the unboxed type? Nice. -- Jon Skeet - <skeet(a)pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
From: Anthony Paul on 19 Jul 2007 15:32 Hello Sheng! Thanks for replying... The problem with the solution you suggested is that I would have to modify the DoSomething procedure to accept a generic type. My question was (and given that *exact* scenario I described) how would I go about using a generic object after it has been cast to an object and passed as a parameter via a method that knows nothing about the generic type? I was even generous enough to allow for the possibility of me already knowing the generic type, but not passed in as a generic type to the method but through a parameter of type Type as in the following : public void DoSomething(object o, Type t) This, of course, is a horrible example but given this exact scenario, how would I be able to cast o back to its original form and use it? And how would be go about doing the same if we didn't happen to have the luxury of knowing the generic type as in the following : public void DoSomething(object o) { // how do I cast o to the appropriate IMinMax<T> and use it here? } Regards, Anthony On Jul 17, 7:01 pm, "Sheng Jiang[MVP]" <sheng_ji...(a)hotmail.com.discuss> wrote: > It seems he did not specify the type for the generics > this program runs fine > static void Main(string[] args) > { > IMinMax<int> intMinMax = new MinMax<int>(0, 100); // percentage > range > IMinMax<DateTime> dateMinMax = new MinMax<DateTime>(new > DateTime(1973, 10, > 4), DateTime.Now); // date range > DoSomething<int>(intMinMax); > DoSomething<DateTime>(dateMinMax); > > } > public static void DoSomething<T>(object o) > { > IMinMax<T> mm = (IMinMax<T>) o; > > // do something with mm.Min and mm.Max here > } > > -- > Sheng Jiang > Microsoft MVP in VC++ > "Jon Skeet [C# MVP]" <sk...(a)pobox.com> wrote in messagenews:MPG.210753e322b7b8832f0(a)msnews.microsoft.com... > > > > > Sheng Jiang[MVP] <sheng_ji...(a)hotmail.com.discuss> wrote: > > > Your struct (or value type) is boxed and the type information seems lost > > > when it is converted to object.. > > > No, type information is certainly *not* lost when it's boxed. Just try > > unboxing something to the wrong type - you'll find out soon enough. > > > -- > > Jon Skeet - <sk...(a)pobox.com> > >http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet > > If replying to the group, please do not mail me too- Hide quoted text - > > - Show quoted text -
From: Anthony Paul on 19 Jul 2007 15:46 Hello Jon, In reply to your post on the other duplicate thread (I had no idea it was posted twice) as to why I didn't pass the generic type via the generic method call : public void DoSomething<T>(object o) I can't do this because it mean making an assumption that object "o" is of a certain generic type when that may not be the case. What if it's just a regular old int? or a string? It could be an IMinMax<T> in which case the T would come in handy, but what if it was a... let's say, ISeries<T, U, V> ?? Regards, Anthony
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: DllImport and Control Library project Next: finding the closest value in a SortedDictionary |