Generic Interface and casting from object at runtime problem

A

Anthony Paul

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
 
J

Jon Skeet [C# MVP]

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);

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.
 

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