[System.Security.SecuritySafeCritical]  // auto-generated
        internal static bool CanCastToXmlTypeHelper(RuntimeType castType, MarshalByRefObject o)
        {
            if (castType == null)
            {
                throw new ArgumentNullException("castType");
            }

            Contract.EndContractBlock();
            // MarshalByRefObject's can only be casted to MarshalByRefObject's or interfaces.
            if (!castType.IsInterface && !castType.IsMarshalByRef)
            {
                return(false);
            }

            // figure out xml type name
            String xmlTypeName      = null;
            String xmlTypeNamespace = null;

            if (!SoapServices.GetXmlTypeForInteropType(castType, out xmlTypeName, out xmlTypeNamespace))
            {
                // There's no registered interop type name, so just use the default.
                xmlTypeName      = castType.Name;
                xmlTypeNamespace =
                    SoapServices.CodeXmlNamespaceForClrTypeNamespace(
                        castType.Namespace, castType.GetRuntimeAssembly().GetSimpleName());
            }

            return(o.CanCastToXmlType(xmlTypeName, xmlTypeNamespace));
        } // CanCastToXmlType
예제 #2
0
        internal override void SetReflectionObject(object reflectionObject)
        {
            Type type = (Type)reflectionObject;

            if (_xmlElementName == null)
            {
                _xmlElementName = type.Name;
            }

            if (_xmlTypeName == null)
            {
                _xmlTypeName = type.Name;
            }

            if (_xmlTypeNamespace == null)
            {
                string na;
                if (type.Assembly == typeof(object).Assembly)
                {
                    na = string.Empty;
                }
                else
                {
                    na = type.Assembly.GetName().Name;
                }
                _xmlTypeNamespace = SoapServices.CodeXmlNamespaceForClrTypeNamespace(type.Namespace, na);
            }

            if (_xmlNamespace == null)
            {
                _xmlNamespace = _xmlTypeNamespace;
            }
        }
예제 #3
0
        public void TestObjects()
        {
            object objReturn;

            objReturn = Serialize("");
            objReturn = Serialize("hello world!");
            Assertion.AssertEquals("#string", "hello world!", objReturn);
            SoapMessage soapMsg = new SoapMessage();

            soapMsg.Headers      = new Header[0];
            soapMsg.MethodName   = "Equals";
            soapMsg.ParamNames   = new String[0];
            soapMsg.ParamTypes   = new Type[0];
            soapMsg.ParamValues  = new object[0];
            soapMsg.XmlNameSpace = SoapServices.CodeXmlNamespaceForClrTypeNamespace("String", "System");
            _soapFormatterDeserializer.TopObject = new SoapMessage();
            objReturn = Serialize(soapMsg);
            _soapFormatterDeserializer.TopObject = null;
            SimpleObject obj = new SimpleObject("simple object", 1);

            objReturn = Serialize(obj);
            Assertion.AssertEquals("#SimpleObject", obj, objReturn);
            objReturn = Serialize(typeof(SimpleObject));
            Assertion.AssertEquals("#Type", typeof(SimpleObject), (Type)objReturn);
            objReturn = Serialize(obj.GetType().Assembly);
            Assertion.AssertEquals("#Assembly", obj.GetType().Assembly, objReturn);
        }
