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


C# Serialization.DataContractResolver类代码示例

本文整理汇总了C#中System.Runtime.Serialization.DataContractResolver的典型用法代码示例。如果您正苦于以下问题:C# DataContractResolver类的具体用法?C# DataContractResolver怎么用?C# DataContractResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


DataContractResolver类属于System.Runtime.Serialization命名空间,在下文中一共展示了DataContractResolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TryResolveType

        // Used at serialization
        // Maps any Type to a new xsi:type representation
        /// <summary>
        /// 序列化时将类确定传输用的类型名称
        /// </summary>
        /// <param name="type"></param>
        /// <param name="declaredType"></param>
        /// <param name="knownTypeResolver"></param>
        /// <param name="typeName"></param>
        /// <param name="typeNamespace"></param>
        /// <returns></returns>
        public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            typeName = new XmlDictionaryString(XmlDictionary.Empty, "", 0);
            typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, "", 0);

            //var dataContract = dataContractType.GetCustomAttributes(typeof(DataContractAttribute), false).FirstOrDefault() as DataContractAttribute;
            //if (dataContract == null)
            //{
            //    return false;
            //}
            try
            {

                string name = type.Name;
                string namesp = type.Namespace;
                typeName = CreateXmlDicString(name);
                typeNamespace = CreateXmlDicString(namesp);
                if (!dictionary.ContainsKey(name))
                {
                    dictionary.Add(name, typeName);
                }
                if (!dictionary.ContainsKey(namesp))
                {
                    dictionary.Add(namesp, typeNamespace);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
开发者ID:fr4nky80,项目名称:LearnCSharp,代码行数:43,代码来源:MyDataContractResolver.cs

示例2: TryResolveType

        public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            var res = knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace);
            if (res)
                return res;
            System.Reflection.Assembly asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(p => p == dataContractType.Assembly);
            if (asm != null)
            {
                XmlDictionary dictionary = new XmlDictionary();
                typeName = dictionary.Add(dataContractType.Name);
                typeNamespace = dictionary.Add("http://tempuri.com/" + asm.FullName);
                return true;
            }
            foreach (IPlugin plugin in PluginHelper.plugins.Values)
            {
                if (plugin.GetType().Assembly == dataContractType.Assembly){
                    XmlDictionary dictionary = new XmlDictionary();
                    typeName = dictionary.Add(dataContractType.Name);
                    typeNamespace = dictionary.Add("http://tempuri.com/"+ plugin.Name);
                    return true;
                }
            }

            return false;
        }
开发者ID:amoraller,项目名称:AptekaAutoOrder,代码行数:25,代码来源:MyDataContractResolver.cs

示例3: ResolveName

        /// <summary>
        /// 反序列化时通过解析类型名称得到类型
        /// Allows users to map xsi:type name to any Type 
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="typeNamespace"></param>
        /// <param name="declaredType"></param>
        /// <param name="knownTypeResolver"></param>
        /// <returns></returns>
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            var fullName = typeNamespace + "." + typeName;
            if (declaredType.FullName == fullName)
                return declaredType;
            else
                return this.assembly.GetType(fullName);




            //XmlDictionaryString tName;
            //XmlDictionaryString tNamespace;
            //if (!(dictionary.TryGetValue(typeName, out tName) && dictionary.TryGetValue(typeNamespace, out tNamespace)))
            //{

            //    var dtoType = this.assembly.GetType(fullName);
            //    if (dtoType != null)
            //    {
            //        if (!dictionary.ContainsKey(typeName))
            //        {
            //            tName = CreateXmlDicString(typeName);
            //            dictionary.Add(typeName, tName);
            //        }
            //        if (!dictionary.ContainsKey(typeNamespace))
            //        {
            //            tNamespace = CreateXmlDicString(typeNamespace);
            //            dictionary.Add(typeNamespace, tNamespace);
            //        }
            //    }               
            //}

        }
开发者ID:fr4nky80,项目名称:LearnCSharp,代码行数:42,代码来源:MyDataContractResolver.cs

