TryResolveType() 공개 추상적인 메소드

public abstract TryResolveType ( System type, System declaredType, System knownTypeResolver, System &typeName, System &typeNamespace ) : bool
type System
declaredType System
knownTypeResolver System
typeName System
typeNamespace System
리턴 bool
예제 #1
0
		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;
		}
        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;
        }
예제 #3
0
        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);
        }
예제 #4
0
파일: Resolver.cs 프로젝트: sunoru/PBO
        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;
        }
        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;
        }
예제 #6
0
		public override bool TryResolveType (Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
		{
			//Console.WriteLine ("TryResolveType: {0} {1}", type, declaredType);
			if (knownTypeResolver.TryResolveType (type, declaredType, null, out typeName, out typeNamespace))
				return true;
			return SafeResolveType (type, out typeName, out typeNamespace);
		}
예제 #7
0
 /// <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);
 }
        // 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);
            }
        }
		public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
		{
			var result = knownTypeResolver.TryResolveType(dataContractType, declaredType, knownTypeResolver, out typeName, out typeNamespace);
			if (!result)
				return true; 
			return result; 
		}
 public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
 {
     if (!knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace))
     {
         XmlDictionary dictionary = new XmlDictionary();
         typeName = dictionary.Add(dataContractType.FullName);
         typeNamespace = dictionary.Add(dataContractType.Assembly.FullName);
     }
     return true;
 }
        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))
            {
                typeName = new XmlDictionaryString(XmlDictionary.Empty, type.Name, 0);
                typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, type.Namespace, 0);
            }

            return true;
        }
예제 #12
0
        public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            if (Identity.ContainsKey(dataContractType))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(Identity[dataContractType]);
                typeNamespace = dictionary.Add(dataContractType.FullName);
                return true;
            }

            // Defer to the known type resolver
            return knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace);
        }
예제 #13
0
 public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
 {
     if (type.Assembly.GetName().Name == DynamicTypeBuilder.AssemblyName)
     {
         XmlDictionary dic = new XmlDictionary();
         typeName = dic.Add(type.Name);
         typeNamespace = dic.Add(ResolverNamespace);
         return true;
     }
     else
     {
         return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
     }
 }
예제 #14
0
 //      These methods are necessary for standard .Net serialisation
 //      For JSon we use different means, have a look in JsonBinSerializer for its custom Json.NET handler.
 public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
 {
     if (TypeIdentity.Instance.ContainsKey(dataContractType))
     {
         XmlDictionary dictionary = new XmlDictionary();
         typeName = dictionary.Add(TypeIdentity.Instance[dataContractType].Alias);
         typeNamespace = dictionary.Add(dataContractType.FullName);
         return true; // indicating that this resolver knows how to handle
     }
     else
     {
         // Defer to the known type resolver
         return knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace);
     }
 }
		public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace)
		{
			Debug.WriteLine("TryResolveType ", type.Name);
			var retVal = _typeToNames.ContainsKey(type);
			if (retVal)
			{
				XmlDictionary dictionary = new XmlDictionary();
				typeNamespace = dictionary.Add(_typeToNames[type].Item1);
				typeName = dictionary.Add(_typeToNames[type].Item2);
			}
			else
			{
				retVal = knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
			}

			return retVal;
		}
예제 #16
0
        public override bool TryResolveType(Type type,
                                            Type declaredType,
                                            DataContractResolver knownTypeResolver,
                                            out XmlDictionaryString typeName,
                                            out XmlDictionaryString typeNamespace)
        {
            var fullName = type.FullName;

            if (_typeToAssemblyLookup.ContainsKey(fullName))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(fullName);
                typeNamespace = dictionary.Add(_xmlNamespace);
                return true;
            }

            return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
        }
        public override bool TryResolveType(
            Type type, 
            Type declaredType, 
            DataContractResolver knownTypeResolver, 
            out XmlDictionaryString typeName, 
            out XmlDictionaryString typeNamespace)
        {
            if (type.FullName.Contains("DynamicProxies"))
            {
                var name = type.Name.Split('_').First();
                var nameSpace = "myNamespace";

                typeName = new XmlDictionaryString(XmlDictionary.Empty, name, 0);
                typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, nameSpace, 0);

                return true;
            }

            return knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace);
        }
