Beispiel #1
0
    private XmlSerializer CreateOverrideSerializer()
    {
        // Create and return an XmlSerializer instance used to
        // override and create SOAP messages.
        SoapAttributeOverrides mySoapAttributeOverrides =
            new SoapAttributeOverrides();
        SoapAttributes soapAtts = new SoapAttributes();

        // Override the SoapTypeAttribute.
        SoapTypeAttribute soapType = new SoapTypeAttribute();

        soapType.TypeName        = "Team";
        soapType.IncludeInSchema = false;
        soapType.Namespace       = "http://www.microsoft.com";
        soapAtts.SoapType        = soapType;

        mySoapAttributeOverrides.Add(typeof(Group), soapAtts);

        // Create an XmlTypeMapping that is used to create an instance
        // of the XmlSerializer. Then return the XmlSerializer object.
        XmlTypeMapping myMapping = (new SoapReflectionImporter(
                                        mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));

        XmlSerializer ser = new XmlSerializer(myMapping);

        return(ser);
    }
Beispiel #2
0
        // Get XML type information for a type.
        public static bool GetXmlTypeForInteropType
            (Type type, out String xmlType, out String xmlTypeNamespace)
        {
            // Look in the registered type table first.
            lock (typeof(SoapServices))
            {
                if (typeToXmlType != null)
                {
                    String key = (String)(typeToXmlType[type]);
                    if (key != null)
                    {
                        XmlKeyExpand(key, out xmlType,
                                     out xmlTypeNamespace);
                        return(true);
                    }
                }
            }

            // Check the attribute on the type.
            SoapTypeAttribute tattr = (SoapTypeAttribute)
                                      InternalRemotingServices.GetCachedSoapAttribute(type);

            if (tattr.xmlTypeWasSet)
            {
                xmlType          = tattr.XmlTypeName;
                xmlTypeNamespace = tattr.XmlTypeNamespace;
                return(true);
            }

            // We were unable to determine the XML element information.
            xmlType          = null;
            xmlTypeNamespace = null;
            return(false);
        }
Beispiel #3
0
        } // CreateBinaryFormatter

        internal static void SerializeSoapMessage(IMessage msg, Stream outputStream, bool includeVersions)
        {
            // create soap formatter
            SoapFormatter fmt = CreateSoapFormatter(true, includeVersions);

            //check for special options if this is the SoapFormatter
            IMethodMessage methodMsg = msg as IMethodMessage;

            if (methodMsg != null)
            {
                MethodBase mb = methodMsg.MethodBase;
                if (mb != null)
                {
                    Type type = methodMsg.MethodBase.DeclaringType;
                    SoapTypeAttribute cache =
                        (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);
                    if ((cache.SoapOptions & SoapOption.AlwaysIncludeTypes) == SoapOption.AlwaysIncludeTypes)
                    {
                        fmt.TypeFormat |= FormatterTypeStyle.TypesAlways;
                    }
                    if ((cache.SoapOptions & SoapOption.XsdString) == SoapOption.XsdString)
                    {
                        fmt.TypeFormat |= FormatterTypeStyle.XsdString;
                    }
                }
            }
            // end of set special options for SoapFormatter

            Header[] h = GetSoapHeaders(msg);

            // this is to make messages within a  message serialize correctly
            // and not use the fake type
            ((RemotingSurrogateSelector)fmt.SurrogateSelector).SetRootObject(msg);
            fmt.Serialize(outputStream, msg, h);
        } // SerializeSoapMessage
Beispiel #4
0
        } // GetXmlElementForInteropType

        /// <include file='doc\Soap.uex' path='docs/doc[@for="SoapServices.GetXmlTypeForInteropType"]/*' />
        public static bool GetXmlTypeForInteropType(Type type,
                                                    out String xmlType, out String xmlTypeNamespace)
        {
            // check table first
            XmlEntry entry = (XmlEntry)_interopTypeToXmlType[type];

            if (entry != null)
            {
                xmlType          = entry.Name;
                xmlTypeNamespace = entry.Namespace;
                return(true);
            }

            // check soap attribute
            SoapTypeAttribute attr = (SoapTypeAttribute)
                                     InternalRemotingServices.GetCachedSoapAttribute(type);

            if (attr.IsInteropXmlType())
            {
                xmlType          = attr.XmlTypeName;
                xmlTypeNamespace = attr.XmlTypeNamespace;
                return(true);
            }
            else
            {
                xmlType          = null;
                xmlTypeNamespace = null;
                return(false);
            }
        } // GetXmlTypeForInteropType
