コード例 #1
0
 public override object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute xmlAttrib)
 {
     if (null == xmlAttrib)
     {
         string typeName = xmlReader.GetAttribute(Keywords.MSD_INSTANCETYPE, Keywords.MSDNS);
         if (typeName == null)
         {
             string xsdTypeName = xmlReader.GetAttribute(Keywords.MSD_INSTANCETYPE, Keywords.XSINS); // this xsd type
             if (null != xsdTypeName)
             {
                 typeName = XSDSchema.XsdtoClr(xsdTypeName).FullName;
             }
         }
         Type   type = (typeName == null) ? _dataType : Type.GetType(typeName);
         object Obj  = System.Activator.CreateInstance(type, true);
         Debug.Assert(xmlReader is DataTextReader, "Invalid DataTextReader is being passed to customer");
         ((IXmlSerializable)Obj).ReadXml(xmlReader);
         return(Obj);
     }
     else
     {
         XmlSerializer deserializerWithRootAttribute = ObjectStorage.GetXmlSerializer(_dataType, xmlAttrib);
         return(deserializerWithRootAttribute.Deserialize(xmlReader));
     }
 }
コード例 #2
0
 public override void ConvertObjectToXml(object value, XmlWriter xmlWriter, XmlRootAttribute xmlAttrib)
 {
     if (xmlAttrib == null)
     {
         ((IXmlSerializable)value).WriteXml(xmlWriter);
     }
     else
     {
         ObjectStorage.GetXmlSerializer(base.DataType, xmlAttrib).Serialize(xmlWriter, value);
     }
 }
コード例 #3
0
        internal static Type GetType(string value)
        {
            Type type = Type.GetType(value);

            if ((null == type) && ("System.Numerics.BigInteger" == value))
            {
                type = typeof(BigInteger);
            }
            ObjectStorage.VerifyIDynamicMetaObjectProvider(type);
            return(type);
        }
コード例 #4
0
 public override void ConvertObjectToXml(object value, XmlWriter xmlWriter, XmlRootAttribute?xmlAttrib)
 {
     if (null == xmlAttrib)
     { // implements IXmlSerializable
         Debug.Assert(xmlWriter is DataTextWriter, "Invalid DataTextWriter is being passed to customer");
         ((IXmlSerializable)value).WriteXml(xmlWriter);
     }
     else
     {
         XmlSerializer serializerWithRootAttribute = ObjectStorage.GetXmlSerializer(value.GetType(), xmlAttrib);
         serializerWithRootAttribute.Serialize(xmlWriter, value);
     }
 }
コード例 #5
0
ファイル: DataStorage.cs プロジェクト: dox0/DotNet471RS3
        /// <summary>wrapper around Type.GetType</summary>
        /// <param name="value">assembly qualified type name or one of the special known types</param>
        /// <returns>Type or null if not found</returns>
        /// <exception cref="InvalidOperationException">when type implements IDynamicMetaObjectProvider and not IXmlSerializable</exception>
        /// <remarks>
        /// Types like "System.Guid" will load regardless of AssemblyQualifiedName because they are special
        /// Types like "System.Data.SqlTypes.SqlString" will load because they are in the same assembly as this code
        /// Types like "System.Numerics.BigInteger" won't load because they are not special and not same assembly as this code
        /// </remarks>
        internal static Type GetType(string value) {
            Type dataType = Type.GetType(value); // throwOnError=false, ignoreCase=fase
            if (null == dataType) {
                if ("System.Numerics.BigInteger" == value) {
                    dataType = typeof(System.Numerics.BigInteger);
                }
            }

            // Dev10 671061: prevent reading type from schema which implements IDynamicMetaObjectProvider and not IXmlSerializable
            // the check here prevents the type from being loaded in schema or as instance data (when DataType is object)
            ObjectStorage.VerifyIDynamicMetaObjectProvider(dataType);
            return dataType;
        }
コード例 #6
0
        public override void ConvertObjectToXml(object value, XmlWriter xmlWriter, XmlRootAttribute xmlAttrib)
        {
            if (null == xmlAttrib)
            {
                Debug.Assert(xmlWriter is DataTextWriter, "Invalid DataTextWriter is being passed to customer");
                ((IXmlSerializable)value).WriteXml(xmlWriter);
            }
            else
            {
                // we support polymorphism only for types that implements IXmlSerializable.
                // Assumption: value is the same type as DataType

                XmlSerializer serializerWithRootAttribute = ObjectStorage.GetXmlSerializer(_dataType, xmlAttrib);
                serializerWithRootAttribute.Serialize(xmlWriter, value);
            }
        }
コード例 #7
0
        public override object ConvertXmlToObject(string s)
        {
            if (this.implementsIXmlSerializable)
            {
                object       obj2  = Activator.CreateInstance(base.DataType, true);
                StringReader input = new StringReader("<col>" + s + "</col>");
                using (XmlTextReader reader = new XmlTextReader(input))
                {
                    ((IXmlSerializable)obj2).ReadXml(reader);
                }
                return(obj2);
            }
            StringReader textReader = new StringReader(s);

            return(ObjectStorage.GetXmlSerializer(base.DataType).Deserialize(textReader));
        }
