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: 
You could try the following:

```
filter.Value = Convert.ChangeType(myTextBox.Text, filter.getFilterType());

```

This won't work if the conversion isn't defined though.

**Edit:**

I didn't notice your `Value` property was generic.
To make this work you could extend your filter object by something like this:

```
public object UnsafeValue
{
    set
    {
        Value = (T)value;
    }
}

```

and assign the result of `Convert.ChangeType` to that property.

Every approach without modifying the filter class would involve either more generics in the call hierarchy or reflection.
Have you considered creating a specific setter method like this:

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

```