Beispiel #5
0
        internal static void ProcessTypeAttribute(Type type, SoapAttributeInfo attributeInfo)
        {
            SoapTypeAttribute attr = (SoapTypeAttribute)
                                     InternalRemotingServices.GetCachedSoapAttribute(type);

            if (attr.Embedded)
            {
                attributeInfo.m_attributeType |= SoapAttributeType.Embedded;
            }

            String xmlName, xmlNamespace;

            if (SoapServices.GetXmlElementForInteropType(type, out xmlName, out xmlNamespace))
            {
                attributeInfo.m_attributeType |= SoapAttributeType.XmlElement;
                attributeInfo.m_elementName    = xmlName;
                attributeInfo.m_nameSpace      = xmlNamespace;
            }

            if (SoapServices.GetXmlTypeForInteropType(type, out xmlName, out xmlNamespace))
            {
                attributeInfo.m_attributeType |= SoapAttributeType.XmlType;
                attributeInfo.m_typeName       = xmlName;
                attributeInfo.m_typeNamespace  = xmlNamespace;
            }
        } // ProcessTypeAttribute
Beispiel #6
0
        string GetQualifiedXmlType(XmlTextWriter tw, Type type, Type containerType)
        {
            string name, ns;

            if (type.IsArray)
            {
                name = GetXmlType(type);
                ns   = GetXmlNamespace(type, containerType);
            }
            else
            {
                name = GetXsdType(type);
                if (name != null)
                {
                    return("xsd:" + name);
                }

                if (!SoapServices.GetXmlTypeForInteropType(type, out name, out ns))
                {
                    SoapTypeAttribute att = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);
                    name = att.XmlTypeName;
                    ns   = att.XmlNamespace;
                }
            }

            return(GetQualifiedName(tw, ns, name));
        }
Beispiel #7
0
        public static void PreLoad(Type type)
        {
            // Register soap actions for the methods in the type.
            foreach (MethodInfo method in type.GetMethods())
            {
                RegisterSoapActionForMethodBase(method);
            }

            // Register XML tags for the type, if specified.
            SoapTypeAttribute tattr = (SoapTypeAttribute)
                                      InternalRemotingServices.GetCachedSoapAttribute(type);

            if (tattr.xmlElementWasSet)
            {
                RegisterInteropXmlElement
                    (tattr.XmlElementName, tattr.XmlNamespace, type);
            }
            if (tattr.xmlTypeWasSet)
            {
                RegisterInteropXmlType
                    (tattr.XmlTypeName, tattr.XmlTypeNamespace, type);
            }

            // Load and register the field mapping information.
            lock (typeof(SoapServices))
            {
                if (fields == null)
                {
                    fields = new Hashtable();
                }
                TypeFieldInfo typeInfo = new TypeFieldInfo();
                foreach (FieldInfo field in type.GetFields())
                {
                    SoapFieldAttribute fattr = (SoapFieldAttribute)
                                               InternalRemotingServices.GetCachedSoapAttribute
                                                   (field);
                    if (fattr.IsInteropXmlElement())
                    {
                        if (fattr.UseAttribute)
                        {
                            typeInfo.StoreAttribute
                                (XmlKey(fattr.XmlElementName,
                                        fattr.XmlNamespace),
                                field.Name, field.FieldType);
                        }
                        else
                        {
                            typeInfo.StoreElement
                                (XmlKey(fattr.XmlElementName,
                                        fattr.XmlNamespace),
                                field.Name, field.FieldType);
                        }
                    }
                }
                if (typeInfo.IsPopulated)
                {
                    fields[type] = typeInfo;
                }
            }
        }
Beispiel #8
0
    private XmlSerializer CreateOverrideSerializer()
    {
        SoapAttributeOverrides mySoapAttributeOverrides =
            new SoapAttributeOverrides();
        SoapAttributes mySoapAttributes = new SoapAttributes();

        SoapTypeAttribute mySoapType = new SoapTypeAttribute();

        mySoapType.TypeName       = "Team";
        mySoapAttributes.SoapType = mySoapType;
        // Add the SoapAttributes to the
        // mySoapAttributeOverridesrides object.
        mySoapAttributeOverrides.Add(typeof(Group), mySoapAttributes);
        // Get the SoapAttributes with the Item property.
        SoapAttributes thisSoapAtts =
            mySoapAttributeOverrides[typeof(Group)];

        Console.WriteLine("New serialized type name: " +
                          thisSoapAtts.SoapType.TypeName);

        // Create an XmlTypeMapping that is used to create an instance
        // of the XmlSerializer. Then return the XmlSerializer object.
        XmlTypeMapping myMapping = (new SoapReflectionImporter(
                                        mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));

        XmlSerializer ser = new XmlSerializer(myMapping);

        return(ser);
    }
