ResolveName() public abstract method

public abstract ResolveName ( string typeName, string typeNamespace, System declaredType, System knownTypeResolver ) : System.Type
typeName string
typeNamespace string
declaredType System
knownTypeResolver System
return System.Type
コード例 #1
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            if (!typeNamespace.StartsWith("http://tempuri.com"))
            {
                return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
            }
            string PluginName = typeNamespace.Substring("http://tempuri.com".Length + 1);
            System.Reflection.Assembly asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(p => p.FullName == PluginName);

            var t = asm.GetTypes().Where(p => p.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0).FirstOrDefault(p => p.Name == typeName);
            if (t != null)
                return t;

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        }
コード例 #2
0
		public override Type ResolveName(string typeName, string typeNamespace, Type declaredType,
										 DataContractResolver knownTypeResolver)
		{
			if (declaredType == null)
				throw new ArgumentNullException("declaredType");
			if (knownTypeResolver == null)
				throw new ArgumentNullException("knownTypeResolver");

			var type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);
			if (type != null)
				return type;

            Debug.Print(typeNamespace);
            Debug.Print(typeName);
            type = Type.GetType(typeName);
            if (type != null)
				return type;

            int lastSlashIndex = typeNamespace.LastIndexOf("/");
            if (lastSlashIndex >= 0)
            {
                string ns = typeNamespace.Substring(lastSlashIndex + 1);
                return Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}.{1}, {0}", ns, typeName));
            }

            return null;

		}
コード例 #3
0
        /// <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);
        }
コード例 #4
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            if (!typeNamespace.StartsWith("http://tempuri.com"))
            {
                return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
            }
            string PluginName = typeNamespace.Substring("http://tempuri.com".Length);
            IPlugin plugin = PluginHelper.plugins[PluginName];

            var lst = plugin.GetType().Assembly.GetTypes().Where(p => p.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0).ToList();
            var t = lst.FirstOrDefault(p => p.Name == typeName);
            if (t != null)
                return t;

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        }
コード例 #5
0
 public override Type ResolveName(string typeName, string typeNamespace, Type declaredType,
     DataContractResolver knownTypeResolver)
 {
     Type type;
     int deserializationIndex;
     int poundIndex = typeNamespace.IndexOf("#");
     if (poundIndex < 0)
     {
         if (Int32.TryParse(typeNamespace, out deserializationIndex))
         {
             deserializationDictionary.TryGetValue(deserializationIndex, out typeNamespace);
         }
         type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
     }
     else
     {
         if (Int32.TryParse(typeNamespace.Substring(0, poundIndex), out deserializationIndex))
         {
             typeNamespace = typeNamespace.Substring(poundIndex + 1, typeNamespace.Length - poundIndex - 1);
             deserializationDictionary.Add(deserializationIndex, typeNamespace);
         }
         type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
     }
     return type;
 }
コード例 #6
0
ファイル: DCSwithDCR.cs プロジェクト: tian1ll1/WPF_Examples
 // Used at deserialization
 // Allows users to map xsi:type name to any Type 
 public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
 {
     Type type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
     if (type == null)
     {
         type = Type.GetType(typeName + ", " + typeNamespace);
     }
     return type;
 }
コード例 #7
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            DbHelpers.ThrowIfNullOrWhitespace(typeName, "typeName");
            DbHelpers.ThrowIfNullOrWhitespace(typeNamespace, "typeNamespace");
            DbHelpers.ThrowIfNull(declaredType, "declaredType");
            DbHelpers.ThrowIfNull(knownTypeResolver, "knownTypeResolver");

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        }
コード例 #8
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            EntityUtil.CheckStringArgument(typeName, "typeName");
            EntityUtil.CheckStringArgument(typeNamespace, "typeNamespace");
            EntityUtil.CheckArgumentNull(declaredType, "declaredType");
            EntityUtil.CheckArgumentNull(knownTypeResolver, "knownTypeResolver");

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType ,null);
        }