예제 #18
0
 public override bool TryResolveType(Type dataContractType, Type declaredType,
     DataContractResolver knownTypeResolver, out XmlDictionaryString typeName,
     out XmlDictionaryString typeNamespace)
 {
     if (!knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace))
     {
         return false;
     }
     int index;
     if (serializationDictionary.TryGetValue(typeNamespace.Value, out index))
     {
         typeNamespace = dic.Add(index.ToString());
     }
     else
     {
         serializationDictionary.Add(typeNamespace.Value, serializationIndex);
         typeNamespace = dic.Add(serializationIndex++ + "#" + typeNamespace);
     }
     return true;
 }
        public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace)
        {
            EntityUtil.CheckArgumentNull(dataContractType, "dataContractType");
            EntityUtil.CheckArgumentNull(declaredType, "declaredType");
            EntityUtil.CheckArgumentNull(knownTypeResolver, "knownTypeResolver");

            Type nonProxyType = ObjectContext.GetObjectType(dataContractType);
            if (nonProxyType != dataContractType)
            {
                // Type was a proxy type, so map the name to the non-proxy name
                XmlQualifiedName qualifiedName = _exporter.GetSchemaTypeName(nonProxyType);
                XmlDictionary dictionary = new XmlDictionary(2);
                typeName = new XmlDictionaryString(dictionary, qualifiedName.Name, 0);
                typeNamespace = new XmlDictionaryString(dictionary, qualifiedName.Namespace, 1);
                return true;
            }
            else 
            {
                // Type was not a proxy type, so do the default
                return knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace);
            } 
        }
 private bool ResolveType(Type objectType, Type declaredType, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
 {
     if (!DataContractResolver.TryResolveType(objectType, declaredType, KnownTypeResolver, out typeName, out typeNamespace))
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ResolveTypeReturnedFalse, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType))));
     }
     if (typeName == null)
     {
         if (typeNamespace == null)
         {
             return(false);
         }
         else
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ResolveTypeReturnedNull, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType))));
         }
     }
     if (typeNamespace == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ResolveTypeReturnedNull, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType))));
     }
     return(true);
 }
        /// <summary>During serialization, maps actual types to xsi:type information.</summary>
        /// <returns>true if the type was resolved; otherwise, false.  </returns>
        /// <param name="type">The actual type of the persistence-ignorant object.</param>
        /// <param name="declaredType">The declared type.</param>
        /// <param name="knownTypeResolver">
        ///     An instance of <see cref="T:System.Data.Entity.Core.Objects.ProxyDataContractResolver" />.
        /// </param>
        /// <param name="typeName">When this method returns, contains a list of xsi:type declarations.</param>
        /// <param name="typeNamespace">When this method returns, contains a list of namespaces used.</param>
        public override bool TryResolveType(
            Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName,
            out XmlDictionaryString typeNamespace)
        {
            Check.NotNull(type, "type");
            Check.NotNull(declaredType, "declaredType");
            Check.NotNull(knownTypeResolver, "knownTypeResolver");

            var nonProxyType = ObjectContext.GetObjectType(type);
            if (nonProxyType != type)
            {
                // Type was a proxy type, so map the name to the non-proxy name
                var qualifiedName = _exporter.GetSchemaTypeName(nonProxyType);
                var dictionary = new XmlDictionary(2);
                typeName = new XmlDictionaryString(dictionary, qualifiedName.Name, 0);
                typeNamespace = new XmlDictionaryString(dictionary, qualifiedName.Namespace, 1);
                return true;
            }
            else
            {
                // Type was not a proxy type, so do the default
                return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
            }
        }
            /// <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 XmlDictionaryString typeName,
                                                out XmlDictionaryString typeNamespace)
            {
                bool isTypeResolved = knownTypeResolver.TryResolveType(type,
                                                                       declaredType,
                                                                       knownTypeResolver,
                                                                       out typeName,
                                                                       out typeNamespace);

                if (!isTypeResolved)
                {
                    Type ensuredType = EnsureModelType(type);

                    isTypeResolved = (ensuredType == declaredType) ||
                                     knownTypeResolver.TryResolveType(ensuredType,
                                                                      declaredType,
                                                                      knownTypeResolver,
                                                                      out typeName,
                                                                      out typeNamespace);
                }

                return isTypeResolved;
            }
        public override void WriteStartObject(
            XmlDictionaryWriter writer, object graph)
        {
            Type rootType = type;

            if (root_name.Value == "")
            {
                throw new InvalidDataContractException("Type '" + type.ToString() +
                                                       "' cannot have a DataContract attribute Name set to null or empty string.");
            }


            if (graph == null)
            {
                if (names_filled)
                {
                    writer.WriteStartElement(root_name.Value, root_ns.Value);
                }
                else
                {
                    writer.WriteStartElement(root_name, root_ns);
                }
                writer.WriteAttributeString("i", "nil", XmlSchema.InstanceNamespace, "true");
                return;
            }

            QName rootQName = null;
            XmlDictionaryString name, ns;

            if (DataContractResolver != null && DataContractResolver.TryResolveType(graph.GetType(), type, default_resolver, out name, out ns))
            {
                rootQName = new QName(name.Value, ns.Value);
            }

            // It is error unless 1) TypeResolver resolved the type name, 2) the object is the exact type, 3) the object is known or 4) the type is primitive.

            if (rootQName == null &&
                graph.GetType() != type &&
                IsUnknownType(graph.GetType()))
            {
                throw new SerializationException(String.Format("Type '{0}' is unexpected. The type should either be registered as a known type, or DataContractResolver should be used.", graph.GetType()));
            }

            QName instName = rootQName;

            rootQName = rootQName ?? known_types.GetQName(rootType);
            QName graph_qname = known_types.GetQName(graph.GetType());

            known_types.Add(graph.GetType());

            if (names_filled)
            {
                writer.WriteStartElement(root_name.Value, root_ns.Value);
            }
            else
            {
                writer.WriteStartElement(root_name, root_ns);
            }

            if (rootQName != graph_qname || rootQName.Namespace != KnownTypeCollection.MSSimpleNamespace && !rootType.IsEnum)
            {
                //FIXME: Hack, when should the "i:type" be written?
                //Not used in case of enums
                writer.WriteXmlnsAttribute("i", XmlSchema.InstanceNamespace);
            }

            if (root_ns.Value != rootQName.Namespace)
            {
                if (rootQName.Namespace != KnownTypeCollection.MSSimpleNamespace)
                {
                    writer.WriteXmlnsAttribute(null, rootQName.Namespace);
                }
            }

            if (rootQName == graph_qname)
            {
                return;
            }

            /* Different names */
            known_types.Add(rootType);

            instName = instName ?? KnownTypeCollection.GetPredefinedTypeName(graph.GetType());
            if (instName == QName.Empty)
            {
                /* Not a primitive type */
                instName = graph_qname;
            }
            else
            {
                /* FIXME: Hack, .. see test WriteObject7 () */
                instName = new QName(instName.Name, XmlSchema.Namespace);
            }

            /* // disabled as it now generates extraneous i:type output.
             *                      // output xsi:type as rootType is not equivalent to the graph's type.
             *                      writer.WriteStartAttribute ("i", "type", XmlSchema.InstanceNamespace);
             *                      writer.WriteQualifiedName (instName.Name, instName.Namespace);
             *                      writer.WriteEndAttribute ();
             */
        }
        public override bool TryResolveType(Type type,
                                            Type declaredType,
                                            DataContractResolver knownTypeResolver,
                                            out XmlDictionaryString typeName,
                                            out XmlDictionaryString typeNamespace)
        {
            if (type == typeof(SdkMessages))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(Names.SdkMessages);
                typeNamespace = dictionary.Add(CrmSvcUtilNamespace);
                return true; // indicating that this resolver knows how to handle "SdkMessages"
            }
            if (type == typeof(SdkMessage))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(Names.SdkMessage);
                typeNamespace = dictionary.Add(CrmSvcUtilNamespace);
                return true; // indicating that this resolver knows how to handle "SdkMessages"
            }
            if (type == typeof(SdkMessagePair))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(Names.SdkMessagePair);
                typeNamespace = dictionary.Add(CrmSvcUtilNamespace);
                return true; // indicating that this resolver knows how to handle "SdkMessages"
            }
            if (type == typeof(SdkMessageRequest))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(Names.SdkMessageRequest);
                typeNamespace = dictionary.Add(CrmSvcUtilNamespace);
                return true; // indicating that this resolver knows how to handle "SdkMessages"
            }
            if (type == typeof(SdkMessageRequestField))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(Names.SdkMessageRequestField);
                typeNamespace = dictionary.Add(CrmSvcUtilNamespace);
                return true; // indicating that this resolver knows how to handle "SdkMessages"
            }
            if (type == typeof(SdkMessageResponse))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(Names.SdkMessageResponse);
                typeNamespace = dictionary.Add(CrmSvcUtilNamespace);
                return true; // indicating that this resolver knows how to handle "SdkMessages"
            }
            if (type == typeof(SdkMessageResponseField))
            {
                var dictionary = new XmlDictionary();
                typeName = dictionary.Add(Names.SdkMessageResponseField);
                typeNamespace = dictionary.Add(CrmSvcUtilNamespace);
                return true; // indicating that this resolver knows how to handle "SdkMessages"
            }

            // Defer to the known type resolver
            var value =  knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace);
            if (!value)
            {
                value = true;
            }
            return value;
        }
