By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,919 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,919 IT Pros & Developers. It's quick & easy.

Generic Interface and casting from object at runtime problem

P: n/a
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<intintMinMax = new MinMax<int>(0, 100); // percentage range
IMinMax<ddateMinMax = 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<tmm = (IMinMax<t>) o; // still doesn't work even though I
know the type

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

Anthony

Jul 17 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Anthony Paul <an**********@gmail.comwrote:

<snip>
Now here's some code to use it :

IMinMax<intintMinMax = new MinMax<int>(0, 100); // percentage range
IMinMax<ddateMinMax = 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);
What's wrong with:

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

?

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

--
Jon Skeet - <sk***@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
Jul 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.