dimanche 15 septembre 2019

The TypeConverter method of CanConvertFrom result in false for a byte types

I'm trying to validate if a object Value is a valid value for a property name with:

public class FooGenericClass<TEntity>
{
    public string PropertyName { get; set; }
    public object Value { get; set; }
    public bool IsValid
    {
        get
        {
            if (this.PropertyName == null || this.Value == null)
            {
                return false;
            }

            PropertyInfo propertyInfo = typeof(TEntity).GetProperty(this.PropertyName);

            if (propertyInfo != null)
            {
                Type propType = propertyInfo.PropertyType;
                TypeConverter converter = TypeDescriptor.GetConverter(propType);
                if (converter.CanConvertFrom(this.Value.GetType()))
                {
                    return true;
                }
            }

        return false;
    }
}

The IsValid works fine for int types but for byte type the CanConvertFrom returns false.

Example:

var foo1 = new FooGenericClass<BooClass>()
{
    PropertyName = "MyByte",
    Value = 1
};
var foo2 = new FooGenericClass<BooClass>()
{
    PropertyName = "MyInt",
    Value = 1
};

class BooClass
{
    byte MyByte { get; set; }
    int MyInt { get; set; }
}

The problem that foo1.IsValid getter return false, wheres foo2.IsValid return true. any suggestions?





Aucun commentaire:

Enregistrer un commentaire