Beispiel #9
0
        string GetXmlType(Type type)
        {
            if (type.IsArray)
            {
                string itemType = GetXmlType(type.GetElementType());
                itemType = "ArrayOf" + char.ToUpper(itemType[0]) + itemType.Substring(1);
                if (type.GetArrayRank() > 1)
                {
                    itemType += type.GetArrayRank();
                }
                return(itemType);
            }
            else
            {
                string name = null, ns;

                name = GetXsdType(type);
                if (name != null)
                {
                    return(name);
                }

                if (SoapServices.GetXmlTypeForInteropType(type, out name, out ns))
                {
                    return(name);
                }

                SoapTypeAttribute att = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);
                return(att.XmlTypeName);
            }
        }
Beispiel #10
0
    private XmlSerializer CreateOverrideSerializer()
    {
        SoapAttributeOverrides mySoapAttributeOverrides =
            new SoapAttributeOverrides();
        SoapAttributes soapAtts = new SoapAttributes();

        SoapElementAttribute mySoapElement = new SoapElementAttribute();

        mySoapElement.ElementName = "xxxx";
        soapAtts.SoapElement      = mySoapElement;
        mySoapAttributeOverrides.Add(typeof(Group), "PostitiveInt",
                                     soapAtts);

        // Override the IgnoreThis property.
        SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute();

        soapAtts            = new SoapAttributes();
        soapAtts.SoapIgnore = false;
        mySoapAttributeOverrides.Add(typeof(Group), "IgnoreThis",
                                     soapAtts);

        // Override the GroupType enumeration.
        soapAtts = new SoapAttributes();
        SoapEnumAttribute xSoapEnum = new SoapEnumAttribute();

        xSoapEnum.Name    = "Over1000";
        soapAtts.SoapEnum = xSoapEnum;

        // Add the SoapAttributes to the
        // mySoapAttributeOverridesrides object.
        mySoapAttributeOverrides.Add(typeof(GroupType), "A",
                                     soapAtts);

        // Create second enumeration and add it.
        soapAtts          = new SoapAttributes();
        xSoapEnum         = new SoapEnumAttribute();
        xSoapEnum.Name    = "ZeroTo1000";
        soapAtts.SoapEnum = xSoapEnum;
        mySoapAttributeOverrides.Add(typeof(GroupType), "B",
                                     soapAtts);

        // Override the Group type.
        soapAtts = new SoapAttributes();
        SoapTypeAttribute soapType = new SoapTypeAttribute();

        soapType.TypeName = "Team";
        soapAtts.SoapType = soapType;
        mySoapAttributeOverrides.Add(typeof(Group), soapAtts);

        // Create an XmlTypeMapping that is used to create an instance
        // of the XmlSerializer. Then return the XmlSerializer object.
        XmlTypeMapping myMapping = (new SoapReflectionImporter(
                                        mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));

        XmlSerializer ser = new XmlSerializer(myMapping);

        return(ser);
    }
Beispiel #11
0
        } // RegisterInteropXmlType

        /// <include file='doc\Soap.uex' path='docs/doc[@for="SoapServices.PreLoad"]/*' />
        public static void PreLoad(Type type)
        {
            // register soap action values
            MethodInfo[] methods = type.GetMethods();
            foreach (MethodInfo mi in methods)
            {
                // This will only add an entry to the table if SoapAction was explicitly set
                //   on the SoapMethodAttribute.
                RegisterSoapActionForMethodBase(mi);
            }

            // register interop xml elements and types if specified
            SoapTypeAttribute attr = (SoapTypeAttribute)
                                     InternalRemotingServices.GetCachedSoapAttribute(type);

            if (attr.IsInteropXmlElement())
            {
                RegisterInteropXmlElement(attr.XmlElementName, attr.XmlNamespace, type);
            }
            if (attr.IsInteropXmlType())
            {
                RegisterInteropXmlType(attr.XmlTypeName, attr.XmlTypeNamespace, type);
            }

            // construct field maps for mapping xml elements and attributes back to
            //   the correct type
            int mapCount          = 0;
            XmlToFieldTypeMap map = new XmlToFieldTypeMap();

            foreach (FieldInfo field in type.GetFields())
            {
                SoapFieldAttribute fieldAttr = (SoapFieldAttribute)
                                               InternalRemotingServices.GetCachedSoapAttribute(field);

                if (fieldAttr.IsInteropXmlElement())
                {
                    String xmlName      = fieldAttr.XmlElementName;
                    String xmlNamespace = fieldAttr.XmlNamespace;
                    if (fieldAttr.UseAttribute)
                    {
                        map.AddXmlAttribute(field.FieldType, field.Name, xmlName, xmlNamespace);
                    }
                    else
                    {
                        map.AddXmlElement(field.FieldType, field.Name, xmlName, xmlNamespace);
                    }

                    mapCount++;
                }
            } // foreach field

            // add field map if there is more than one entry
            if (mapCount > 0)
            {
                _xmlToFieldTypeMap[type] = map;
            }
        } // PreLoad