예제 #25
0
        /// <summary>
        /// Tries to resolve whether a type is known.
        /// Is used for serialization.
        /// Gets called by the framework.
        /// </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)
        {
            Logger.Debug(String.Format("BusDataContractResolver: TryResolveType(type='{0}', declaredType='{1}')", type.FullName, declaredType.FullName));

            if (ServiceHelper.IsServiceValidType(type))
            {
                XmlDictionary dictionary = new XmlDictionary();
                string encodedTypeName = Encoding.Encoder.SafeEncode(type.FullName);
                Logger.Debug(String.Format("BusDataContractResolver: TryResolveType() got valid type: '{0}'. Encoded name: '{1}'.", type.FullName, encodedTypeName));
                typeName = dictionary.Add(encodedTypeName);
                typeNamespace = dictionary.Add("http://tempuri.com");
                return true;
            }

            Logger.Error(String.Format("BusDataContractResolver: TryResolveType() got invalid type: '{0}'.", type.FullName));
            return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
        }
예제 #26
0
 public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver,
     out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
 {
     TryResolveTypeInvoked = true;
     return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
 }
예제 #27
0
 public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
 {
     return TryResolveDataContractType(type, declaredType, out typeName, out typeNamespace)
         || knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace)
         || TryResolveClrType(type, declaredType, out typeName, out typeNamespace);
 }