コード例 #9
0
        /// <summary>During deserialization, maps any xsi:type information to the actual type of the persistence-ignorant object. </summary>
        /// <returns>Returns the type that the xsi:type is mapped to. Returns null if no known type was found that matches the xsi:type.</returns>
        /// <param name="typeName">The xsi:type information to map.</param>
        /// <param name="typeNamespace">The namespace of the xsi:type.</param>
        /// <param name="declaredType">The declared type.</param>
        /// <param name="knownTypeResolver">
        ///     An instance of <see cref="T:System.Data.Entity.Core.Objects.ProxyDataContractResolver" />.
        /// </param>
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            Check.NotEmpty(typeName, "typeName");
            Check.NotEmpty(typeNamespace, "typeNamespace");
            Check.NotNull(declaredType, "declaredType");
            Check.NotNull(knownTypeResolver, "knownTypeResolver");

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        }
コード例 #10
0
ファイル: TypeResolver.cs プロジェクト: modulexcite/WcfEx
 /// <summary>
 /// Resolves a type name to a managed type
 /// </summary>
 /// <param name="name">
 /// The name of the type to resolve
 /// </param>
 /// <param name="ns">
 /// The named type's namespace
 /// </param>
 /// <param name="declared">
 /// Teh declared parameter type
 /// </param>
 /// <param name="knownTypes">
 /// The base known type resolver
 /// </param>
 /// <returns>
 /// The resolved managed type
 /// </returns>
 public override Type ResolveName(
     String name,
     String ns,
     Type declared,
     DataContractResolver knownTypes)
 {
     return
     knownTypes.ResolveName(name, ns, declared, null) ??
     Resolve(ns, name, declared);
 }
コード例 #11
0
ファイル: DataboxResolver.cs プロジェクト: StevenLaw/DataBox
 /// <summary>
 /// Override this method to map the specified xsi:type name and namespace to a data contract type during deserialization.
 /// </summary>
 /// <param name="typeName">The xsi:type name to map.</param>
 /// <param name="typeNamespace">The xsi:type namespace to map.</param>
 /// <param name="declaredType">The type declared in the data contract.</param>
 /// <param name="knownTypeResolver">The known type resolver.</param>
 /// <returns>
 /// The type the xsi:type name and namespace is mapped to.
 /// </returns>
 public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
 {
     if (typeName == "Tag" && typeNamespace == uri)
         return typeof(Tag);
     else if (typeName == "Entry" && typeNamespace == uri)
         return typeof(Entry);
     else if (typeName == "LinkEntry" && typeNamespace == uri)
         return typeof(LinkEntry);
     else if (typeName == "LinkItem" && typeNamespace == uri)
         return typeof(LinkItem);
     else return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
 }
コード例 #12
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            if (typeNamespace == ResolverNamespace)
            {
                Type result = DynamicTypeBuilder.Instance.GetDynamicType(typeName);
                if (result != null)
                {
                    return result;
                }
            }

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        }
コード例 #13
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            if (string.Compare(typeNamespace, _xmlNamespace, StringComparison.OrdinalIgnoreCase) == 0)
            {
                Assembly containingAssembly;
                if (_typeToAssemblyLookup.TryGetValue(typeName, out containingAssembly))
                {
                    var type = containingAssembly.GetType(typeName);
                    if (type != null) return type;
                }
            }

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        }
コード例 #14
0
		public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
		{
			Type type = null;
			try
			{
				type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
			}
			catch (Exception)
			{
				throw;
			}

			return type ?? declaredType;
		}
コード例 #15
0
        // Used at deserialization
        // Allows users to map xsi:type name to any Type 
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            XmlDictionaryString tName;
            XmlDictionaryString tNamespace;

            // If the type is found in the dictionary then extract it from the assembly. Use the default KnownTypeResolver otherwise.
            if (dictionary.TryLookup(typeName, out tName) && dictionary.TryLookup(typeNamespace, out tNamespace))
            {
                return this.assembly.GetType(tNamespace.Value + "." + tName.Value);
            }
            else
            {
                return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
            }
        }