示例4: WriteObjectHandleExceptions

 internal void WriteObjectHandleExceptions(XmlWriterDelegator writer, object graph, DataContractResolver dataContractResolver)
 {
     try
     {
         CheckNull(writer, "writer");
         if (DiagnosticUtility.ShouldTraceInformation)
         {
             TraceUtility.Trace(TraceEventType.Information, TraceCode.WriteObjectBegin,
                 SR.GetString(SR.TraceCodeWriteObjectBegin), new StringTraceRecord("Type", GetTypeInfo(GetSerializeType(graph))));
             InternalWriteObject(writer, graph, dataContractResolver);
             TraceUtility.Trace(TraceEventType.Information, TraceCode.WriteObjectEnd,
                 SR.GetString(SR.TraceCodeWriteObjectEnd), new StringTraceRecord("Type", GetTypeInfo(GetSerializeType(graph))));
         }
         else
         {
             InternalWriteObject(writer, graph, dataContractResolver);
         }
     }
     catch (XmlException ex)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorSerializing, GetSerializeType(graph), ex), ex));
     }
     catch (FormatException ex)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(GetTypeInfoError(SR.ErrorSerializing, GetSerializeType(graph), ex), ex));
     }
 }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:27,代码来源:XmlObjectSerializer.cs

示例5: TryResolveType

        public override bool TryResolveType(
			Type type, Type declaredType, DataContractResolver knownTypeResolver,
			out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            //			bool r = false;
            //			if (declaredType.IsSubclassOf(typeof(System.Linq.IQueryable)))
            //			{
            //				typeName = _typeDictionary.Add(type.Name);
            //				typeNamespace = _typeDictionary.Add(type.Namespace);
            //				r = true;
            //			}
            //			else
            //			{
            if (!type.IsPrimitive)
            {
                typeName = _typeDictionary.Add(GetTypeName(type));
                typeNamespace = _typeDictionary.Add(type.Namespace);
            //				r = true;
            //			}
            //			if (r)
            //			{
                string typeKey = typeName + "." + typeNamespace;
                if (!_typeNames.ContainsKey(typeKey))
                    _typeNames.Add(typeKey, type);
                return true;
            }
            else
                return knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace);
        }
开发者ID:jbowwww,项目名称:ArtefactService,代码行数:29,代码来源:WCFTypeResolver.cs

示例6: ResolveName

        /// <summary>
        /// Tries to get a type by name.
        /// Is used for deserialization.
        /// Gets called by the framework.
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="typeNamespace"></param>
        /// <param name="declaredType"></param>
        /// <param name="knownTypeResolver"></param>
        /// <returns></returns>
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            Logger.Debug(String.Format("BusDataContractResolver: ResolveName(typeName='{0}', typeNamespace='{1}', declaredType='{2}')", typeName, typeNamespace, declaredType.FullName));

            Type type = ServiceHelper.GetServiceValidType(typeName);

            if (type == null)
            {
                Logger.Debug(String.Format("BusDataContractResolver: ResolveName() got invalid type: '{0}'. Trying to get it from declared type: '{1}'.", typeName, declaredType.FullName));
                if (ServiceHelper.IsServiceValidType(declaredType))
                {
                    Logger.Warn(String.Format("BusDataContractResolver: ResolveName() was successful using declared type: '{0}.", declaredType));
                    type = declaredType;
                }
            }

            if(type != null)
            {
                Logger.Debug(String.Format("BusDataContractResolver: ResolveName() got valid type: '{0}'.", typeName));
                return type;
            }

            Logger.Error(String.Format("BusDataContractResolver: ResolveName() got invalid type: '{0}'.", typeName));
            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        }
开发者ID:sbolofsson,项目名称:OpenBus,代码行数:35,代码来源:BusDataContractResolver.cs

