Generic type conversion FROM string

前端 未结 11 570
终归单人心
终归单人心 2020-11-27 09:47

I have a class that I want to use to store \"properties\" for another class. These properties simply have a name and a value. Ideally, what I would like is to be able to add

相关标签:
11条回答
  • 2020-11-27 09:59

    Yet another variation. Handles Nullables, as well as situations where the string is null and T is not nullable.

    public class TypedProperty<T> : Property where T : IConvertible
    {
        public T TypedValue
        {
            get
            {
                if (base.Value == null) return default(T);
                var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
                return (T)Convert.ChangeType(base.Value, type);
            }
            set { base.Value = value.ToString(); }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 10:02

    You can do it in one line as below:

    YourClass obj = (YourClass)Convert.ChangeType(YourValue, typeof(YourClass));
    

    Happy coding ;)

    0 讨论(0)
  • 2020-11-27 10:03
    public class TypedProperty<T> : Property
    {
        public T TypedValue
        {
            get { return (T)(object)base.Value; }
            set { base.Value = value.ToString();}
        }
    }
    

    I using converting via an object. It is a little bit simpler.

    0 讨论(0)
  • 2020-11-27 10:06

    Check the static Nullable.GetUnderlyingType. - If the underlying type is null, then the template parameter is not Nullable, and we can use that type directly - If the underlying type is not null, then use the underlying type in the conversion.

    Seems to work for me:

    public object Get( string _toparse, Type _t )
    {
        // Test for Nullable<T> and return the base type instead:
        Type undertype = Nullable.GetUnderlyingType(_t);
        Type basetype = undertype == null ? _t : undertype;
        return Convert.ChangeType(_toparse, basetype);
    }
    
    public T Get<T>(string _key)
    {
        return (T)Get(_key, typeof(T));
    }
    
    public void test()
    {
        int x = Get<int>("14");
        int? nx = Get<Nullable<int>>("14");
    }
    
    0 讨论(0)
  • 2020-11-27 10:12

    lubos hasko's method fails for nullables. The method below will work for nullables. I didn't come up with it, though. I found it via Google: http://web.archive.org/web/20101214042641/http://dogaoztuzun.com/post/C-Generic-Type-Conversion.aspx Credit to "Tuna Toksoz"

    Usage first:

    TConverter.ChangeType<T>(StringValue);  
    

    The class is below.

    public static class TConverter
    {
        public static T ChangeType<T>(object value)
        {
            return (T)ChangeType(typeof(T), value);
        }
    
        public static object ChangeType(Type t, object value)
        {
            TypeConverter tc = TypeDescriptor.GetConverter(t);
            return tc.ConvertFrom(value);
        }
    
        public static void RegisterTypeConverter<T, TC>() where TC : TypeConverter
        {
    
            TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(TC)));
        }
    }
    
    0 讨论(0)
提交回复
热议问题