Exemplo n.º 1
0
        public virtual void SetDefaultValue()
        {
            Type sectionType = this.GetType();

            IICConfigSectionAttribute sectionAttr = AttributeHelper.GetAttribute <IICConfigSectionAttribute>(sectionType);

            foreach (FieldInfo field in sectionType.GetFields())
            {
                IICConfigFieldAttribute fieldAttr = AttributeHelper.TryGetAttribute <IICConfigFieldAttribute>(field);
                if (fieldAttr != null && fieldAttr.DefaultValue != null)
                {
                    ObjectHelper.SetValue(field, this, fieldAttr.DefaultValue);
                    continue;
                }

                IICConfigItemAttribute itemAttr = AttributeHelper.TryGetAttribute <IICConfigItemAttribute>(field);
                if (itemAttr != null)
                {
                    IICConfigItem item = (IICConfigItem)Activator.CreateInstance(field.FieldType);
                    item.SetDefaultValue();
                    field.SetValue(this, item);
                    continue;
                }

                IICConfigItemCollectionAttribute colletionAttr = AttributeHelper.TryGetAttribute <IICConfigItemCollectionAttribute>(field);
                if (colletionAttr != null)
                {
                    object collection = Activator.CreateInstance(field.FieldType);
                    field.SetValue(this, collection);
                    continue;
                }
            }
        }
