当前位置: 首页>>代码示例>>C#>>正文


C# IScope.GetElementDef方法代码示例

本文整理汇总了C#中IScope.GetElementDef方法的典型用法代码示例。如果您正苦于以下问题:C# IScope.GetElementDef方法的具体用法?C# IScope.GetElementDef怎么用?C# IScope.GetElementDef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IScope的用法示例。


在下文中一共展示了IScope.GetElementDef方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WriteElement

        public static void WriteElement(IScope scope, IWriter writer, object obj)
        {
            var type = obj.GetType();

            var xmlWriter = writer as XmlWriterImpl;
            if (xmlWriter != null)
            {
                var surrogate = scope.GetSurrogate(type);
                if (surrogate != null)
                {
                    surrogate.Write(xmlWriter.XmlWriter, obj);
                    return;
                }
            }

            var def = scope.GetElementDef(type);
            if (def != null)
            {
                var subScope = def as IScope ?? scope;
                WriteElement(subScope, writer, obj, def, def.Name);
                return;
            }

            throw new NotSupportedException();
        }
开发者ID:sergeyt,项目名称:xserializer,代码行数:25,代码来源:Serializer.cs

示例2: ResolveElementDef

 private static IElementDef ResolveElementDef(IScope scope, IReader reader, Type type)
 {
     #if FULL
     if (reader.Format == Format.Json)
     {
         return scope.GetElementDef(type);
     }
     #endif
     return scope.GetElementDef(reader.CurrentName) ?? scope.GetElementDef(type);
 }
开发者ID:sergeyt,项目名称:xserializer,代码行数:10,代码来源:Deserializer.cs

示例3: WriteValue

        private static void WriteValue(IScope scope, IWriter writer, IPropertyDef property, XName name, object value)
        {
            if (value == null) return;

            if (value is Enum && WriteStringElement(scope, writer, property, name, value))
                return;

            if (value.IsPrimitive())
            {
                if (property.Type == typeof(object))
                {
                    writer.WriteObjectElement(name, value);
                }
                else
                {
                    writer.WritePrimitiveElement(name, value);
                }
                return;
            }

            if (WriteStringElement(scope, writer, property, name, value))
                return;

            var type = value.GetType();
            var xmlWriter = writer as XmlWriterImpl;
            if (xmlWriter != null)
            {
                var surrogate = scope.GetSurrogate(type);
                if (surrogate != null)
                {
                    surrogate.Write(xmlWriter.XmlWriter, value);
                    return;
                }
            }

            var elementDef = scope.GetElementDef(type);
            if (elementDef != null)
            {
                var subScope = elementDef as IScope;
                WriteElement(subScope, writer, value, elementDef, elementDef.Name);
                return;
            }

            var collection = value as IEnumerable;
            if (collection != null)
            {
                var itemDef = new CollectionItemDef(property.ItemName, Reflector.GetItemType(type));
                var empty = true;
                foreach (var item in collection)
                {
                    if (empty)
                    {
                        writer.WriteStartCollection(name);
                        empty = false;
                    }
                    if (item == null)
                    {
                        writer.WriteNullItem(property.ItemName);
                        continue;
                    }
                    WriteValue(scope, writer, itemDef, property.ItemName, item);
                }
                if (!empty) writer.WriteEndCollection();
                return;
            }

            throw new InvalidOperationException(string.Format("Unknown element. Name: {0}. Type: {1}.", name, type));
        }
开发者ID:sergeyt,项目名称:xserializer,代码行数:68,代码来源:Serializer.cs

示例4: ReadValue

        private static bool ReadValue(IScope scope, IReader reader, object obj, IElementDef def, IPropertyDef property, out object value)
        {
            var type = property.Type;

            if (type == typeof(object))
            {
                value = reader.ReadObject();
                return true;
            }

            if (scope.TryRead(() => reader.ReadString(), type, out value))
                return true;

            var xmlReader = reader as XmlReaderImpl;
            if (xmlReader != null)
            {
                var surrogate = scope.GetSurrogate(type);
                if (surrogate != null)
                {
                    value = CreateElement(property, obj);
                    surrogate.Read(xmlReader.XmlReader, value);
                    return true;
                }
            }

            var elementDef = scope.GetElementDef(type);
            if (elementDef != null)
            {
                var subScope = elementDef as IScope;
                value = ReadElement(subScope ?? scope, reader, elementDef, () => CreateElement(property, obj));
                return true;
            }

            if (ReadEnumElement(reader, type, out value))
                return true;

            // try read collection
            var elementType = Reflector.GetItemType(type);
            if (elementType != null)
            {
                elementDef = new CollectionDef(scope, property.Name, type, elementType);
                value = def.IsImmutable ? CreateList(elementType) : CreateElement(property, obj);
                ReadElement(scope, reader, elementDef, value);
                return true;
            }

            value = null;
            return false;
        }
开发者ID:sergeyt,项目名称:xserializer,代码行数:49,代码来源:Deserializer.cs


注:本文中的IScope.GetElementDef方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。