示例7: TryResolveType

		public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver,
											out System.Xml.XmlDictionaryString typeName,
											out System.Xml.XmlDictionaryString typeNamespace)
		{

			if (type == null)
				throw new ArgumentNullException("type");
			if (declaredType == null)
				throw new ArgumentNullException("declaredType");
			if (knownTypeResolver == null)
				throw new ArgumentNullException("knownTypeResolver");

			if (knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace))
				return true;

			if (type.IsPrimitive && declaredType == typeof(object))
			{
				return knownTypeResolver.TryResolveType(type, type, knownTypeResolver, out typeName, out typeNamespace);
			}

			XmlDictionary dict = new XmlDictionary();

			typeNamespace = dict.Add(xmlNamespace);
			typeName = dict.Add(type.AssemblyQualifiedName);

			return true;
		}
开发者ID:aries544,项目名称:eXpand,代码行数:27,代码来源:XpandDataContractResolver.cs

示例8: TryResolveType

        public override bool TryResolveType(
            Type type,
            Type declaredType,
            DataContractResolver knownTypeResolver,
            out XmlDictionaryString typeName,
            out XmlDictionaryString typeNamespace)
        {
            if (!MessageType.AllKnownTypes.Contains(type))
            {
                typeName = null;
                typeNamespace = null;
                return false;
            }

            knownTypeResolver.TryResolveType(
                type, declaredType, knownTypeResolver, out typeName, out typeNamespace);

            if (this.NamespaceShouldBeReplaced(typeNamespace))
            {
                typeName = new XmlDictionaryString(XmlDictionary.Empty, type.Name, 0);
                typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, type.Namespace, 0);
            }

            return true;
        }
开发者ID:paulkearney,项目名称:brnkly,代码行数:25,代码来源:BusReceiverDataContractResolver.cs

示例9: TryResolveType

 public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
 {
     if (type == null)
     {
         typeName = null;
         typeNamespace = null;
         return false;
     }
     if (((declaredType != null) && declaredType.IsInterface) && CollectionDataContract.IsCollectionInterface(declaredType))
     {
         typeName = null;
         typeNamespace = null;
         return true;
     }
     DataContract dataContract = DataContract.GetDataContract(type);
     if (this.context.IsKnownType(dataContract, dataContract.KnownDataContracts, declaredType))
     {
         typeName = dataContract.Name;
         typeNamespace = dataContract.Namespace;
         return true;
     }
     typeName = null;
     typeNamespace = null;
     return false;
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:25,代码来源:KnownTypeDataContractResolver.cs

示例10: XmlObjectSerializerReadContextComplex

 internal XmlObjectSerializerReadContextComplex(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver)
     : base(serializer, rootTypeDataContract, dataContractResolver)
 {
     this.mode = SerializationMode.SharedContract;
     this.preserveObjectReferences = serializer.PreserveObjectReferences;
     this.dataContractSurrogate = serializer.DataContractSurrogate;
 }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:7,代码来源:XmlObjectSerializerReadContextComplex.cs

示例11: TryResolveType

        public override bool TryResolveType(Type type, Type declaredType,
        DataContractResolver knownTypeResolver,
        out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            if (knownTypeResolver.TryResolveType(type, declaredType,
              null, out typeName, out typeNamespace))
            return true;

              string typeNameString = null;
              if (Resolver.IsCollection(type))
              {
            TryGetCollectionTypeName(type, knownTypeResolver, out typeNameString);
              }
              else if (Resolver.IsArray(type))
              {
            TryGetArrayTypeName(type, knownTypeResolver, out typeNameString);
              }

              if (typeNameString != null)
              {
            typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, Namespaces.DEFAULT, 0);
            typeName = new XmlDictionaryString(XmlDictionary.Empty, typeNameString, 0);
            return true;
              }
              return false;
        }
开发者ID:sunoru,项目名称:PBO,代码行数:26,代码来源:Resolver.cs

