Example #1
0
        private object DeserializeNodeToObject(XElement objectNode, bool ignoreError, XmlDeserializeContext context)
        {
            object data = null;

            int objID = objectNode.Attribute("id", 0);

            if (context.ObjectContext.TryGetValue(objID, out data) == false)
            {
                try
                {
                    Type type = context.GetTypeInfo(objectNode.AttributeValueWithAlterName("ownerTypeID", "oti"));

                    XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(type);

                    if (attr != null && attr.IgnoreDeserializeError == true)
                    {
                        ignoreError = attr.IgnoreDeserializeError;
                    }

                    data = CreateSerializableInstance(type);

                    context.ObjectContext.Add(objID, data);

                    if ((data is IXElementSerializable) && objectNode.Attribute("cs", false) == true)
                    {
                        ((IXElementSerializable)data).Deserialize(objectNode, context);
                    }
                    else
                    {
                        string objectValue = objectNode.Element("Value", string.Empty);

                        if (objectValue.IsNotEmpty())
                        {
                            SerializationBinder binder = this.Binder ?? UnknownTypeStrategyBinder.Instance;

                            data = SerializationHelper.DeserializeStringToObject(objectValue, SerializationFormatterType.Binary, binder);
                        }
                        else
                        {
                            DeserializeNodesToProperties(objectNode, data, context);
                        }
                    }

                    if (data is IXmlDeserialize)
                    {
                        ((IXmlDeserialize)data).AfterDeserialize(context);
                    }
                }
                catch (System.Exception)
                {
                    if (ignoreError == false)
                    {
                        throw;
                    }
                }
            }

            return(data);
        }
Example #2
0
        private static string GetAndCheckContextKey()
        {
            ActionContextDescriptionAttribute attr = AttributeHelper.GetCustomAttribute <ActionContextDescriptionAttribute>(typeof(T));

            (attr != null).FalseThrow("不能在类{0}上找到ActionContextDescriptionAttribute的定义", typeof(T).AssemblyQualifiedName);

            (attr.Key.IsNotEmpty()).FalseThrow("类{0}上ActionContextDescriptionAttribute的Key属性不能为空", typeof(T).AssemblyQualifiedName);

            return(attr.Key);
        }
        /// <summary>
        /// 通过一个字段属性创建一个对枚举项描述的实例
        /// </summary>
        /// <param name="fi">字段属性实例</param>
        /// <param name="enumType">枚举型</param>
        /// <returns>描述枚举项的实例</returns>
        /// <remarks>通过一个字段属性值创建一个枚举项描述的实例
        /// <seealso cref="MCS.Library.Core.EnumItemDescriptionAttribute"/>
        /// </remarks>
        internal static EnumItemDescription CreateFromFieldInfo(FieldInfo fi, Type enumType)
        {
            EnumItemDescription eid = new EnumItemDescription();

            eid.Name      = fi.Name;
            eid.EnumValue = (int)fi.GetValue(enumType);
            eid.SortId    = eid.EnumValue;

            eid.FillDescriptionAttributeInfo(AttributeHelper.GetCustomAttribute <EnumItemDescriptionAttribute>(fi));

            return(eid);
        }
        private void InitCountersFromAttribute(string instanceName)
        {
            PerformanceCounterDescriptionAttribute attribute = AttributeHelper.GetCustomAttribute <PerformanceCounterDescriptionAttribute>(this.GetType());

            if (attribute != null && attribute.CategoryName.IsNotEmpty() && attribute.CounterNames.IsNotEmpty())
            {
                PerformanceCounterInitData initData = new PerformanceCounterInitData(attribute.CategoryName, string.Empty, instanceName);

                string[] counterNames = attribute.CounterNames.Split(',');

                foreach (string counterName in counterNames)
                {
                    initData.CounterName = counterName;

                    this.counters[counterName] = new PerformanceCounterWrapper(initData);
                }
            }
        }
Example #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fieldInfo"></param>
        public ExtendedFieldInfo(FieldInfo fieldInfo)
        {
            MemberInfo declaringInfo = fieldInfo;

            if (fieldInfo.IsDefined(typeof(CompilerGeneratedAttribute), false))
            {
                declaringInfo = GetFieldOriginalMemberInfo(fieldInfo);
            }

            XElementFieldSerializeAttribute fieldAttr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(declaringInfo);

            if (fieldAttr != null)
            {
                this._AlternateFieldName     = fieldAttr.AlternateFieldName;
                this._IgnoreDeserializeError = fieldAttr.IgnoreDeserializeError;
            }

            this._FieldInfo = fieldInfo;
        }
Example #6
0
        private void DeserializeNodesToProperties(XElement parent, object graph, XmlDeserializeContext context)
        {
            TypeFields tf = TypeFields.GetTypeFields(graph.GetType());

            foreach (KeyValuePair <TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields)
            {
                ExtendedFieldInfo efi = kp.Value;

                if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi))
                {
                    System.Type realType = efi.FieldInfo.FieldType;

                    var propertiesElement = from property in parent.Descendants("F")
                                            where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName ||
                                                   property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) &&
                                            context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType
                                            select property;

                    if (propertiesElement.FirstOrDefault() == null)
                    {
                        propertiesElement = from property in parent.Descendants("Field")
                                            where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName ||
                                                   property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) &&
                                            context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType
                                            select property;
                    }

                    XElement propertyElement = propertiesElement.FirstOrDefault();

                    if (propertyElement != null)
                    {
                        object data = null;

                        if (propertyElement.AttributeWithAlterName("isRef", "r", false) == true)
                        {
                            XElement objectElement = GetObjectElementByID(context.RootElement, propertyElement.AttributeWithAlterName("value", "v", 0));

                            if (objectElement != null)
                            {
                                bool ignoreError = propertyElement.Attribute("ide", false);

                                if (ignoreError == false)
                                {
                                    XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(efi.FieldInfo);

                                    if (attr != null)
                                    {
                                        ignoreError = attr.IgnoreDeserializeError;
                                    }
                                }

                                data = DeserializeNodeToObject(objectElement, ignoreError, context);

                                SetValueToObject(efi.FieldInfo, graph, data);
                            }
                        }
                        else
                        {
                            data = propertyElement.AttributeWithAlterName("value", "v", TypeCreator.GetTypeDefaultValue(realType));

                            if (Convertible(realType, data))
                            {
                                SetValueToObject(efi.FieldInfo, graph, ConvertData(efi.FieldInfo, data));
                            }
                        }
                    }
                }
            }

            DeserializeXmlSerilizableList(parent, graph, context);
        }
 public static EnumItemDescriptionAttribute GetAttribute(System.Enum enumItem)
 {
     return(AttributeHelper.GetCustomAttribute <EnumItemDescriptionAttribute>(enumItem.GetType().GetField(enumItem.ToString())));
 }