예제 #4
0
        public Element GetXmlElement(string typeFullName, string assemblyName)
        {
            Element element;
            string  typeNamespace = string.Empty;
            string  typeName      = typeFullName;

            if (_assemblyFormat == FormatterAssemblyStyle.Simple)
            {
                string[] items = assemblyName.Split(',');
                assemblyName = items[0];
            }
            string assemblyQualifiedName = typeFullName + ", " + assemblyName;

            element = (Element)typeToXmlNodeTable[assemblyQualifiedName];
            if (element == null)
            {
                int typeNameIndex = typeFullName.LastIndexOf('.');
                if (typeNameIndex != -1)
                {
                    typeNamespace = typeFullName.Substring(0, typeNameIndex);
                    typeName      = typeFullName.Substring(typeNamespace.Length + 1);
                }
                string namespaceURI =
                    SoapServices.CodeXmlNamespaceForClrTypeNamespace(
                        typeNamespace,
                        (!assemblyName.StartsWith("mscorlib"))?assemblyName:String.Empty);
                string prefix = (string)namespaceToPrefixTable[namespaceURI];
                if (prefix == null || prefix == string.Empty)
                {
                    prefix = "a" + (_prefixNumber++).ToString();
                    namespaceToPrefixTable[namespaceURI] = prefix;
                }

                int i = typeName.IndexOf("[");
                if (i != -1)
                {
                    typeName = XmlConvert.EncodeName(typeName.Substring(0, i)) + typeName.Substring(i);
                }
                else
                {
                    int j = typeName.IndexOf("&");
                    if (j != -1)
                    {
                        typeName = XmlConvert.EncodeName(typeName.Substring(0, j)) + typeName.Substring(j);
                    }
                    else
                    {
                        typeName = XmlConvert.EncodeName(typeName);
                    }
                }

                element = new Element(
                    prefix,
                    typeName,
                    namespaceURI);
            }
            return(element);
        }
예제 #5
0
        private DottedInfo ParseAssemblyName(string typeFullName, string assemName)
        {
            string typeNamespace = null;
            string str2          = null;
            string key           = null;
            int    num;

            if (this.typeNameToDottedInfoTable.ContainsKey(typeFullName))
            {
                return((DottedInfo)this.typeNameToDottedInfoTable[typeFullName]);
            }
            int length = typeFullName.LastIndexOf('.');

            if (length > 0)
            {
                typeNamespace = typeFullName.Substring(0, length);
            }
            else
            {
                typeNamespace = "";
            }
            key  = SoapServices.CodeXmlNamespaceForClrTypeNamespace(typeNamespace, assemName);
            str2 = typeFullName.Substring(length + 1);
            if (this.dottedAssemToAssemIdTable.ContainsKey(key))
            {
                num = (int)this.dottedAssemToAssemIdTable[key];
            }
            else
            {
                num = this.dottedAssemId++;
                this.assemblyInfos[this.AssemIdToString(num)] = new AssemblyInfo(num, key, false);
                this.dottedAssemToAssemIdTable[key]           = num;
            }
            DottedInfo info = new DottedInfo {
                dottedAssemblyName = key,
                name      = str2,
                nameSpace = typeNamespace,
                assemId   = num
            };

            this.typeNameToDottedInfoTable[typeFullName] = info;
            return(info);
        }
예제 #6
0
        internal static bool CanCastToXmlTypeHelper(RuntimeType castType, MarshalByRefObject o)
        {
            if (castType == null)
            {
                throw new ArgumentNullException("castType");
            }
            if (!castType.IsInterface && !castType.IsMarshalByRef)
            {
                return(false);
            }
            string xmlType          = null;
            string xmlTypeNamespace = null;

            if (!SoapServices.GetXmlTypeForInteropType(castType, out xmlType, out xmlTypeNamespace))
            {
                xmlType          = castType.Name;
                xmlTypeNamespace = SoapServices.CodeXmlNamespaceForClrTypeNamespace(castType.Namespace, castType.GetRuntimeAssembly().GetSimpleName());
            }
            return(o.CanCastToXmlType(xmlType, xmlTypeNamespace));
        }