Beispiel #12
0
        public void TypeNameDefault()
        {
            SoapTypeAttribute attr = new SoapTypeAttribute();

            Assert.AreEqual(string.Empty, attr.TypeName, "#1");

            attr.TypeName = null;
            Assert.AreEqual(string.Empty, attr.TypeName, "#2");
        }
Beispiel #13
0
        public static void PreLoad(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
            }
            MethodInfo[] methods = type.GetMethods();
            foreach (MethodInfo mb in methods)
            {
                SoapServices.RegisterSoapActionForMethodBase(mb);
            }
            SoapTypeAttribute soapTypeAttribute = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);

            if (soapTypeAttribute.IsInteropXmlElement())
            {
                SoapServices.RegisterInteropXmlElement(soapTypeAttribute.XmlElementName, soapTypeAttribute.XmlNamespace, type);
            }
            if (soapTypeAttribute.IsInteropXmlType())
            {
                SoapServices.RegisterInteropXmlType(soapTypeAttribute.XmlTypeName, soapTypeAttribute.XmlTypeNamespace, type);
            }
            int num = 0;

            SoapServices.XmlToFieldTypeMap xmlToFieldTypeMap = new SoapServices.XmlToFieldTypeMap();
            foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                SoapFieldAttribute soapFieldAttribute = (SoapFieldAttribute)InternalRemotingServices.GetCachedSoapAttribute(fieldInfo);
                if (soapFieldAttribute.IsInteropXmlElement())
                {
                    string xmlElementName = soapFieldAttribute.XmlElementName;
                    string xmlNamespace   = soapFieldAttribute.XmlNamespace;
                    if (soapFieldAttribute.UseAttribute)
                    {
                        xmlToFieldTypeMap.AddXmlAttribute(fieldInfo.FieldType, fieldInfo.Name, xmlElementName, xmlNamespace);
                    }
                    else
                    {
                        xmlToFieldTypeMap.AddXmlElement(fieldInfo.FieldType, fieldInfo.Name, xmlElementName, xmlNamespace);
                    }
                    num++;
                }
            }
            if (num > 0)
            {
                SoapServices._xmlToFieldTypeMap[type] = xmlToFieldTypeMap;
            }
        }
