static CharRecognizerHelper()
        {
            _handlers = new System.Collections.Generic.Dictionary <string, RecognizerItem>(StringComparer.OrdinalIgnoreCase);
            Type baseType = typeof(ICharRecognizer);

            foreach (Type item in baseType.Assembly.GetTypes())
            {
                if (item.IsAbstract || !item.IsPublic || !item.IsClass || !TypeExtensions.IsInheritFrom(item, baseType))
                {
                    continue;
                }
                string name        = ConstAttributeExtensions.Const(item, "Name");
                string displayName = ConstAttributeExtensions.Const(item, "DisplayName");
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(displayName))
                {
                    continue;
                }
                _handlers.Add(name, new RecognizerItem()
                {
                    Name        = name,
                    DisplayName = displayName,
                    Type        = item,
                });
            }
        }
Beispiel #2
0
    /// <summary>
    /// 将当前枚举的值,变成名称串,通常用于多值的枚举。比如将 Abc.A | Abc.B 变成Abc[]{ Abc.A,Abc.B }。
    /// </summary>
    /// <param name="value">当前枚举值。</param>
    /// <param name="defineName">是否为定义名称,为false时表示特性名称。</param>
    /// <returns>返回一个值的数组。</returns>
    public static string ToName(
#if !net20
        this
#endif
        Enum value, bool defineName)
    {
        string[] values = value.ToString().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
        if (!defineName)
        {
            var type = value.GetType();
            for (int i = 0; i < values.Length; i++)
            {
                string name = ConstAttributeExtensions.Const(type.GetField(values[i]));
                if (!string.IsNullOrEmpty(name))
                {
                    values[i] = name;
                }
            }
        }
        return(string.Join(defineName ? "," : ",", values));
    }
Beispiel #3
0
    /// <summary>
    /// 获取某一个特定的属性值。
    /// </summary>
    /// <param name="value">当前枚举值。</param>
    /// <param name="key">属性名称。</param>
    /// <returns>返回属性的值,未找到时,将是string.Empty。</returns>
    public static string GetProperty(
#if !net20
        this
#endif
        Enum value, string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            key = "Text";
        }
        string[] values = value.ToString().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
        var      type   = value.GetType();

        for (int i = 0; i < values.Length; i++)
        {
            string name = ConstAttributeExtensions.Const(type.GetField(values[i]), key);
            if (!string.IsNullOrEmpty(name))
            {
                return(name);
            }
        }
        return("");
    }
