Question: I have a class which has a property of generic type as given below. Type T can be any number (short, int, float ...)

```
    public class EqualFilter <T> : Filter {
        private T _value;
        public override T Value {
        get {
            return _value;
        }
        set {
            if (!EqualityComparer<T>.Default.Equals(_value, value)) {
                _value = value;
                RaiseFilteringChanged();
            }
        }
    }
    .....
}

```

Now I have a client class which has a "String" that needs to be passed to the above setter. Now at the time of setting the value in the above setter, the type T is already determined at runtime and I can get hold of that type as Type T in my client. Is it possible to convert the string to the appropriate type ,as identified by the EqualFilter, in client program? Something like this that is not working

```
Type T = filter.getFilterType();
filter.Value = (T) myTextBox.Text;

```

The below code works but it will involve if-else for all the types that can be numeric.

```
Type T = filter.getFilterType();
if (T == typeof(int)) {
    filter.Value = Int32.Parse(myTextBox.Text);
} else if() {
....
}

```

In short I am not sure how to approach the issue in a better way.

Answer: 
It sounds like you're doing something similar to data binding. If all you're interested in is working with strings, you might consider constraining the generic type to `IConvertible` and calling the `Convert.ChangeType` method from a special setter method like so:

```
public class EqualFilter<T> : Filter where T : IConvertible {

    // ... omitted Value property code ...

    public void SetValue(string value)
    {
        Value = (T)Convert.ChangeType(value, typeof(T));
    }

    // ...
}

```

The generic type constraint permits only those types that [Convert.ChangeType](http://msdn.microsoft.com/en-us/library/dtb69x08.aspx) can operate upon. Besides the integral and floating point types, this also permits use of the types `string`, `decimal` and `DateTime`. Just be aware that `string` types may not always be exact when working with the `double` type as there can be round-off and floating point representation errors. Also there may be other exceptions that you may want to catch to make this more robust (`FormatException` and `OverflowException`, for instance).

```
void Main()
{
    var a = new EqualFilter<int> { Value = 10 };
    var b = new EqualFilter<double> { Value = 20 };
    b.Value = Math.PI; // RaiseFilteringChanged called - no surprise
    b.SetValue(Math.PI.ToString()); // RaiseFilteringChanged called - surprised?
    Console.WriteLine(b.Value);
    b.SetValue("25");
    Console.WriteLine(b.Value);
    var c = new EqualFilter<DateTime> { Value = DateTime.Today };
    Console.WriteLine(c.Value);
    c.SetValue("12/23/2011");
    Console.WriteLine(c.Value);

    // compiler error object isn't an IConvertible:
    // var illegal = new EqualFilter<object>();
}

```

The big switch statement, in this case, is handled by the framework in the `ChangeType` method.

**Edit:**
Adding the `Console.WriteLine` output from my version of the code above (that has overrides and base-class usage commented-out):

> 
> 3.14159265358979
> 
> 
> 25
> 
> 
> 4/10/2012 12:00:00 AM
> 
> 
> 12/23/2011 12:00:00 AM
> 
> 
>
Have you considered creating a specific setter method like this:

```
public void setValue(object o){
   if (typeof(o) == typeof(Value)) {
       Value = (typeof(Value))o;
   }
}

```