Beispiel #14
0
        /// <summary>Returns XML type information that should be used when serializing the given <see cref="T:System.Type" />.</summary>
        /// <returns>true if the requested values have been set flagged with <see cref="T:System.Runtime.Remoting.Metadata.SoapTypeAttribute" />; otherwise, false.</returns>
        /// <param name="type">The object <see cref="T:System.Type" /> for which the XML element and namespace names were requested. </param>
        /// <param name="xmlType">The XML type of the specified object <see cref="T:System.Type" />. </param>
        /// <param name="xmlTypeNamespace">The XML type namespace of the specified object <see cref="T:System.Type" />. </param>
        /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
        /// </PermissionSet>
        public static bool GetXmlTypeForInteropType(Type type, out string xmlType, out string xmlTypeNamespace)
        {
            SoapTypeAttribute soapTypeAttribute = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);

            if (!soapTypeAttribute.IsInteropXmlType)
            {
                xmlType          = null;
                xmlTypeNamespace = null;
                return(false);
            }
            xmlType          = soapTypeAttribute.XmlTypeName;
            xmlTypeNamespace = soapTypeAttribute.XmlTypeNamespace;
            return(true);
        }
        public static void PreLoad(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
            }
            foreach (MethodInfo info in type.GetMethods())
            {
                RegisterSoapActionForMethodBase(info);
            }
            SoapTypeAttribute cachedSoapAttribute = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);

            if (cachedSoapAttribute.IsInteropXmlElement())
            {
                RegisterInteropXmlElement(cachedSoapAttribute.XmlElementName, cachedSoapAttribute.XmlNamespace, type);
            }
            if (cachedSoapAttribute.IsInteropXmlType())
            {
                RegisterInteropXmlType(cachedSoapAttribute.XmlTypeName, cachedSoapAttribute.XmlTypeNamespace, type);
            }
            int num = 0;
            XmlToFieldTypeMap map = new XmlToFieldTypeMap();

            foreach (FieldInfo info2 in type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                SoapFieldAttribute attribute2 = (SoapFieldAttribute)InternalRemotingServices.GetCachedSoapAttribute(info2);
                if (attribute2.IsInteropXmlElement())
                {
                    string xmlElementName = attribute2.XmlElementName;
                    string xmlNamespace   = attribute2.XmlNamespace;
                    if (attribute2.UseAttribute)
                    {
                        map.AddXmlAttribute(info2.FieldType, info2.Name, xmlElementName, xmlNamespace);
                    }
                    else
                    {
                        map.AddXmlElement(info2.FieldType, info2.Name, xmlElementName, xmlNamespace);
                    }
                    num++;
                }
            }
            if (num > 0)
            {
                _xmlToFieldTypeMap[type] = map;
            }
        }
        public static bool GetXmlElementForInteropType(Type type, out string xmlElement, out string xmlNamespace)
        {
            SoapTypeAttribute att = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);

            if (!att.IsInteropXmlElement)
            {
                xmlElement   = null;
                xmlNamespace = null;
                return(false);
            }

            xmlElement   = att.XmlElementName;
            xmlNamespace = att.XmlNamespace;
            return(true);
        }
Beispiel #17
0
        string GetXmlNamespace(Type t, Type containerType)
        {
            string name, ns;

            if (t.IsArray)
            {
                return(GetXmlNamespace(containerType, null));
            }

            if (SoapServices.GetXmlTypeForInteropType(t, out name, out ns))
            {
                return(ns);
            }

            SoapTypeAttribute att = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(t);

            return(att.XmlNamespace);
        }
        /// <summary>Gets an appropriate SOAP-related attribute for the specified class member or method parameter. </summary>
        /// <param name="reflectionObject">A class member or method parameter.</param>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
        /// </PermissionSet>
        public static SoapAttribute GetCachedSoapAttribute(object reflectionObject)
        {
            object        syncRoot = InternalRemotingServices._soapAttributes.SyncRoot;
            SoapAttribute result;

            lock (syncRoot)
            {
                SoapAttribute soapAttribute = InternalRemotingServices._soapAttributes[reflectionObject] as SoapAttribute;
                if (soapAttribute != null)
                {
                    result = soapAttribute;
                }
                else
                {
                    ICustomAttributeProvider customAttributeProvider = (ICustomAttributeProvider)reflectionObject;
                    object[] customAttributes = customAttributeProvider.GetCustomAttributes(typeof(SoapAttribute), true);
                    if (customAttributes.Length > 0)
                    {
                        soapAttribute = (SoapAttribute)customAttributes[0];
                    }
                    else if (reflectionObject is Type)
                    {
                        soapAttribute = new SoapTypeAttribute();
                    }
                    else if (reflectionObject is FieldInfo)
                    {
                        soapAttribute = new SoapFieldAttribute();
                    }
                    else if (reflectionObject is MethodBase)
                    {
                        soapAttribute = new SoapMethodAttribute();
                    }
                    else if (reflectionObject is ParameterInfo)
                    {
                        soapAttribute = new SoapParameterAttribute();
                    }
                    soapAttribute.SetReflectionObject(reflectionObject);
                    InternalRemotingServices._soapAttributes[reflectionObject] = soapAttribute;
                    result = soapAttribute;
                }
            }
            return(result);
        }