예제 #28
0
 /// <summary>
 /// Attempts to resolve a type to an XML schema type name
 /// </summary>
 /// <param name="type">
 /// The managed type to resolve
 /// </param>
 /// <param name="declared">
 /// The declared parameter type
 /// </param>
 /// <param name="knownTypes">
 /// The base known type resolver
 /// </param>
 /// <param name="xmlName">
 /// Return the type name via here
 /// </param>
 /// <param name="xmlNamespace">
 /// Return the type namespace via here
 /// </param>
 /// <returns>
 /// True if the type was resolved
 /// False otherwise
 /// </returns>
 public override Boolean TryResolveType(
     Type type,
     Type declared,
     DataContractResolver knownTypes,
     out XmlDictionaryString xmlName,
     out XmlDictionaryString xmlNamespace)
 {
     if (!knownTypes.TryResolveType(type, declared, null, out xmlName, out xmlNamespace))
      {
     String ns, name;
     if (!TryResolve(type, declared, out ns, out name))
        return false;
     xmlNamespace = this.dictionary.Add(ns);
     xmlName = this.dictionary.Add(name);
      }
      return true;
 }
예제 #29
0
        // Used at serialization
        // Maps any Type to a new xsi:type representation
        /// <summary>
        /// 序列化时将类确定传输用的类型名称
        /// </summary>
        /// <param name="type">需要处理的类型,默认情况下应该传入的是实体或者DTO类型,通用CRUD查询实体时传入的是实体的代理子类对象,
        /// 所以需要特别处理</param>
        /// <param name="declaredType">契约上定义的类型</param>
        /// <param name="knownTypeResolver">已知类型处理器</param>
        /// <param name="typeName">类型全名</param>
        /// <param name="typeNamespace">类型所在Assembly</param>
        /// <returns></returns>
        public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver,
            out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            typeName = null;// new XmlDictionaryString(XmlDictionary.Empty, "", 0);
            typeNamespace = null; new XmlDictionaryString(XmlDictionary.Empty, "", 0);

            //if (dataContract == null)
            //{
            //    return false;
            //}
            //if (type == null)
            //{
            //    return false;s
            //}

            try
            {
                //不是实体或者DTO的类型
                if (!typeof(XObject).IsAssignableFrom(type))
                {
                    //优先使用系统定义的策略
                    if (!type.IsPrimitive)  //Assembly.FullName.Contains("NHibernate")
                    {
                        _logger.Warn(string.Format("Unknown type has occur,Name:{0},NameSpace:{1}",
                            type.FullName, typeNamespace));
                        return true;
                    }
                    else
                    {
                        return knownTypeResolver.TryResolveType(type, declaredType,
                            knownTypeResolver, out typeName, out typeNamespace);
                    }
                }

                //判断是否代理子类对象
                if (type.BaseType != null && type.Assembly.IsDynamic)
                {
                    //使用基类来代替
                    type = type.BaseType;
                }

                if (!TypeDic.ContainsKey(type))
                {
                    GetTypeInfo(type);
                }
                typeName = TypeDic[type].TypeName;
                typeNamespace = TypeDic[type].AssemblyName;

                return true;
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return false;
            }
        }