Beispiel #4
0
        /// <summary>
        /// 映射DataReader当前数据记录。
        /// </summary>
        /// <param name="reader">数据读取对象。</param>
        /// <param name="type">实体类型。</param>
        /// <returns>返回映射结果。</returns>
        public static object Current(
#if !net20
            this
#endif
            IDataReader reader, Type type)
        {
            CommonException.CheckArgumentNull(reader, "reader");
            if (reader.IsClosed)
            {
                return(null);
            }
            if (type != null && (type.IsValueType || reader.GetFieldType(0) == type || (reader.FieldCount == 1 && type == typeof(string))))
            {
                //只拿第一列
                object value = reader.GetValue(0);
                if (value == DBNull.Value)
                {
                    return(TypeExtensions.DefaultValue(type));
                }
                return(TypeExtensions.Convert(value, type));
            }
            if (type != null)
            {
                if ((reader.FieldCount == 1 && (type.IsEnum || (TypeExtensions.IsNullableType(type) && TypeExtensions.GetNullableType(type).IsEnum))) || //枚举
                    (type.IsValueType || reader.GetFieldType(0) == type))     //类型匹配
                {
                    object value = reader.GetValue(0);
                    return(TypeExtensions.Convert((value == DBNull.Value ? TypeExtensions.DefaultValue(type) : value), type));
                }
            }
            Symbol.Collections.Generic.NameValueCollection <object> values = null;
            object result = null;

            if (type == null)
            {
                values = new Symbol.Collections.Generic.NameValueCollection <object>();
                result = values;
            }
            else
            {
                result = FastWrapper.CreateInstance(type);
                //IExtensibleModel extensiableModel = result as IExtensibleModel;
                //if (extensiableModel != null) {
                //    values = extensiableModel.Extendeds = new Symbol.Collections.Generic.NameValueCollection<object>();
                if (result is Symbol.Collections.Generic.NameValueCollection <object> )
                {
                    values = (Symbol.Collections.Generic.NameValueCollection <object>)result;
                    type   = null;
                }
            }

            for (int i = 0; i < reader.FieldCount; i++)
            {
                string name = reader.GetName(i);
                //int extIndex = name.IndexOf("ext_");
                //if (extIndex != -1)
                //    name = name.Substring(extIndex + 4);
                bool isJson = false;
                if (reader.GetFieldType(i) == typeof(string))
                {
                    string dataTypeName = reader.GetDataTypeName(i);
                    isJson = string.Equals(dataTypeName, "jsonb", StringComparison.OrdinalIgnoreCase) ||
                             string.Equals(dataTypeName, "json", StringComparison.OrdinalIgnoreCase);
                }

                object value = reader[i];
                if (value == DBNull.Value)
                {
                    value = null;
                }
                //if (isJson) {
                //    string text = value as string;
                //    if (!string.IsNullOrEmpty(text)) {
                //        value = JSON.Parse(text);
                //    }
                //} else
                if (reader.GetFieldType(i) == typeof(byte[]) && string.Equals(reader.GetDataTypeName(i), "timestamp", StringComparison.OrdinalIgnoreCase))
                {
                    byte[] buffer = (byte[])value;
                    Array.Reverse(buffer);
                }
                if (string.IsNullOrEmpty(name))
                {
                    if (values != null)
                    {
                        values.Add("无列名" + i, value);
                    }
                    continue;
                }
                if (type != null)
                {
                    PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                    if (property != null)
                    {
                        if (!isJson)
                        {
                            isJson = TypeExtensions.Convert <bool>(ConstAttributeExtensions.Const(property, "SaveAsJson"), false);
                        }
                        if (!isJson && reader.GetFieldType(i) == typeof(string) &&
                            (!property.PropertyType.IsValueType && property.PropertyType != typeof(string)))
                        {
                            isJson = true;
                        }
                        if (isJson && !property.PropertyType.IsValueType &&
                            property.PropertyType != typeof(string))
                        {
                            //value = Symbol.Serialization.ObjectConverter.ConvertObjectToType(value, property.PropertyType, new Serialization.JavaScriptSerializer());
                            value = JSON.ToObject(value as string, property.PropertyType);
                        }
                        else
                        {
                            value = TypeExtensions.Convert(value, property.PropertyType);
                        }
                        property.SetValue(result, value, null);
                        continue;
                    }

                    FieldInfo fieldInfo = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                    if (fieldInfo != null)
                    {
                        if (isJson && !fieldInfo.FieldType.IsValueType && fieldInfo.FieldType != typeof(string))
                        {
                            //value = Symbol.Serialization.ObjectConverter.ConvertObjectToType(value, fieldInfo.FieldType, new Serialization.JavaScriptSerializer());
                            value = JSON.ToObject(value as string, fieldInfo.FieldType);
                        }
                        else
                        {
                            value = TypeExtensions.Convert(value, fieldInfo.FieldType);
                        }

                        fieldInfo.SetValue(result, value);
                        continue;
                    }
                }
                if (values == null)
                {
                    continue;
                }
                if (value != null)
                {
lb_Json_retry:
                    if (isJson)
                    {
                        value = JSON.Parse(value as string, true);
                    }
                    else if (string.Equals(reader.GetDataTypeName(i), "char(1)", StringComparison.OrdinalIgnoreCase) ||
                             string.Equals(reader.GetDataTypeName(i), "nchar(1)", StringComparison.OrdinalIgnoreCase))
                    {
                        value = reader.GetChar(i);
                    }
                    else if (value is string)
                    {
                        string text = ((string)value).Trim();
                        if (text != null && ((text.StartsWith("{") && text.EndsWith("}")) || (text.StartsWith("[") && text.EndsWith("]"))))
                        {
                            isJson = true;
                            goto lb_Json_retry;
                        }
                    }
                }
                values[name] = value;
                if (isJson && reader.FieldCount == 1 && (type == null || type == typeof(object)))
                {
                    return(value);
                }
                //values[name] = reader.GetValue(i);
                //if (values[name] == DBNull.Value)
                //    values[name] = null;
                //else if (values[name] != null) {
                //    if (string.Equals(reader.GetDataTypeName(i), "char(1)", StringComparison.OrdinalIgnoreCase)
                //        || string.Equals(reader.GetDataTypeName(i), "nchar(1)", StringComparison.OrdinalIgnoreCase)) {
                //        values[name] = reader.GetChar(i);
                //    }
                //}
            }

            return(result);
        }