Generic type conversion FROM string

前端 未结 11 569
终归单人心
终归单人心 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:47

    With inspiration from the Bob's answer, these extensions also support null value conversion and all primitive conversion back and fourth.

    public static class ConversionExtensions
    {
            public static object Convert(this object value, Type t)
            {
                Type underlyingType = Nullable.GetUnderlyingType(t);
    
                if (underlyingType != null && value == null)
                {
                    return null;
                }
                Type basetype = underlyingType == null ? t : underlyingType;
                return System.Convert.ChangeType(value, basetype);
            }
    
            public static T Convert<T>(this object value)
            {
                return (T)value.Convert(typeof(T));
            }
    }
    

    Examples

                string stringValue = null;
                int? intResult = stringValue.Convert<int?>();
    
                int? intValue = null;
                var strResult = intValue.Convert<string>();
    
    0 讨论(0)
  • 2020-11-27 09:51

    You could possibly use a construct such as a traits class. In this way, you would have a parameterised helper class that knows how to convert a string to a value of its own type. Then your getter might look like this:

    get { return StringConverter<DataType>.FromString(base.Value); }
    

    Now, I must point out that my experience with parameterised types is limited to C++ and its templates, but I imagine there is some way to do the same sort of thing using C# generics.

    0 讨论(0)
  • 2020-11-27 09:52

    I used lobos answer and it works. But I had a problem with the conversion of doubles because of the culture settings. So I added

    return (T)Convert.ChangeType(base.Value, typeof(T), CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2020-11-27 09:53
    TypeDescriptor.GetConverter(PropertyObject).ConvertFrom(Value)
    

    TypeDescriptor is class having method GetConvertor which accept a Type object and then you can call ConvertFrom method to convert the value for that specified object.

    0 讨论(0)
  • 2020-11-27 09:55

    I am not sure whether I understood your intentions correctly, but let's see if this one helps.

    public class TypedProperty<T> : Property where T : IConvertible
    {
        public T TypedValue
        {
            get { return (T)Convert.ChangeType(base.Value, typeof(T)); }
            set { base.Value = value.ToString();}
        }
    }
    
    0 讨论(0)
  • 2020-11-27 09:55

    For many types (integer, double, DateTime etc), there is a static Parse method. You can invoke it using reflection:

    MethodInfo m = typeof(T).GetMethod("Parse", new Type[] { typeof(string) } );
    
    if (m != null)
    {
        return m.Invoke(null, new object[] { base.Value });
    }
    
    0 讨论(0)
提交回复
热议问题