Пример #1
0
        internal ObjectInfo(object receiver, Type type, string modelName)
            : this()
        {
            IsObjectContext = Attribute.GetCustomAttribute(type,
                                                           typeof(ObjectContextAttribute), false) != null;

            // 扫描对象的每个属性,根据属性所标注的特性,配置Attribute,Content和Element子项
            PropertyInfo[] properties = type.GetProperties(BIND_ATTRIBUTE);

            foreach (var property in properties)
            {
                object[] propertyAttributes = property.GetCustomAttributes(false);
                if (propertyAttributes.Length == 0)
                {
                    continue;
                }

                var attribute = (from attr in propertyAttributes
                                 where attr is SimpleAttributeAttribute
                                 select attr).FirstOrDefault();
                if (attribute != null)
                {
                    SimpleAttributeAttribute simpleAttr = attribute as SimpleAttributeAttribute;
                    simpleAttr.SetNameMode(modelName, property);
                    ObjectPropertyInfo attrInfo = new ReflectorObjectPropertyInfo(
                        property, simpleAttr, modelName);
                    fAttributes.Add(attrInfo.QName.LocalName, attrInfo);
                    continue;
                }

                attribute = (from attr in propertyAttributes
                             where (attr is TextContentAttribute || attr is ComplexContentAttribute)
                             select attr).FirstOrDefault();
                if (attribute != null)
                {
                    AssertContent(receiver, type, fContent);
                    fContent = new ReflectorObjectPropertyInfo(property,
                                                               attribute as BaseObjectAttribute, modelName);
                    continue;
                }

                attribute = (from attr in propertyAttributes
                             where attr is TagElementAttribute
                             select attr).FirstOrDefault();
                if (attribute != null)
                {
                    TagElementAttribute tagAttr = attribute.Convert <TagElementAttribute>();
                    tagAttr.SetNameMode(modelName, property);
                    fElements.Add(type, property, tagAttr, modelName);
                    tagAttr.Read(type, property, modelName, propertyAttributes);
                    continue;
                }

                fElements.ReadElementAttribute(type, property, modelName, propertyAttributes);
            }
        }
Пример #2
0
        //#region IEnumerable<ObjectPropertyInfo> 成员

        //public IEnumerator<ObjectPropertyInfo> GetEnumerator()
        //{
        //    return fElements.Values.GetEnumerator();
        //}

        //#endregion IEnumerable<ObjectPropertyInfo> 成员

        //#region IEnumerable 成员

        //IEnumerator IEnumerable.GetEnumerator()
        //{
        //    return GetEnumerator();
        //}

        //#endregion IEnumerable 成员

        //private void InternalAdd(Type type, PropertyInfo property,
        //    NamedAttribute elementAttribute, string modelName)
        //{
        //    elementAttribute.SetNameMode(modelName, property);
        //    ObjectPropertyInfo item = new ReflectorObjectPropertyInfo(property, elementAttribute, modelName);
        //    QName key = elementAttribute.GetQName(item.LocalName);
        //    try
        //    {
        //        fElements.Add(key, item);
        //        fLocalElements.Add(key.LocalName, item);
        //        ObjectPropertyInfoList list;
        //        if (fProperties.ContainsKey(property))
        //            list = fProperties[property];
        //        else
        //        {
        //            list = new ObjectPropertyInfoList(property, elementAttribute.Required);
        //            fProperties.Add(property, list);
        //        }
        //        list.Add(type, item);
        //    }
        //    catch (Exception ex)
        //    {
        //        TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
        //            "类型{0}的属性{1}中的XmlElementAttribute或者MultipleFlagAttribute的Name(值为{2})已存在,请检查",
        //            type, property.Name, key), ex, type);
        //    }
        //}

        //private void AddAttribute(SimpleElementAttribute elementAttribute, Type type,
        //    PropertyInfo property, string modelName)
        //{
        //    //ElementAttribute attr = elementAttribute as ElementAttribute;
        //    //bool addDefault = attr.DefaultValue != null && !attr.IsMultiple &&
        //    //    attr.GetType() == typeof(XmlElementAttribute);
        //    InternalAdd(type, property, elementAttribute, modelName);
        //}

        private void AddSingleAttrible(Type type, PropertyInfo property,
                                       NamedAttribute elementAttribute, string modelName)
        {
            elementAttribute.SetNameMode(modelName, property);
            ObjectPropertyInfo item = new ReflectorObjectPropertyInfo(property, elementAttribute, modelName);
            QName key = elementAttribute.GetQName(item.LocalName);

            try
            {
                fSingleElements.Add(key, item);
                fSingleLocalElements.Add(key.LocalName, item);
                fElementWriter.Add(new SingleElementWriter(item));
            }
            catch (Exception ex)
            {
                TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                            "类型{0}的属性{1}中的XmlElementAttribute或者MultipleFlagAttribute的Name(值为{2})已存在,请检查",
                                                            type, property.Name, key), ex, type);
            }
        }