示例12: TryResolveType

 /// <summary>
 /// Override this method to map a data contract type to an xsi:type name and namespace during serialization.
 /// </summary>
 /// <param name="type">The type to map.</param>
 /// <param name="declaredType">The type declared in the data contract.</param>
 /// <param name="knownTypeResolver">The known type resolver.</param>
 /// <param name="typeName">The xsi:type name.</param>
 /// <param name="typeNamespace">The xsi:type namespace.</param>
 /// <returns>
 /// true if mapping succeeded; otherwise, false.
 /// </returns>
 public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace)
 {
     if (type == typeof(Tag))
     {
         XmlDictionary dictionary = new XmlDictionary();
         typeName = dictionary.Add("Tag");
         typeNamespace = dictionary.Add(uri);
         return true;
     }
         else if (type == typeof(Entry))
     {
         XmlDictionary dictionary = new XmlDictionary();
         typeName = dictionary.Add("Entry");
         typeNamespace = dictionary.Add(uri);
         return true;
     }
     else if (type == typeof(LinkEntry))
     {
         XmlDictionary dictionary = new XmlDictionary();
         typeName = dictionary.Add("LinkEntry");
         typeNamespace = dictionary.Add(uri);
         return true;
     }
     else if (type == typeof(LinkItem))
     {
         XmlDictionary dictionary = new XmlDictionary();
         typeName = dictionary.Add("LinkItem");
         typeNamespace = dictionary.Add(uri);
         return true;
     }
     else
         return knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace);
 }
开发者ID:StevenLaw,项目名称:DataBox,代码行数:44,代码来源:DataboxResolver.cs

示例13: TryResolveType

        // Serialization
        public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace)
        {
            Type[] genericTypes = type.GetGenericArguments();

            string ns = string.Empty;
            Type genericType = null;

            foreach (Type genType in genericTypes)
            {
                if (typesByType.ContainsKey(genType) == true)
                {
                    typesByType.TryGetValue(genType, out ns);
                    genericType = genType;
                    break;
                }
            }

            if (string.IsNullOrEmpty(ns) == false)
            {
                XmlDictionary dictionary = new XmlDictionary();
                typeName = dictionary.Add(genericType.Name);
                typeNamespace = dictionary.Add(ns);

                return true;
            }
            else
            {
                return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
            }
        }
开发者ID:PeaceCode,项目名称:Elasticity,代码行数:31,代码来源:SchedulerTaskDataContractResolver.cs

示例14: ResolveName

        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            var decodedTypeName = Decode(typeName);

            Type type = Type.GetType(decodedTypeName + ", " + typeNamespace);
            if (type != null)
                return type;

            if (XmlUtility.Dom != null)
            {
                // Without explicit use of XmlUtility.Dom, there is a possibility that ServerDom.dll will not get in AppDomain.CurrentDomain.GetAssemblies(), so ResolveName will fail to resolve the type. Unexpectedly, the problem occured stochastically when the server was under heavy load.
                type = XmlUtility.Dom.GetType(decodedTypeName);
                if (type != null)
                    return type;
            }

            type = Type.GetType(decodedTypeName);
            if (type != null)
                return type;

            type =
                (from asm in AppDomain.CurrentDomain.GetAssemblies()
                 where asm.FullName.StartsWith(typeNamespace + ",")
                 let asmType = asm.GetType(decodedTypeName)
                 where asmType != null
                 select asmType).FirstOrDefault();
            if (type != null)
                return type;

            return ResolveRuntimeType(decodedTypeName);
        }
开发者ID:koav,项目名称:Rhetos,代码行数:31,代码来源:GenericDataContractResolver.cs

示例15: XmlObjectSerializerReadContextComplex

 internal XmlObjectSerializerReadContextComplex(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver)
     : base(serializer, rootTypeDataContract, dataContractResolver)
 {
     _mode = SerializationMode.SharedContract;
     _preserveObjectReferences = serializer.PreserveObjectReferences;
     _serializationSurrogateProvider = serializer.SerializationSurrogateProvider;
 }
开发者ID:nadyalo,项目名称:corefx,代码行数:7,代码来源:XmlObjectSerializerReadContextComplex.cs


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