Exemplo n.º 1
0
        /// <summary>
        /// Given a Wsdl Message part element returns the name of the request or response element
        /// wrapper. If a message part is not found return the name of the action with "Request" or "Response"
        /// appended.
        /// </summary>
        /// <param name="message">A Wsdl Message element.</param>
        /// <returns>The name of the Clr type used by the message element.</returns>
        internal static string GetMessageElementName(ServiceDescription svcDesc, Message message)
        {
            // If there is no message part(s) the message is void so return null
            if (message.Parts.Count == 0)
            {
                return(null);
            }

            // If the message part contains a type reference
            XmlSchemaType type     = null;
            string        typeName = null;

            if (message.Parts[0].Element == null || message.Parts[0].Element.IsEmpty)
            {
                type = FindSchemaType(svcDesc, message.Parts[0].Type.Name, message.Parts[0].Type.Namespace);
                if (type.TypeCode == XmlTypeCode.None)
                {
                    return(message.Parts[0].Type.Name);
                }
                typeName = message.Parts[0].Type.Name;
            }
            // If the message part contains an element reference
            else
            {
                XmlSchemaElement element = FindSchemaElement(svcDesc, message.Parts[0].Element.Name, message.Parts[0].Element.Namespace);
                if (element == null || element.SchemaTypeName == null || element.SchemaTypeName.Name == "")
                {
                    return(message.Parts[0].Element.Name);
                }

                type     = element.ElementSchemaType;
                typeName = message.Parts[0].Element.Name;
            }

            string clrTypeName = CodeGenUtils.GetClrType(typeName);

            clrTypeName = clrTypeName != null && clrTypeName.IndexOf("System.") == 0 ? clrTypeName.Substring(7) : clrTypeName;
            return(clrTypeName == null ? typeName : clrTypeName);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Given a Wsdl Message part element returns the type refered to by the message part.
        /// </summary>
        /// <param name="message">A Wsdl Message element.</param>
        /// <returns>The name of the type used by the message element.</returns>
        internal static string GetMessageTypeName(ServiceDescription svcDesc, Message message)
        {
            // If there are no message part(s) the message is void so return null
            if (message.Parts.Count == 0)
            {
                return(null);
            }

            // If the message contains a type reference instead of an element reference find the schema type
            XmlSchemaType type        = null;
            string        typeName    = null;
            string        elementName = null;

            if (message.Parts[0].Element == null || message.Parts[0].Element.IsEmpty)
            {
                type = FindSchemaType(svcDesc, message.Parts[0].Type.Name, message.Parts[0].Type.Namespace);
                if (type == null || type.TypeCode == XmlTypeCode.None)
                {
                    return(message.Parts[0].Type.Name);
                }
                typeName = message.Parts[0].Type.Name;
            }
            else
            {
                XmlSchemaElement element = FindSchemaElement(svcDesc, message.Parts[0].Element.Name, message.Parts[0].Element.Namespace);
                if (element == null || element.SchemaTypeName == null || element.SchemaTypeName.Name == "")
                {
                    return(message.Parts[0].Element.Name);
                }

                elementName = element.QualifiedName.Name;
                type        = element.ElementSchemaType;
                typeName    = element.SchemaTypeName.Name;
            }

            // If this is a complex type return the element type name
            if (type is XmlSchemaComplexType)
            {
                return(elementName == null ? type.Name : elementName);
            }

            bool IsEnum = false;

            if (type.DerivedBy == XmlSchemaDerivationMethod.Restriction)
            {
                XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)((XmlSchemaSimpleType)type).Content;
                foreach (XmlSchemaFacet facet in restriction.Facets)
                {
                    if (facet is XmlSchemaEnumerationFacet)
                    {
                        IsEnum = true;
                        break;
                    }
                }
            }

            // If this is a simple type with enumeration restrictions, return the type name.
            // Else return the clr type name if this is a native type or the simple type name if this is not a native type.
            if (IsEnum == true)
            {
                return(typeName);
            }
            else
            {
                string clrTypeName = CodeGenUtils.GetClrType(type.TypeCode);
                clrTypeName = clrTypeName != null && clrTypeName.IndexOf("System.") == 0 ? clrTypeName.Substring(7) : clrTypeName;
                if (clrTypeName == null)
                {
                    return(typeName);
                }
                else if (type.Datatype.ValueType.IsArray)
                {
                    string typeSuffix = (clrTypeName.Length > 2 && clrTypeName.Substring(clrTypeName.Length - 2) == "[]") ? "" : "[]";
                    return(clrTypeName + typeSuffix);
                }
                else
                {
                    return(clrTypeName);
                }
            }
        }