Beispiel #19
0
        public static SoapAttribute GetCachedSoapAttribute(object reflectionObject)
        {
            lock (_soapAttributes.SyncRoot)
            {
                SoapAttribute att = _soapAttributes [reflectionObject] as SoapAttribute;
                if (att != null)
                {
                    return(att);
                }

                ICustomAttributeProvider ap = (ICustomAttributeProvider)reflectionObject;
                object[] atts = ap.GetCustomAttributes(typeof(SoapAttribute), true);
                if (atts.Length > 0)
                {
                    att = (SoapAttribute)atts[0];
                }
                else
                {
                    if (reflectionObject is Type)
                    {
                        att = new SoapTypeAttribute();
                    }
                    else if (reflectionObject is FieldInfo)
                    {
                        att = new SoapFieldAttribute();
                    }
                    else if (reflectionObject is MethodBase)
                    {
                        att = new SoapMethodAttribute();
                    }
                    else if (reflectionObject is ParameterInfo)
                    {
                        att = new SoapParameterAttribute();
                    }
                }

                att.SetReflectionObject(reflectionObject);
                _soapAttributes [reflectionObject] = att;
                return(att);
            }
        }
Beispiel #20
0
        internal static void SerializeSoapMessage(IMessage msg, Stream outputStream, bool includeVersions)
        {
            SoapFormatter  formatter = CreateSoapFormatter(true, includeVersions);
            IMethodMessage message   = msg as IMethodMessage;

            if ((message != null) && (message.MethodBase != null))
            {
                SoapTypeAttribute cachedSoapAttribute = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(message.MethodBase.DeclaringType);
                if ((cachedSoapAttribute.SoapOptions & SoapOption.AlwaysIncludeTypes) == SoapOption.AlwaysIncludeTypes)
                {
                    formatter.TypeFormat |= FormatterTypeStyle.TypesAlways;
                }
                if ((cachedSoapAttribute.SoapOptions & SoapOption.XsdString) == SoapOption.XsdString)
                {
                    formatter.TypeFormat |= FormatterTypeStyle.XsdString;
                }
            }
            Header[] soapHeaders = GetSoapHeaders(msg);
            ((RemotingSurrogateSelector)formatter.SurrogateSelector).SetRootObject(msg);
            formatter.Serialize(outputStream, msg, soapHeaders);
        }
Beispiel #21
0
        public static bool GetXmlTypeForInteropType(Type type, out string xmlType, out string xmlTypeNamespace)
        {
            SoapServices.XmlEntry xmlEntry = (SoapServices.XmlEntry)SoapServices._interopTypeToXmlType[type];
            if (xmlEntry != null)
            {
                xmlType          = xmlEntry.Name;
                xmlTypeNamespace = xmlEntry.Namespace;
                return(true);
            }
            SoapTypeAttribute soapTypeAttribute = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);

            if (soapTypeAttribute.IsInteropXmlType())
            {
                xmlType          = soapTypeAttribute.XmlTypeName;
                xmlTypeNamespace = soapTypeAttribute.XmlTypeNamespace;
                return(true);
            }
            xmlType          = null;
            xmlTypeNamespace = null;
            return(false);
        }
Beispiel #22
0
 /// <include file='doc\SoapAttributes.uex' path='docs/doc[@for="SoapAttributes.SoapAttributes1"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public SoapAttributes(ICustomAttributeProvider provider)
 {
     object[] attrs = provider.GetCustomAttributes(false);
     for (int i = 0; i < attrs.Length; i++)
     {
         if (attrs[i] is SoapIgnoreAttribute || attrs[i] is ObsoleteAttribute)
         {
             _soapIgnore = true;
             break;
         }
         else if (attrs[i] is SoapElementAttribute)
         {
             _soapElement = (SoapElementAttribute)attrs[i];
         }
         else if (attrs[i] is SoapAttributeAttribute)
         {
             _soapAttribute = (SoapAttributeAttribute)attrs[i];
         }
         else if (attrs[i] is SoapTypeAttribute)
         {
             _soapType = (SoapTypeAttribute)attrs[i];
         }
         else if (attrs[i] is SoapEnumAttribute)
         {
             _soapEnum = (SoapEnumAttribute)attrs[i];
         }
         else if (attrs[i] is DefaultValueAttribute)
         {
             _soapDefaultValue = ((DefaultValueAttribute)attrs[i]).Value;
         }
     }
     if (_soapIgnore)
     {
         _soapElement      = null;
         _soapAttribute    = null;
         _soapType         = null;
         _soapEnum         = null;
         _soapDefaultValue = null;
     }
 }
        public static bool GetXmlElementForInteropType(Type type, out string xmlElement, out string xmlNamespace)
        {
            XmlEntry entry = (XmlEntry)_interopTypeToXmlElement[type];

            if (entry != null)
            {
                xmlElement   = entry.Name;
                xmlNamespace = entry.Namespace;
                return(true);
            }
            SoapTypeAttribute cachedSoapAttribute = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);

            if (cachedSoapAttribute.IsInteropXmlElement())
            {
                xmlElement   = cachedSoapAttribute.XmlElementName;
                xmlNamespace = cachedSoapAttribute.XmlNamespace;
                return(true);
            }
            xmlElement   = null;
            xmlNamespace = null;
            return(false);
        }
        internal static void ProcessTypeAttribute(Type type, SoapAttributeInfo attributeInfo)
        {
            string            str;
            string            str2;
            SoapTypeAttribute cachedSoapAttribute = (SoapTypeAttribute)InternalRemotingServices.GetCachedSoapAttribute(type);

            if (cachedSoapAttribute.Embedded)
            {
                attributeInfo.m_attributeType |= SoapAttributeType.Embedded;
            }
            if (SoapServices.GetXmlElementForInteropType(type, out str, out str2))
            {
                attributeInfo.m_attributeType |= SoapAttributeType.XmlElement;
                attributeInfo.m_elementName    = str;
                attributeInfo.m_nameSpace      = str2;
            }
            if (SoapServices.GetXmlTypeForInteropType(type, out str, out str2))
            {
                attributeInfo.m_attributeType |= SoapAttributeType.XmlType;
                attributeInfo.m_typeName       = str;
                attributeInfo.m_typeNamespace  = str2;
            }
        }