コード例 #8
0
        public override string ConvertObjectToXml(object value)
        {
            StringWriter w = new StringWriter(base.FormatProvider);

            if (this.implementsIXmlSerializable)
            {
                using (XmlTextWriter writer = new XmlTextWriter(w))
                {
                    ((IXmlSerializable)value).WriteXml(writer);
                    goto Label_0047;
                }
            }
            ObjectStorage.GetXmlSerializer(value.GetType()).Serialize((TextWriter)w, value);
Label_0047:
            return(w.ToString());
        }
コード例 #9
0
        override public string ConvertObjectToXml(object value)
        {
            StringWriter strwriter = new StringWriter(FormatProvider);

            if (implementsIXmlSerializable)
            {
                using (XmlTextWriter xmlTextWriter = new XmlTextWriter(strwriter)) {
                    ((IXmlSerializable)value).WriteXml(xmlTextWriter);
                }
            }
            else
            {
                XmlSerializer serializerWithOutRootAttribute = ObjectStorage.GetXmlSerializer(value.GetType());
                serializerWithOutRootAttribute.Serialize(strwriter, value);
            }
            return(strwriter.ToString());
        }
コード例 #10
0
        public override string ConvertObjectToXml(object value)
        {
            if ((value == null) || (value == _nullValue))// this case won't happen,  this is added in case if code in xml saver changes
            {
                return(string.Empty);
            }

            Type type = _dataType;

            if (type == typeof(byte[]) || (type == typeof(object) && (value is byte[])))
            {
                return(Convert.ToBase64String((byte[])value));
            }
            if ((type == typeof(Type)) || ((type == typeof(object)) && (value is Type)))
            {
                return(((Type)value).AssemblyQualifiedName !);
            }

            if (!IsTypeCustomType(value.GetType()))
            { // Guid and Type had TypeCode.Object
                return((string)SqlConvert.ChangeTypeForXML(value, typeof(string)));
            }

            if (Type.GetTypeCode(value.GetType()) != TypeCode.Object)
            {
                return(value.ToString() !);
            }

            StringWriter strwriter = new StringWriter(FormatProvider);

            if (_implementsIXmlSerializable)
            {
                using (XmlTextWriter xmlTextWriter = new XmlTextWriter(strwriter))
                {
                    ((IXmlSerializable)value).WriteXml(xmlTextWriter);
                }
                return(strwriter.ToString());
            }

            XmlSerializer serializerWithOutRootAttribute = ObjectStorage.GetXmlSerializer(value.GetType());

            serializerWithOutRootAttribute.Serialize(strwriter, value);
            return(strwriter.ToString());
        }
コード例 #11
0
        override public object ConvertXmlToObject(string s)
        {
            if (implementsIXmlSerializable)
            {
                object Obj = System.Activator.CreateInstance(DataType, true);

                string       tempStr   = string.Concat("<col>", s, "</col>"); // this is done since you can give fragmet to reader, bug 98767
                StringReader strReader = new  StringReader(tempStr);

                using (XmlTextReader xmlTextReader = new XmlTextReader(strReader)) {
                    ((IXmlSerializable)Obj).ReadXml(xmlTextReader);
                }
                return(Obj);
            }

            StringReader  strreader = new  StringReader(s);
            XmlSerializer deserializerWithOutRootAttribute = ObjectStorage.GetXmlSerializer(DataType);

            return(deserializerWithOutRootAttribute.Deserialize(strreader));
        }
コード例 #12
0
        public override object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute xmlAttrib)
        {
            if (xmlAttrib != null)
            {
                return(ObjectStorage.GetXmlSerializer(base.DataType, xmlAttrib).Deserialize(xmlReader));
            }
            string attribute = xmlReader.GetAttribute("InstanceType", "urn:schemas-microsoft-com:xml-msdata");

            if (attribute == null)
            {
                string xsdTypeName = xmlReader.GetAttribute("InstanceType", "http://www.w3.org/2001/XMLSchema-instance");
                if (xsdTypeName != null)
                {
                    attribute = XSDSchema.XsdtoClr(xsdTypeName).FullName;
                }
            }
            Type   type = (attribute == null) ? base.DataType : Type.GetType(attribute);
            object obj2 = Activator.CreateInstance(type, true);

            ((IXmlSerializable)obj2).ReadXml(xmlReader);
            return(obj2);
        }