コード例 #16
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            //			if (typeof(System.Linq.IQueryable).IsAssignableFrom(declaredType))
            //			{
            //				string fullTypeName = typeNamespace + "." + typeName;
            //				Type type = Type.GetType(fullTypeName);
            //				if (type != null)
            //					return type;
            //			}
            //			else
            //			{
            string typeKey = typeName + "." + typeNamespace;

            return _typeNames.ContainsKey(typeKey) ? _typeNames[typeKey] :
                knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);
        }
コード例 #17
0
        public override Type ResolveName(string typeName, string typeNamespace, Type type, DataContractResolver knownTypeResolver)
        {
            Type foundType = null;
            foreach (var t in Identity.Keys) {
                if (typeName.ToLower() == Identity[t] && typeNamespace == t.FullName) {
                    foundType = t;
                }
            }

            if (foundType != null) {
                return foundType;
            }

            // Defer to the known type resolver
            return knownTypeResolver.ResolveName(typeName, typeNamespace, type, null);
        }
コード例 #18
0
        public override Type ResolveName(
            string typeName,
            string typeNamespace,
            Type declaredType,
            DataContractResolver knownTypeResolver)
        {
            Type type =
                GetTypeMatchingNameAndNamespace(typeName, typeNamespace) ??
                GetTypeMatchingName(typeName) ??
                knownTypeResolver.ResolveName(
                    typeName,
                    typeNamespace,
                    declaredType,
                    knownTypeResolver);

            return type;
        }
コード例 #19
0
        // Deserialization
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            Type type = null;
            typesByNamespace.TryGetValue(typeNamespace, out type);

            if (type != null)
            {
                Type genType = typeof(SchedulerTask<>);
                Type genericType = genType.MakeGenericType(type);
                return genericType;
            }
            else
            {
                // Defer to the known type resolver
                return knownTypeResolver.ResolveName(typeName, typeNamespace, null, knownTypeResolver);
            }
        }
コード例 #20
0
        protected DataContract ResolveDataContractFromKnownTypes(string typeName, string typeNs, DataContract memberTypeContract, Type declaredType)
        {
            XmlQualifiedName qname = new XmlQualifiedName(typeName, typeNs);
            DataContract     dataContract;

            if (_dataContractResolver == null)
            {
                dataContract = ResolveDataContractFromKnownTypes(qname);
            }
            else
            {
                Type dataContractType = _dataContractResolver.ResolveName(typeName, typeNs, declaredType, KnownTypeResolver);
                dataContract = dataContractType == null ? null : GetDataContract(dataContractType);
            }
            if (dataContract == null)
            {
                if (memberTypeContract != null &&
                    !memberTypeContract.UnderlyingType.IsInterface &&
                    memberTypeContract.StableName == qname)
                {
                    dataContract = memberTypeContract;
                }
                if (dataContract == null && rootTypeDataContract != null)
                {
                    if (rootTypeDataContract.StableName == qname)
                    {
                        dataContract = rootTypeDataContract;
                    }
                    else
                    {
                        CollectionDataContract collectionContract = rootTypeDataContract as CollectionDataContract;
                        while (collectionContract != null)
                        {
                            DataContract itemContract = GetDataContract(GetSurrogatedType(collectionContract.ItemType));
                            if (itemContract.StableName == qname)
                            {
                                dataContract = itemContract;
                                break;
                            }
                            collectionContract = itemContract as CollectionDataContract;
                        }
                    }
                }
            }
            return(dataContract);
        }
コード例 #21
0
        public override Type ResolveName(
            string typeName, 
            string typeNamespace, 
            Type declaredType, 
            DataContractResolver knownTypeResolver)
        {
            if (typeNamespace == "myNamespace")
            {
                var assembly = Assembly.GetExecutingAssembly();

                var type = assembly.GetType("Task.DB." + typeName);

                return type;
            }

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);
        }