Beispiel #25
0
        [System.Security.SecurityCritical]  // auto-generated
        public static void PreLoad(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            Contract.EndContractBlock();

            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
            }

            // register soap action values
            MethodInfo[] methods = type.GetMethods();
            foreach (MethodInfo mi in methods)
            {
                // This will only add an entry to the table if SoapAction was explicitly set
                //   on the SoapMethodAttribute.
                RegisterSoapActionForMethodBase(mi);
            }

            // register interop xml elements and types if specified
            SoapTypeAttribute attr = (SoapTypeAttribute)
                                     InternalRemotingServices.GetCachedSoapAttribute(type);

            if (attr.IsInteropXmlElement())
            {
                RegisterInteropXmlElement(attr.XmlElementName, attr.XmlNamespace, type);
            }
            if (attr.IsInteropXmlType())
            {
                RegisterInteropXmlType(attr.XmlTypeName, attr.XmlTypeNamespace, type);
            }

            // construct field maps for mapping xml elements and attributes back to
            //   the correct type
            int mapCount          = 0;
            XmlToFieldTypeMap map = new XmlToFieldTypeMap();

            foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                SoapFieldAttribute fieldAttr = (SoapFieldAttribute)
                                               InternalRemotingServices.GetCachedSoapAttribute(field);

                if (fieldAttr.IsInteropXmlElement())
                {
                    String xmlName      = fieldAttr.XmlElementName;
                    String xmlNamespace = fieldAttr.XmlNamespace;
                    if (fieldAttr.UseAttribute)
                    {
                        map.AddXmlAttribute(field.FieldType, field.Name, xmlName, xmlNamespace);
                    }
                    else
                    {
                        map.AddXmlElement(field.FieldType, field.Name, xmlName, xmlNamespace);
                    }

                    mapCount++;
                }
            } // foreach field

            // add field map if there is more than one entry
            if (mapCount > 0)
            {
                _xmlToFieldTypeMap[type] = map;
            }
        } // PreLoad
        public static string ObjectToXmlString_second(Object _object, Type objType)
        {
            // Create and return an XmlSerializer instance used to
            // override and create SOAP messages.
            SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides();
            SoapAttributes         soapAtts = new SoapAttributes();

            // Override the SoapTypeAttribute.
            SoapTypeAttribute soapType = new SoapTypeAttribute();

            //soapType.TypeName = "Team";
            soapType.IncludeInSchema = false;
            //soapType.Namespace = "http://www.microsoft.com";

            // https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.soaptypeattribute.includeinschema?view=netframework-4.8
            // https://docs.microsoft.com/en-us/dotnet/standard/data/xml/
            // https://stackoverflow.com/questions/1729711/prevent-xmlserializer-from-emitting-xsitype-on-inherited-types/1730412#1730412
            // https://stackoverflow.com/questions/1729711/prevent-xmlserializer-from-emitting-xsitype-on-inherited-types/1730412
            // https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/dkwy2d72(v=vs.100)
            // https://csharp.hotexamples.com/examples/System.Xml/XmlWriterSettings/-/php-xmlwritersettings-class-examples.html

            //mySoapAttributeOverrides.Add(typeof(QuoteData), soapAtts);

            mySoapAttributeOverrides.Add(objType, soapAtts);


            // Serializes a class named Group as a SOAP message.
            XmlTypeMapping myTypeMapping =
                new SoapReflectionImporter(mySoapAttributeOverrides).ImportTypeMapping(objType);


            XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);
            //XmlSerializer mySerializer = new XmlSerializer(objType);

            XmlWriterSettings settings = new XmlWriterSettings();


            // this removes <?xml version="1.0" encoding="utf-16"?>
            //settings.ConformanceLevel = ConformanceLevel.Document;
            //settings.ConformanceLevel = ConformanceLevel.Auto;

            settings.OmitXmlDeclaration = true;

            settings.Indent = false;

            // this removes <?xml version="1.0" encoding="utf-16"?>
            //settings.OmitXmlDeclaration = true;

            settings.NewLineChars    = string.Empty;
            settings.NewLineHandling = NewLineHandling.None;

            string xmlStr = string.Empty;

            using (StringWriter stringWriter = new StringWriter())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
                {
                    mySerializer.Serialize(xmlWriter, _object);

                    xmlStr = stringWriter.ToString();
                    xmlWriter.Close();
                }
                stringWriter.Close();
            }

            return(xmlStr);
        }