Exemplo n.º 2
0
        public virtual void SetDefaultValue()
        {
            Type itemType = this.GetType();

            foreach (FieldInfo field in itemType.GetFields())
            {
                IICConfigFieldAttribute fieldAttr = AttributeHelper.TryGetAttribute <IICConfigFieldAttribute>(field);
                if (fieldAttr != null)
                {
                    if (fieldAttr.DefaultValue != null)
                    {
                        try {
                            field.SetValue(this, fieldAttr.DefaultValue);
                        } catch (Exception ex) {
                            throw new ConfigurationException(
                                      string.Format("<{0}.{1}: {2}> DefaultValue Convert Failed: {3}",
                                                    itemType.Name,
                                                    field.Name,
                                                    field.FieldType.Name,
                                                    fieldAttr.DefaultValue),
                                      ex);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public RpcServiceDecorator(T serviceObj, string serviceName) : base(string.Empty)
        {
            Type intf = typeof(T);

            if (!intf.IsInterface)
            {
                throw new NotSupportedException();
            }

            RpcServiceAttribute serviceAttr = AttributeHelper.GetAttribute <RpcServiceAttribute>(intf);

            if (!string.IsNullOrEmpty(serviceName))
            {
                p_serviceName = serviceName;
            }
            else
            {
                p_serviceName = serviceAttr.ServiceName;
            }

            _serviceObj = serviceObj;
            _methods    = new HybridDictionary <string, RpcServiceMethod>();

            bool enableCounter = serviceAttr.EnableCounters == RpcPerformanceCounterMode.Both || serviceAttr.EnableCounters == RpcPerformanceCounterMode.Server;

            IICPerformanceCounterCategory category = new IICPerformanceCounterCategory("rpc:" + p_serviceName, PerformanceCounterCategoryType.MultiInstance);

            foreach (MethodInfo method in intf.GetMethods())
            {
                string           methodName = method.Name;
                RpcServiceMethod m;

                RpcServiceBatchMethodAttribute battr = AttributeHelper.TryGetAttribute <RpcServiceBatchMethodAttribute>(method);
                if (battr != null)
                {
                    if (!string.IsNullOrEmpty(battr.MethodName))
                    {
                        methodName = battr.MethodName;
                    }

                    m = new RpcServiceBatchMethod(serviceObj, category, methodName, method, enableCounter);
                }
                else
                {
                    RpcServiceMethodAttribute attr = AttributeHelper.GetAttribute <RpcServiceMethodAttribute>(method);
                    if (!string.IsNullOrEmpty(attr.MethodName))
                    {
                        methodName = attr.MethodName;
                    }

                    m = new RpcServiceMethod(serviceObj, category, methodName, method, enableCounter);
                }

                _methods.Add(methodName, m);
            }
            IICPerformanceCounterFactory.GetCounters(category);
        }
Exemplo n.º 4
0
        public static object Parse(string enumValue, Type enumType, string defaultValue, bool ignoreUnknown)
        {
            bool isFlags = AttributeHelper.TryGetAttribute <FlagsAttribute>(enumType) != null;

            try {
                long n = 0;
                if (enumValue.StartsWith("0x") || enumValue.StartsWith("0X"))
                {
                    string hex = enumValue.Substring(2);
                    if (hex.Length > 8)
                    {
                        throw new FormatException("Flag HexToLong: " + enumValue);
                    }

                    n = int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
                    return(Enum.ToObject(enumType, n));
                }
                else if (long.TryParse(enumValue, out n))
                {
                    return(Enum.ToObject(enumType, n));
                }
                else if (isFlags && ignoreUnknown)
                {
                    return(ParseFlags(enumValue, enumType, true));
                }
                else
                {
                    return(Enum.Parse(enumType, enumValue, true));
                }
            } catch (Exception ex) {
                if (defaultValue == null)
                {
                    throw new FormatException("EnumParse Failed:" + enumValue, ex);
                }
                else
                {
                    return(Parse(defaultValue, enumType, "", ignoreUnknown));
                }
            }
        }
Exemplo n.º 5
0
        public IICCodeTable(IICConfigTableBuffer buffer)
            : base(buffer.TableName, buffer.Version)
        {
            _innerTable = new Dictionary <K, V>();
            int columnCount = buffer.ColumnNames.Length;

            Type keyType   = typeof(K);
            Type valueType = typeof(V);
            bool simpleKey = keyType.BaseType != typeof(IICCodeTableItemKey);

            FieldInfo[] valueFields = new FieldInfo[columnCount];
            FieldInfo[] keyFields   = new FieldInfo[columnCount];
            IICCodeTableFieldAttribute[] valueAttrs = new IICCodeTableFieldAttribute[columnCount];
            IICCodeTableFieldAttribute[] keyAttrs   = new IICCodeTableFieldAttribute[columnCount];

            FieldInfo[] fields = valueType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                IICCodeTableFieldAttribute attr = AttributeHelper.TryGetAttribute <IICCodeTableFieldAttribute>(field);

                if (attr != null)
                {
                    bool find = false;
                    for (int i = 0; i < columnCount; i++)
                    {
                        if (buffer.ColumnNames[i] == attr.FieldName)
                        {
                            valueFields[i] = field;
                            valueAttrs[i]  = attr;
                            find           = true;
                            if (simpleKey && attr.IsKeyField)
                            {
                                keyFields[i] = field;
                            }
                            break;
                        }
                    }
                    if (!find && attr.IsRequired)
                    {
                        throw new ConfigurationNotFoundException(TableName, "", attr.FieldName);
                    }
                }
            }

            if (!simpleKey)
            {
                fields = keyType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                foreach (FieldInfo field in fields)
                {
                    IICCodeTableFieldAttribute attr = AttributeHelper.TryGetAttribute <IICCodeTableFieldAttribute>(field);

                    if (attr != null)
                    {
                        bool find = false;
                        for (int i = 0; i < columnCount; i++)
                        {
                            if (buffer.ColumnNames[i] == attr.FieldName)
                            {
                                keyFields[i] = field;
                                keyAttrs[i]  = attr;
                                find         = true;
                                break;
                            }
                        }
                        if (!find)
                        {
                            throw new ConfigurationNotFoundException(TableName, "", attr.FieldName);
                        }
                    }
                }
            }

            foreach (RpcClass <string[]> row in buffer.Rows)
            {
                object key   = null;
                object value = Activator.CreateInstance(valueType);

                if (!simpleKey)
                {
                    key = Activator.CreateInstance(keyType);
                }

                for (int i = 0; i < columnCount; i++)
                {
                    if (valueFields[i] == null)
                    {
                        continue;
                    }

                    string valStr = row.Value[i];
                    if (valueAttrs[i].TrimString)
                    {
                        valStr = valStr.Trim();
                    }

                    object fieldValue = ObjectHelper.ConvertTo(valStr, valueFields[i].FieldType);
                    valueFields[i].SetValue(value, fieldValue);
                    if (keyFields[i] != null)
                    {
                        if (simpleKey)
                        {
                            key = fieldValue;
                        }
                        else
                        {
                            keyFields[i].SetValue(key, fieldValue);
                        }
                    }
                }

                K k1 = (K)key;
                V v1 = (V)value;
                _innerTable.Add(k1, v1);
            }
        }