<p>I've built a wrapper over NumbericUpDown control.
The wrapper is generic and can support int? and double?</p>

<p>I would like to write a method that will do the following.</p>

<pre><code>public partial class NullableNumericUpDown&lt;T&gt; : UserControl where T : struct
{
  private NumbericUpDown numericUpDown;


  private T? Getvalue()
  {
    T? value = numericUpDown.Value as T?; // &lt;-- this is null :) thus my question
    return value;
  }}
</code></pre>

<p>of course there is no cast between decimal and double? or int? so I need to use a certain way of converting.
I would like to avoid switch or if expressions.</p>

<p>What would you do?</p>

<p>To clarify my question I've provided more code...</p>

It's not clear how you gonna use it.
If you want double create GetDouble() method, for integers - GetInteger()
EDIT:
Ok, now I think I understand your use case
Try this:
```
```
using System;
using System.ComponentModel;

static Nullable<T> ConvertFromString<T>(string value) where T:struct
{
    TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
    if (converter != null && !string.IsNullOrEmpty(value))
    {
        try
        {
            return (T)converter.ConvertFrom(value);
        }
        catch (Exception e) // Unfortunately Converter throws general Exception
        {
            return null;
        }
    }

    return null;
}

...

double? @double = ConvertFromString<double>("1.23");
Console.WriteLine(@double); // prints 1.23

int? @int = ConvertFromString<int>("100");
Console.WriteLine(@int); // prints 100

long? @long = ConvertFromString<int>("1.1");
Console.WriteLine(@long.HasValue); // prints False
```
```