예제 #7
0
    public static void Main(string[] args)
    {
        //<snippet101>
        // Convert a CLR namespace and assembly name into an XML namespace.
        string xmlNamespace =
            SoapServices.CodeXmlNamespaceForClrTypeNamespace(
                "ExampleNamespace", "AssemblyName");

        Console.WriteLine("The name of the XML namespace is {0}.",
                          xmlNamespace);
        //</snippet101>

        //<snippet102>
        // Extract a CLR namespace and assembly name from an XML namespace.
        string typeNamespace;
        string assemblyName;

        SoapServices.DecodeXmlNamespaceForClrTypeNamespace(xmlNamespace,
                                                           out typeNamespace, out assemblyName);
        Console.WriteLine("The name of the CLR namespace is {0}.",
                          typeNamespace);
        Console.WriteLine("The name of the CLR assembly is {0}.",
                          assemblyName);
        //</snippet102>

        //<snippet103>
        // Get the XML element name and the XML namespace for
        // an Interop type.
        string xmlElement;
        bool   isSoapTypeAttribute =
            SoapServices.GetXmlElementForInteropType(
                typeof(ExampleNamespace.ExampleClass),
                out xmlElement, out xmlNamespace);

        // Print whether the requested value was flagged
        // with a SoapTypeAttribute.
        if (isSoapTypeAttribute)
        {
            Console.WriteLine(
                "The requested value was flagged " +
                "with the SoapTypeAttribute.");
        }
        else
        {
            Console.WriteLine(
                "The requested value was not flagged " +
                "with the SoapTypeAttribute.");
        }

        // Print the XML element and the XML namespace.
        Console.WriteLine(
            "The XML element for the type " +
            "ExampleNamespace.ExampleClass is {0}.",
            xmlElement);
        Console.WriteLine(
            "The XML namespace for the type " +
            "ExampleNamespace.ExampleClass is {0}.",
            xmlNamespace);
        //</snippet103>

        //<snippet104>
        // Get the XML type name and the XML type namespace for
        // an Interop type.
        string xmlTypeName;
        string xmlTypeNamespace;

        isSoapTypeAttribute =
            SoapServices.GetXmlTypeForInteropType(
                typeof(ExampleNamespace.ExampleClass),
                out xmlTypeName, out xmlTypeNamespace);

        // Print whether the requested value was flagged
        // with a SoapTypeAttribute.
        if (isSoapTypeAttribute)
        {
            Console.WriteLine(
                "The requested value was flagged " +
                "with the SoapTypeAttribute.");
        }
        else
        {
            Console.WriteLine(
                "The requested value was not flagged " +
                "with the SoapTypeAttribute.");
        }

        // Print the XML type name and the XML type namespace.
        Console.WriteLine(
            "The XML type for the type " +
            "ExampleNamespace.ExampleClass is {0}.",
            xmlTypeName);
        Console.WriteLine(
            "The XML type namespace for the type " +
            "ExampleNamespace.ExampleClass is {0}.",
            xmlTypeNamespace);
        //</snippet104>

        //<snippet105>
        // Print the XML namespace for a method invocation and its
        // response.
        System.Reflection.MethodBase getHelloMethod =
            typeof(ExampleNamespace.ExampleClass).GetMethod("GetHello");
        string methodCallXmlNamespace =
            SoapServices.GetXmlNamespaceForMethodCall(getHelloMethod);
        string methodResponseXmlNamespace =
            SoapServices.GetXmlNamespaceForMethodResponse(getHelloMethod);

        Console.WriteLine(
            "The XML namespace for the invocation of the method " +
            "GetHello in ExampleClass is {0}.",
            methodResponseXmlNamespace);
        Console.WriteLine(
            "The XML namespace for the response of the method " +
            "GetHello in ExampleClass is {0}.",
            methodCallXmlNamespace);
        //</snippet105>

        //<snippet106>
        // Determine whether an XML namespace represents a CLR namespace.
        string clrNamespace = SoapServices.XmlNsForClrType;

        if (SoapServices.IsClrTypeNamespace(clrNamespace))
        {
            Console.WriteLine(
                "The namespace {0} is a CLR namespace.",
                clrNamespace);
        }
        else
        {
            Console.WriteLine(
                "The namespace {0} is not a CLR namespace.",
                clrNamespace);
        }
        //</snippet106>

        //<snippet130>
        // Print the XML namespace for the CLR types.
        Console.WriteLine(
            "The XML namespace for the CLR types " +
            "is {0}.",
            SoapServices.XmlNsForClrType);
        //</snippet130>

        //<snippet131>
        // Print the XML namespace for the CLR types
        // that have an assembly but no common language runtime namespace.
        Console.WriteLine(
            "The XML namespace for the CLR types " +
            "that have an assembly but no namespace, is {0}.",
            SoapServices.XmlNsForClrTypeWithAssembly);
        //</snippet131>

        //<snippet132>
        // Print the XML namespace for the CLR types
        // that are a part of the Mscorlib.dll.
        Console.WriteLine(
            "The XML namespace for the CLR types " +
            "that are part of the Mscorlib.dll, is {0}.",
            SoapServices.XmlNsForClrTypeWithNs);
        //</snippet132>

        //<snippet133>
        // Print the XML namespace for the CLR types
        // that have both an assembly and a common language runtime
        // namespace.
        Console.WriteLine(
            "The XML namespace for the CLR types " +
            "that have both an assembly and a namespace, is {0}.",
            SoapServices.XmlNsForClrTypeWithNsAndAssembly);
        //</snippet133>

        //<snippet140>
        // Get the SOAP action for the method.
        System.Reflection.MethodBase getHelloMethodBase =
            typeof(ExampleNamespace.ExampleClass).GetMethod("GetHello");
        string getHelloSoapAction =
            SoapServices.GetSoapActionFromMethodBase(getHelloMethodBase);

        Console.WriteLine(
            "The SOAP action for the method " +
            "ExampleClass.GetHello is {0}.", getHelloSoapAction);
        bool isSoapActionValid = SoapServices.IsSoapActionValidForMethodBase(
            getHelloSoapAction,
            getHelloMethodBase);

        if (isSoapActionValid)
        {
            Console.WriteLine(
                "The SOAP action, {0}, " +
                "is valid for ExampleClass.GetHello",
                getHelloSoapAction);
        }
        else
        {
            Console.WriteLine(
                "The SOAP action, {0}, " +
                "is not valid for ExampleClass.GetHello",
                getHelloSoapAction);
        }

        // Register the SOAP action for the GetHello method.
        SoapServices.RegisterSoapActionForMethodBase(getHelloMethodBase);

        // Get the type and the method names encoded into the SOAP action.
        string encodedTypeName;
        string encodedMethodName;

        SoapServices.GetTypeAndMethodNameFromSoapAction(
            getHelloSoapAction,
            out encodedTypeName,
            out encodedMethodName);
        Console.WriteLine(
            "The type name encoded in this SOAP action is {0}.",
            encodedTypeName);
        Console.WriteLine(
            "The method name encoded in this SOAP action is {0}.",
            encodedMethodName);
        //</snippet140>

        //<snippet150>
        // Get the name and the type of the field using its XML
        // element name and its XML namespace. For this query to work,
        // the containing type must be preloaded, and the XML element
        // name and the XML namespace must be explicitly declared on
        // the field using a SoapFieldAttribute.

        // Preload the containing type.
        SoapServices.PreLoad(typeof(ExampleNamespace.ExampleClass));

        // Get the name and the type of a field that will be
        // serialized as an XML element.
        Type   containingType      = typeof(ExampleNamespace.ExampleClass);
        string xmlElementNamespace =
            "http://example.org/ExampleFieldNamespace";
        string xmlElementName = "ExampleFieldElementName";
        Type   fieldType;
        string fieldName;

        SoapServices.GetInteropFieldTypeAndNameFromXmlElement(
            containingType, xmlElementName, xmlElementNamespace,
            out fieldType, out fieldName);
        Console.WriteLine(
            "The type of the field is {0}.",
            fieldType);
        Console.WriteLine(
            "The name of the field is {0}.",
            fieldName);

        // Get the name and the type of a field that will be
        // serialized as an XML attribute.
        string xmlAttributeNamespace =
            "http://example.org/ExampleAttributeNamespace";
        string xmlAttributeName = "ExampleFieldAttributeName";

        SoapServices.GetInteropFieldTypeAndNameFromXmlAttribute(
            containingType, xmlAttributeName, xmlAttributeNamespace,
            out fieldType, out fieldName);
        Console.WriteLine(
            "The type of the field is {0}.",
            fieldType);
        Console.WriteLine(
            "The name of the field is {0}.",
            fieldName);
        //</snippet150>

        //<snippet160>
        string interopTypeXmlElementName =
            "ExampleClassElementName";
        string interopTypeXmlNamespace =
            "http://example.org/ExampleXmlNamespace";
        Type interopType = SoapServices.GetInteropTypeFromXmlElement(
            interopTypeXmlElementName,
            interopTypeXmlNamespace);

        Console.WriteLine("The interop type is {0}.", interopType);

        string interopTypeXmlTypeName =
            "ExampleXmlTypeName";
        string interopTypeXmlTypeNamespace =
            "http://example.org/ExampleXmlTypeNamespace";

        interopType = SoapServices.GetInteropTypeFromXmlType(
            interopTypeXmlTypeName, interopTypeXmlTypeNamespace);
        Console.WriteLine("The interop type is {0}.", interopType);
        //</snippet160>

        //<snippet170>
        // Get the method base object for the GetHello method.
        System.Reflection.MethodBase methodBase =
            typeof(ExampleNamespace.ExampleClass).GetMethod("GetHello");

        // Print its current SOAP action.
        Console.WriteLine(
            "The SOAP action for the method " +
            "ExampleClass.GetHello is {0}.",
            SoapServices.GetSoapActionFromMethodBase(methodBase));

        // Set the SOAP action of the GetHello method to a new value.
        string newSoapAction =
            "http://example.org/ExampleSoapAction#NewSoapAction";

        SoapServices.RegisterSoapActionForMethodBase(
            methodBase, newSoapAction);
        Console.WriteLine(
            "The SOAP action for the method " +
            "ExampleClass.GetHello is {0}.",
            SoapServices.GetSoapActionFromMethodBase(methodBase));

        // Reset the SOAP action of the GetHello method to its default
        // value, which is determined using its SoapMethod attribute.
        SoapServices.RegisterSoapActionForMethodBase(methodBase);
        Console.WriteLine(
            "The SOAP action for the method " +
            "ExampleClass.GetHello is {0}.",
            SoapServices.GetSoapActionFromMethodBase(methodBase));
        //</snippet170>

        //<snippet120>
        // Register all types in the assembly with the SoapType attribute.
        System.Reflection.Assembly executingAssembly =
            System.Reflection.Assembly.GetExecutingAssembly();
        SoapServices.PreLoad(executingAssembly);
        //</snippet120>

        //<snippet121>
        // Register a specific type with the SoapType attribute.
        Type exampleType = typeof(ExampleNamespace.ExampleClass);

        SoapServices.PreLoad(exampleType);
        //</snippet121>

        //<snippet180>
        // Get the currently registered type for the given XML element
        // and namespace.
        string registeredXmlElementName =
            "ExampleClassElementName";
        string registeredXmlNamespace =
            "http://example.org/ExampleXmlNamespace";
        Type registeredType =
            SoapServices.GetInteropTypeFromXmlElement(
                registeredXmlElementName,
                registeredXmlNamespace);

        Console.WriteLine(
            "The registered interop type is {0}.",
            registeredType);

        // Register a new type for the XML element and namespace.
        SoapServices.RegisterInteropXmlElement(
            registeredXmlElementName,
            registeredXmlNamespace,
            typeof(String));

        // Get the currently registered type for the given XML element
        // and namespace.
        registeredType =
            SoapServices.GetInteropTypeFromXmlElement(
                registeredXmlElementName,
                registeredXmlNamespace);
        Console.WriteLine(
            "The registered interop type is {0}.",
            registeredType);
        //</snippet180>

        //<snippet190>
        // Get the currently registered type for the given XML element
        // and namespace.
        string registeredXmlTypeName =
            "ExampleXmlTypeName";
        string registeredXmlTypeNamespace =
            "http://example.org/ExampleXmlTypeNamespace";

        registeredType =
            SoapServices.GetInteropTypeFromXmlType(
                registeredXmlTypeName,
                registeredXmlTypeNamespace);
        Console.WriteLine(
            "The registered interop type is {0}.",
            registeredType);

        // Register a new type for the XML element and namespace.
        SoapServices.RegisterInteropXmlType(
            registeredXmlTypeName,
            registeredXmlTypeNamespace,
            typeof(String));

        // Get the currently registered type for the given XML element
        // and namespace.
        registeredType =
            SoapServices.GetInteropTypeFromXmlType(
                registeredXmlTypeName,
                registeredXmlTypeNamespace);
        Console.WriteLine(
            "The registered interop type is {0}.",
            registeredType);
        //</snippet190>
    }