コード例 #22
0
        DataContract ResolveDataContractFromDataContractResolver(XmlQualifiedName typeName, Type declaredType)
        {
            if (TD.DCResolverResolveIsEnabled())
            {
                TD.DCResolverResolve(typeName.Name + ":" + typeName.Namespace);
            }

            Type dataContractType = DataContractResolver.ResolveName(typeName.Name, typeName.Namespace, declaredType, KnownTypeResolver);

            if (dataContractType == null)
            {
                return(null);
            }
            else
            {
                return(GetDataContract(dataContractType));
            }
        }
コード例 #23
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType,
            DataContractResolver knownTypeResolver)
        {
            if (declaredType == typeof (Object)) //&& typeName != "int" && typeName != "bool" && typeName != "string")
            {
                //if (typeNamespace == ServiceConstants.Schema && typeName == "ArrayOfSplit")
                //{
                //    return typeof(List<Split>);
                //}
                //if (typeNamespace == ServiceConstants.Schema && typeName == "ArrayOfint")
                //{
                //    return typeof(List<int>);
                //}
            }

            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null) ??
                   Type.GetType(typeName + ", " + typeNamespace);
        }
コード例 #24
0
		public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
		{
			//Debug.WriteLine("ResolveName ", typeName);

			Type retVal = null;

			if (_namesToType.ContainsKey(typeNamespace))
			{
				if (_namesToType[typeNamespace].ContainsKey(typeName))
				{
					retVal = _namesToType[typeNamespace][typeName];
				}
			}
			else
			{
				retVal = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
			}

			return retVal;
		}
コード例 #25
0
        public override Type ResolveName(string alias, string typeNamespace, Type type, DataContractResolver knownTypeResolver)
        {
            Type foundType = null;
            foreach (Type t in TypeIdentity.Instance.Keys)
            {
                if (alias.ToLower() == TypeIdentity.Instance[t]?.Alias && typeNamespace == t.FullName)
                {
                    foundType = t;
                }
            }

            if (foundType != null) {
                return foundType;
            }
            else
            {
                // Defer to the known type resolver
                return knownTypeResolver.ResolveName(alias, typeNamespace, type, null);
            }
        }
コード例 #26
0
ファイル: Resolver.cs プロジェクト: sunoru/PBO
 public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
 {
     Type type = knownTypeResolver.ResolveName(typeName, typeNamespace,
       declaredType, null);
       if (type != null)
     return type;
       if (typeNamespace == Namespaces.DEFAULT)
       {
     if (typeName.StartsWith(CollectionType))
     {
       Type argType = ResolveName(typeName.Substring(12), knownTypeResolver);
       if (argType != null)
     return Resolver.GetGenericCollectionType(argType);
     }
     else if (typeName.StartsWith(ArrayType))
     {
       Type elementType = ResolveName(typeName.Substring(7), knownTypeResolver);
       return elementType.MakeArrayType();
     }
       }
       return null;
 }
コード例 #27
0
		public override Type ResolveName(string typeName, string typeNamespace, Type declaredType,
										 DataContractResolver knownTypeResolver)
		{
			if (declaredType == null)
				throw new ArgumentNullException("declaredType");
			if (knownTypeResolver == null)
				throw new ArgumentNullException("knownTypeResolver");

			var type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);
			if (type != null)
				return type;


            var tuple = new Tuple<string,string>(typeName, typeNamespace);
            if (typesCache.TryGetValue(tuple, out type))
                return type;

            type = TryGetType(typeName, typeNamespace);
            typesCache.Add(tuple, type);
            return type;

		}
