Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generics : how to use x.MaxValue / x.MinValue (int, float, double) in a generic class

I'm using the WPF Extended Toolkit ( http://wpftoolkit.codeplex.com/ ).

It has a nice NumericUpDown control that I'd like to use, but internally it uses doubles - which means it uses double.MinValue and double.MaxValue.

I'd like to use the same control, but I need a generic version - for ints it needs to use int.MaxValue/MinValue, for floats float.MaxValue/MinValue, etc. (I think you get the idea :))

So I though about copying the NumericUpDown to a GNumericUpDown, where T would ofcourse be the Type.. But this doesn't work, because a generic Type doesn't have MinValue / MaxValue. And normally I'd use the 'where' clause to specify a base-type, but this doesn't work as afaik there's no common interface that defines 'MinValue' and 'MaxValue'.

Is there a way to solve this with generics, or do I really need to copy/paste/search&replace the original NumericUpDown for each type ?

like image 494
Pygmy Avatar asked Dec 11 '10 19:12

Pygmy


1 Answers

Well, given that you can get at the type at execution time, you could rely on the fact that all of the numeric types in .NET have MinValue and MaxValue fields, and read them with reflection. It wouldn't be terribly nice, but easy enough to do:

using System;
using System.Reflection;

// Use constraints which at least make it *slightly* hard to use
// with the wrong types...
public class NumericUpDown<T> where T : struct,
    IComparable<T>, IEquatable<T>, IConvertible
{
    public static readonly T MaxValue = ReadStaticField("MaxValue");
    public static readonly T MinValue = ReadStaticField("MinValue");

    private static T ReadStaticField(string name)
    {
        FieldInfo field = typeof(T).GetField(name,
            BindingFlags.Public | BindingFlags.Static);
        if (field == null)
        {
            // There's no TypeArgumentException, unfortunately. You might want
            // to create one :)
            throw new InvalidOperationException
                ("Invalid type argument for NumericUpDown<T>: " +
                 typeof(T).Name);
        }
        return (T) field.GetValue(null);
    }
}

class Test
{
    static void Main()
    {
        Console.WriteLine(NumericUpDown<int>.MaxValue); 
        Console.WriteLine(NumericUpDown<float>.MinValue);
    }
}

Note that if you use this with an inappropriate type, I've tried to force a compile-time error as best I can... but it won't be foolproof. If you manage to find a structure with all the right interfaces but without MinValue and MaxValue fields, then any attempt to use the NumericUpDown with that type will cause an exception to be thrown.

like image 121
Jon Skeet Avatar answered Oct 23 '22 02:10

Jon Skeet