예제 #30
0
        public override bool TryResolveType(Type type,Type declaredType,DataContractResolver knownTypeResolver,out XmlDictionaryString typeName,out XmlDictionaryString typeNamespace)
        {
            if(m_TypeToNames.ContainsKey(type))
             {
            XmlDictionary dictionary = new XmlDictionary();
            typeNamespace = dictionary.Add(m_TypeToNames[type].Item1);
            typeName      = dictionary.Add(m_TypeToNames[type].Item2);

            return true;
             }
             else
             {
            return knownTypeResolver.TryResolveType(type,declaredType,null,out typeName,out typeNamespace);
             }
        }
예제 #31
0
        public void Serialize(Type type, object graph)
        {
            if (graph == null)
            {
                writer.WriteAttributeString("nil", XmlSchema.InstanceNamespace, "true");
            }
#if !MOONLIGHT
            else if (type == typeof(XmlElement))
            {
                ((XmlElement)graph).WriteTo(Writer);
            }
            else if (type == typeof(XmlNode []))
            {
                foreach (var xn in (XmlNode [])graph)
                {
                    xn.WriteTo(Writer);
                }
            }
#endif
            else
            {
                QName resolvedQName = null;
                if (resolver != null)
                {
                    XmlDictionaryString rname, rns;
                    if (resolver.TryResolveType(graph != null ? graph.GetType() : typeof(object), type, default_resolver, out rname, out rns))
                    {
                        resolvedQName = new QName(rname.Value, rns.Value);
                    }
                }

                Type actualType = graph.GetType();

                SerializationMap map;
                map = types.FindUserMap(actualType);
                // For some collection types, the actual type does not matter. So get nominal serialization type instead.
                // (The code below also covers the lines above, but I don't remove above lines to avoid extra search cost.)
                if (map == null)
                {
                    // FIXME: not sure if type.IsInterface is the correct condition to determine whether items are serialized with i:type or not. (e.g. bug #675144 server response).
                    actualType = types.GetSerializedType(type.IsInterface ? type : actualType);
                    map        = types.FindUserMap(actualType);
                }
                // If it is still unknown, then register it.
                if (map == null)
                {
                    types.Add(actualType);
                    map = types.FindUserMap(actualType);
                }

                if (actualType != type && (map == null || map.OutputXsiType))
                {
                    QName  qname = resolvedQName ?? types.GetXmlName(actualType);
                    string name  = qname.Name;
                    string ns    = qname.Namespace;
                    if (qname == QName.Empty)
                    {
                        name = XmlConvert.EncodeLocalName(actualType.Name);
                        ns   = KnownTypeCollection.DefaultClrNamespaceBase + actualType.Namespace;
                    }
                    else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace)
                    {
                        ns = XmlSchema.Namespace;
                    }
                    if (writer.LookupPrefix(ns) == null)                      // it goes first (extraneous, but it makes att order compatible)
                    {
                        writer.WriteXmlnsAttribute(null, ns);
                    }
                    writer.WriteStartAttribute("i", "type", XmlSchema.InstanceNamespace);
                    writer.WriteQualifiedName(name, ns);
                    writer.WriteEndAttribute();
                }
                QName predef = KnownTypeCollection.GetPredefinedTypeName(actualType);
                if (predef != QName.Empty)
                {
                    SerializePrimitive(type, graph, predef);
                }
                else
                {
                    map.Serialize(graph, this);
                }
            }
        }