コード例 #28
0
 //public EntityDTODataContractResolver()
 //{
 //}
 //private Assembly assembly;
 //public EntityDTODataContractResolver(Assembly assembly)
 //{
 //    this.assembly = assembly;
 //}
 /// <summary>
 /// 反序列化时通过解析类型名称得到类型
 /// Allows users to map xsi:type name to any Type 
 /// </summary>
 /// <param name="typeFullName">实体/DTO全名</param>
 /// <param name="assemblyFullName">实体/DTO所在的Assembly名称</param>
 /// <param name="declaredType">契约上定义的类型</param>
 /// <param name="knownTypeResolver">已知类型处理器</param>
 /// <returns></returns>
 public override Type ResolveName(string typeFullName, string assemblyFullName, Type declaredType,
     DataContractResolver knownTypeResolver)
 {
     if (declaredType.FullName == typeFullName)
         return declaredType;
     else
     {
         if (!AssemblyDic.ContainsKey(assemblyFullName))
         {
             var type = knownTypeResolver.ResolveName(typeFullName, assemblyFullName, declaredType, knownTypeResolver);
             if (type == null)
             {
                 AssemblyDic.Add(assemblyFullName, Assembly.Load(assemblyFullName));
             }
             else
             {
                 return type;
             }
         }
         return AssemblyDic[assemblyFullName].GetType(typeFullName);
     }
 }
コード例 #29
0
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType,
            DataContractResolver knownTypeResolver)
        {
            try
            {
                Type type;

                return this.knownTypes.TryGetValue(typeName, out type)
                    ? type
                    : knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                    $"Unable to resolve type {typeName}. {ex.InnerException} " +
                    "If the given type name is postfixed with a weird base64 encoded value, it means that " +
                    "the type is a generic type. WCF postfixes the name with a hash based on the " +
                    "namespaces of the generic type arguments. To fix this, mark the class with the " +
                    "DataContractAttribute to force a specific name. Example:" +
                    "[DataContract(Name = nameof(YourType<T>) + \"Of{0}\")]. And don't forget to mark the type's " +
                    "properties with the DataMemberAttribute.", ex);
            }            
        }
コード例 #30
0
 public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
 {
     return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null) ?? Type.GetType(typeName + ", " + typeNamespace);
 }
コード例 #31
0
        // This method handles z:Ref, xsi:nil and primitive types, and then delegates to DeserializeByMap() for anything else.

        public object Deserialize(Type type, XmlReader reader)
        {
#if !MOONLIGHT
            if (type == typeof(XmlElement))
            {
                return(XmlDocument.ReadNode(reader));
            }
            else if (type == typeof(XmlNode []))
            {
                reader.ReadStartElement();
                var l = new List <XmlNode> ();
                for (; !reader.EOF && reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent())
                {
                    l.Add(XmlDocument.ReadNode(reader));
                }
                reader.ReadEndElement();
                return(l.ToArray());
            }
#endif
            QName graph_qname = null;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                Type internal_type = type.GetGenericArguments() [0];

                if (types.FindUserMap(internal_type) != null)
                {
                    graph_qname = types.GetQName(internal_type);
                }
            }

            if (graph_qname == null)
            {
                graph_qname = types.GetQName(type);
            }

            string itype = reader.GetAttribute("type", XmlSchema.InstanceNamespace);
            if (itype != null)
            {
                string [] parts = itype.Split(':');
                if (parts.Length > 1)
                {
                    graph_qname = new QName(parts [1], reader.LookupNamespace(reader.NameTable.Get(parts [0])));
                }
                else
                {
                    graph_qname = new QName(itype, reader.LookupNamespace(String.Empty));
                }
            }

            string label = reader.GetAttribute("Ref", KnownTypeCollection.MSSimpleNamespace);
            if (label != null)
            {
                object o;
                if (!references.TryGetValue(label, out o))
                {
                    throw new SerializationException(String.Format("Deserialized object with reference Id '{0}' was not found", label));
                }
                reader.Skip();
                return(o);
            }

            bool isNil = reader.GetAttribute("nil", XmlSchema.InstanceNamespace) == "true";

            if (isNil)
            {
                reader.Skip();
                if (!type.IsValueType || type == typeof(void))
                {
                    return(null);
                }
                else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    return(null);
                }
                else
                {
                    throw new SerializationException(String.Format("Value type {0} cannot be null.", type));
                }
            }

            if (resolver != null)
            {
                Type t;
                if (resolved_qnames.TryGetValue(graph_qname, out t))
                {
                    type = t;
                }
                else                   // i.e. resolve name only once.
                {
                    type = resolver.ResolveName(graph_qname.Name, graph_qname.Namespace, type, default_resolver) ?? type;
                    resolved_qnames.Add(graph_qname, type);
                    types.Add(type);
                }
            }

            if (KnownTypeCollection.GetPrimitiveTypeFromName(graph_qname) != null)
            {
                string id = reader.GetAttribute("Id", KnownTypeCollection.MSSimpleNamespace);

                object ret = DeserializePrimitive(type, reader, graph_qname);

                if (id != null)
                {
                    if (references.ContainsKey(id))
                    {
                        throw new InvalidOperationException(String.Format("Object with Id '{0}' already exists as '{1}'", id, references [id]));
                    }
                    references.Add(id, ret);
                }
                return(ret);
            }

            return(DeserializeByMap(graph_qname, type, reader));
        }