Пример #3
0
        private ObjectPropertyInfo CreatePropertyInfo(ObjectElementAttribute attr)
        {
            ObjectPropertyInfo result = new ReflectorObjectPropertyInfo(fInfo, attr, fModelName);

            return(result);
        }
Пример #4
0
        internal void ReadElementAttribute(Type type, PropertyInfo property, string modelName,
                                           object[] propertyAttributes)
        {
            object dynamicAttribute = (from attr in propertyAttributes
                                       where attr is DynamicElementAttribute
                                       select attr).FirstOrDefault();

            if (dynamicAttribute != null)
            {
                DynamicElementAttribute attribute = dynamicAttribute as DynamicElementAttribute;
                BaseXmlConfigFactory    factory   = attribute.PlugInFactory;
                if (fXmlConfigFactories == null)
                {
                    fXmlConfigFactories = new Dictionary <string, BaseXmlConfigFactory>();
                }
                if (!fXmlConfigFactories.ContainsKey(factory.Name))
                {
                    fXmlConfigFactories.Add(factory.Name, factory);
                }
                //factory.AddElements(this, type, property, modelName, attribute);
                ReflectorObjectPropertyInfo objInfo = new ReflectorObjectPropertyInfo(property, attribute, modelName);
                var dyn = new ConfigFactoryElementReader(attribute, objInfo, modelName);
                fMultiElements.Add(dyn);
                fElementWriter.Add(dyn);
            }
            else
            {
                //foreach (var attr in propertyAttributes)
                //{
                //    if (attr is BaseDictionaryAttribute)
                //        InternalAdd(type, property, attr as NamedAttribute, modelName);
                //    else
                //    {
                //        SimpleElementAttribute eleAttr = attr as SimpleElementAttribute;
                //        if (eleAttr != null)
                //            AddAttribute(eleAttr, type, property, modelName);
                //    }
                //}

                var objElemAttrs = (from item in propertyAttributes
                                    where item is ObjectElementAttribute
                                    select(ObjectElementAttribute) item).ToArray();
                if (objElemAttrs.Length == 0)
                {
                    foreach (var attr in propertyAttributes)
                    {
                        if (attr is BaseDictionaryAttribute)
                        {
                            AddSingleAttrible(type, property, attr as NamedAttribute, modelName);
                        }
                        else
                        {
                            SimpleElementAttribute eleAttr = attr as SimpleElementAttribute;
                            if (eleAttr != null)
                            {
                                AddSingleAttrible(type, property, eleAttr, modelName);
                            }
                        }
                    }
                }
                else if (objElemAttrs.Length == 1)
                {
                    AddSingleAttrible(type, property, objElemAttrs[0], modelName);
                }
                else
                {
                    var multi = new MultipleElementReader(property, modelName, objElemAttrs);
                    fMultiElements.Add(multi);
                    fElementWriter.Add(multi);
                }
            }
        }