Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Generic Interface and casting from object at runtime problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anthony Paul - 17 Jul 2007 22:10 GMT
Hello everyone!

Let's say that I would like a generic type that supports Min/Max
properties and can be double or integer or even datetime if need be,
something flexible.

So I go about creating the following generic interface :

*note : in reality I implement the IComparable and IEquatable generic
interfaces, constraints, and associated overriden methods, but I've
cut everything down to the bare minimum for this example.

public interface IMinMax<T>
    {
    T Min{get;}
    T Max{get;}
    }

and the following generic struct :

public struct MinMax<T> : IMinMax<T>
    {
    private readonly T min;
    private readonly T max;

    public T Min
        {
        get
            {
            return min;
            }
        }

    public T Max
        {
        get
            {
            return max;
            }
        }

    public MinMax(T min, T max)
        {
        this.min = min;
        this.max = max;
        }

    }

Now here's some code to use it :

IMinMax<int> intMinMax = new MinMax<int>(0, 100); // percentage range
IMinMax<d> dateMinMax = new MinMax<DateTime>(new DateTime(1973, 10,
4), DateTime.Now); // date range

Okay, so here's the problem... what if I have the following
procedure :

public void DoSomething(object o)
    {
    IMinMax<> mm = (IMinMax<>) o; // this doesn't work

    // do something with mm.Min and mm.Max here
    }

and I want to call the procedure as follows :

DoSomething(intMinMax);
DoSomething(dateMinMax);

How do we go about doing something with Min and Max? Obviously there's
a lot of meat missing in the code and I simplified it quite
unrealistically for the purpose of this newsgroup so please no
questions as to why I would want to do it... this comes up all the
time in one form or another.

I guess the real question is... once you've cast a generic interface
to an object, how do you go about extracting its information at run-
time? In my case I happen to know the type at runtime but the
following modified method still doesn't work :

public void DoSomething(Type t, object o)
    {
    IMinMax<t> mm = (IMinMax<t>) o; // still doesn't work even though I
know the type

    // do something with mm.Min and mm.Max here
    }

Cheers!

Anthony
Jon Skeet [C# MVP] - 17 Jul 2007 23:05 GMT
<snip>

> Now here's some code to use it :
>
[quoted text clipped - 16 lines]
> DoSomething(intMinMax);
> DoSomething(dateMinMax);

What's wrong with:

public void DoSomething<T>(IMinMax<T> mm)
{
   ...
}

?

Type inference lets you get away without specifying the T most of the
time.

Signature

Jon Skeet - <skeet@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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.