コード例 #32
0
ファイル: MyResolver.cs プロジェクト: ChuangYang/corefx
 public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
 {
     ResolveNameInvoked = true;
     return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
 }
コード例 #33
0
        // This method handles z:Ref, xsi:nil and primitive types, and then delegates to DeserializeByMap() for anything else.
        public object Deserialize(Type type, XmlReader reader)
        {
            QName  graph_qname = types.GetQName(type);
            string itype       = reader.GetAttribute("type", XmlSchema.InstanceNamespace);

            if (itype != null)
            {
                string [] parts = itype.Split(':');
                if (parts.Length > 1)
                {
                    graph_qname = new QName(parts [1], reader.LookupNamespace(reader.NameTable.Get(parts [0])));
                }
                else
                {
                    graph_qname = new QName(itype, reader.LookupNamespace(String.Empty));
                }
            }

            string label = reader.GetAttribute("Ref", KnownTypeCollection.MSSimpleNamespace);

            if (label != null)
            {
                object o;
                if (!references.TryGetValue(label, out o))
                {
                    throw new SerializationException(String.Format("Deserialized object with reference Id '{0}' was not found", label));
                }
                reader.Skip();
                return(o);
            }

            bool isNil = reader.GetAttribute("nil", XmlSchema.InstanceNamespace) == "true";

            if (isNil)
            {
                reader.Skip();
                if (!type.IsValueType)
                {
                    return(null);
                }
                else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    return(null);
                }
                else
                {
                    throw new SerializationException(String.Format("Value type {0} cannot be null.", type));
                }
            }

            if (resolver != null)
            {
                Type t;
                if (resolved_qnames.TryGetValue(graph_qname, out t))
                {
                    type = t;
                }
                else                   // i.e. resolve name only once.
                {
                    type = resolver.ResolveName(graph_qname.Name, graph_qname.Namespace, type, default_resolver) ?? type;
                    resolved_qnames.Add(graph_qname, type);
                    types.Add(type);
                }
            }

            if (KnownTypeCollection.GetPrimitiveTypeFromName(graph_qname) != null)
            {
                string id = reader.GetAttribute("Id", KnownTypeCollection.MSSimpleNamespace);

                object ret = DeserializePrimitive(type, reader, graph_qname);

                if (id != null)
                {
                    if (references.ContainsKey(id))
                    {
                        throw new InvalidOperationException(String.Format("Object with Id '{0}' already exists as '{1}'", id, references [id]));
                    }
                    references.Add(id, ret);
                }
                return(ret);
            }

            return(DeserializeByMap(graph_qname, type, reader));
        }
コード例 #34
0
ファイル: GenericResolver.cs プロジェクト: JMnITup/HotelApp
 public override Type ResolveName(string typeName,string typeNamespace,Type declaredType,DataContractResolver knownTypeResolver)
 {
     if(m_NamesToType.ContainsKey(typeNamespace))
      {
     if(m_NamesToType[typeNamespace].ContainsKey(typeName))
     {
        return m_NamesToType[typeNamespace][typeName];
     }
      }
      return knownTypeResolver.ResolveName(typeName,typeNamespace,declaredType,null);
 }