Пример #1
0
    public T ReadIni <T>(string section, string key)
    {
        StringBuilder temp = new StringBuilder(255);

        GetPrivateProfileString(section, key, "", temp, 255, filepath);
        if (string.IsNullOrEmpty(temp.ToString()))
        {
            return(default(T));
        }
        return((T)LazyConvert.ToProperty(typeof(T), temp.ToString()));
    }
Пример #2
0
    static public object ToProperty(Type type, string value)
    {
        if (type == typeof(int))
        {
            return(LazyConvert.ToInt(value));
        }
        if (type == typeof(float))
        {
            return(LazyConvert.ToFloat(value));
        }
        if (type == typeof(bool))
        {
            return(LazyConvert.ToBool(value));
        }
        if (type == typeof(DateTime))
        {
            return(LazyConvert.ToDateTime(value));
        }
        if (type == typeof(string))
        {
            return(value);
        }
        if (type.IsEnum)
        {
            return(LazyConvert.ToEnum(type, value));
        }
        if (type == typeof(long))
        {
            return(LazyConvert.ToLong(value));
        }
        if (type == typeof(Color))
        {
            return(LazyConvert.ToColor(value));
        }
        if (type.IsArray)
        {
            if (type.GetElementType() == typeof(int))
            {
                return(ToIntArray(value));
            }
            if (type.GetElementType() == typeof(float))
            {
                return(ToFloatArray(value));
            }
            if (type.GetElementType() == typeof(string))
            {
                return(value.Split(','));
            }
        }
        if (type == typeof(Char))
        {
            if (value != null && value.Length > 0)
            {
                return(value.ToCharArray()[0]);
            }
            else
            {
                return(null);
            }
        }

        return(Activator.CreateInstance(type));
    }