Beispiel #27
0
        public void IncludeInSchemaDefault()
        {
            SoapTypeAttribute attr = new SoapTypeAttribute();

            Assert.AreEqual(true, attr.IncludeInSchema);
        }
Beispiel #28
0
        public void NamespaceDefault()
        {
            SoapTypeAttribute attr = new SoapTypeAttribute();

            Assert.IsNull(attr.Namespace);
        }
        // Get the cached SOAP attribute data for an object.
        public static SoapAttribute GetCachedSoapAttribute(Object reflectionObject)
        {
            // Validate the paramter to ensure that it is a
            // legitimate reflection object.
            if (reflectionObject == null)
            {
                return(null);
            }
            else if (!(reflectionObject is MemberInfo) &&
                     !(reflectionObject is ParameterInfo))
            {
                return(null);
            }
            lock (typeof(InternalRemotingServices))
            {
                Object   attr;
                Object[] attrs;

                // Look for a cached value from last time.
                if (attributeHash == null)
                {
                    attributeHash = new Hashtable();
                }
                else if ((attr = attributeHash[reflectionObject]) != null)
                {
                    return(attr as SoapAttribute);
                }

                // Get the attribute information from the type.
                if (reflectionObject is Type)
                {
                    attrs = ((Type)reflectionObject).GetCustomAttributes
                                (typeof(SoapTypeAttribute), true);
                    if (attrs == null || attrs.Length < 1)
                    {
                        attr = new SoapTypeAttribute();
                    }
                    else
                    {
                        attr = attrs[0];
                    }
                }
                else if (reflectionObject is MethodBase)
                {
                    attrs = ((MethodBase)reflectionObject)
                            .GetCustomAttributes
                                (typeof(SoapMethodAttribute), true);
                    if (attrs == null || attrs.Length < 1)
                    {
                        attr = new SoapMethodAttribute();
                    }
                    else
                    {
                        attr = attrs[0];
                    }
                }
                else if (reflectionObject is FieldInfo)
                {
                    attrs = ((FieldInfo)reflectionObject)
                            .GetCustomAttributes
                                (typeof(SoapFieldAttribute), true);
                    if (attrs == null || attrs.Length < 1)
                    {
                        attr = new SoapFieldAttribute();
                    }
                    else
                    {
                        attr = attrs[0];
                    }
                }
                else if (reflectionObject is ParameterInfo)
                {
                    attrs = ((ParameterInfo)reflectionObject)
                            .GetCustomAttributes
                                (typeof(SoapParameterAttribute), true);
                    if (attrs == null || attrs.Length < 1)
                    {
                        attr = new SoapParameterAttribute();
                    }
                    else
                    {
                        attr = attrs[0];
                    }
                }
                else
                {
                    attrs = ((MemberInfo)reflectionObject)
                            .GetCustomAttributes(typeof(SoapAttribute), true);
                    if (attrs == null || attrs.Length < 1)
                    {
                        attr = new SoapAttribute();
                    }
                    else
                    {
                        attr = attrs[0];
                    }
                }
                ((SoapAttribute)attr).SetReflectInfo(reflectionObject);
                return((SoapAttribute)attr);
            }
        }