コード例 #13
0
        public override object ConvertXmlToObject(string s)
        {
            Type type = _dataType; // real type of objects in this column

            if (type == typeof(byte[]))
            {
                return(Convert.FromBase64String(s));
            }
            if (type == typeof(Type))
            {
                return(Type.GetType(s) !);
            }
            if (type == typeof(Guid))
            {
                return(new Guid(s));
            }
            if (type == typeof(Uri))
            {
                return(new Uri(s));
            }


            if (_implementsIXmlSerializable)
            {
                object       Obj       = System.Activator.CreateInstance(_dataType, true) !;
                StringReader strReader = new StringReader(s);
                using (XmlTextReader xmlTextReader = new XmlTextReader(strReader))
                {
                    ((IXmlSerializable)Obj).ReadXml(xmlTextReader);
                }
                return(Obj);
            }

            StringReader  strreader = new StringReader(s);
            XmlSerializer deserializerWithOutRootAttribute = ObjectStorage.GetXmlSerializer(type);

            return(deserializerWithOutRootAttribute.Deserialize(strreader));
        }
コード例 #14
0
        public override object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute xmlAttrib)
        {
            object?retValue      = null;
            bool   isBaseCLRType = false;
            bool   legacyUDT     = false; // in 1.0 and 1.1 we used to call ToString on CDT obj. so if we have the same case

            // we need to handle the case when we have column type as object.
            if (null == xmlAttrib)
            { // this means type implements IXmlSerializable
                Type?  type     = null;
                string?typeName = xmlReader.GetAttribute(Keywords.MSD_INSTANCETYPE, Keywords.MSDNS);
                if (typeName == null || typeName.Length == 0)
                {                                                                               // No CDT polumorphism
                    string?xsdTypeName = xmlReader.GetAttribute(Keywords.TYPE, Keywords.XSINS); // this xsd type: Base type polymorphism
                    if (null != xsdTypeName && xsdTypeName.Length > 0)
                    {
                        string[] _typename = xsdTypeName.Split(':');
                        if (_typename.Length == 2)
                        { // split will return aray of size 1 if ":" is not there
                            if (xmlReader.LookupNamespace(_typename[0]) == Keywords.XSDNS)
                            {
                                xsdTypeName = _typename[1]; // trim the prefix and just continue with
                            }
                        } // for other case, let say we have two ':' in type, the we throws (as old behavior)
                        type          = XSDSchema.XsdtoClr(xsdTypeName);
                        isBaseCLRType = true;
                    }
                    else if (_dataType == typeof(object))
                    {                     // there is no Keywords.MSD_INSTANCETYPE and no Keywords.TYPE
                        legacyUDT = true; // see if our type is object
                    }
                }

                if (legacyUDT)
                { // if Everett UDT, just read it and return string
                    retValue = xmlReader.ReadString();
                }
                else
                {
                    if (typeName == Keywords.TYPEINSTANCE)
                    {
                        retValue = Type.GetType(xmlReader.ReadString());
                        xmlReader.Read(); // need to move to next node
                    }
                    else
                    {
                        if (null == type)
                        {
                            type = (typeName == null) ? _dataType : DataStorage.GetType(typeName);
                        }

                        if (type == typeof(char) || type == typeof(Guid))
                        { //msdata:char and msdata:guid imply base types.
                            isBaseCLRType = true;
                        }

                        if (type == typeof(object))
                        {
                            throw ExceptionBuilder.CanNotDeserializeObjectType();
                        }

                        TypeLimiter.EnsureTypeIsAllowed(type);

                        if (!isBaseCLRType)
                        {
                            retValue = System.Activator.CreateInstance(type, true) !;
                            Debug.Assert(xmlReader is DataTextReader, "Invalid DataTextReader is being passed to customer");
                            ((IXmlSerializable)retValue).ReadXml(xmlReader);
                        }
                        else
                        {  // Process Base CLR type
                           // for Element Node, if it is Empty, ReadString does not move to End Element; we need to move it
                            if (type == typeof(string) && xmlReader.NodeType == XmlNodeType.Element && xmlReader.IsEmptyElement)
                            {
                                retValue = string.Empty;
                            }
                            else
                            {
                                retValue = xmlReader.ReadString();
                                if (type != typeof(byte[]))
                                {
                                    retValue = SqlConvert.ChangeTypeForXML(retValue, type);
                                }
                                else
                                {
                                    retValue = Convert.FromBase64String(retValue.ToString() !);
                                }
                            }
                            xmlReader.Read();
                        }
                    }
                }
            }
            else
            {
                XmlSerializer deserializerWithRootAttribute = ObjectStorage.GetXmlSerializer(_dataType, xmlAttrib);
                retValue = deserializerWithRootAttribute.Deserialize(xmlReader);
            }
            return(retValue !);
        }
コード例 #15
0
 /// <summary>wrapper around Type.AssemblyQualifiedName</summary>
 /// <param name="type"></param>
 /// <returns>qualified name when writing in xml</returns>
 /// <exception cref="InvalidOperationException">when type implements IDynamicMetaObjectProvider and not IXmlSerializable</exception>
 internal static string GetQualifiedName(Type type)
 {
     Debug.Assert(null != type, "null type");
     ObjectStorage.VerifyIDynamicMetaObjectProvider(type);
     return(type.AssemblyQualifiedName !);
 }
コード例 #16
0
 internal static string GetQualifiedName(Type type)
 {
     ObjectStorage.VerifyIDynamicMetaObjectProvider(type);
     return(type.AssemblyQualifiedName);
 }