Svcutil.exe - wsdl中的枚举未在c-sharp客户端中正确创建 [英] Svcutil.exe - enum from wsdl not created correctly in c-sharp client

查看:205
本文介绍了Svcutil.exe - wsdl中的枚举未在c-sharp客户端中正确创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < xsd:simpleType name =OurEnum> 
< xsd:annotation>
< xsd:appinfo>
< i:Base i:namespace =http://x.com/y/structures/2.0i:name =Object/>
< / xsd:appinfo>
< / xsd:annotation>
< xsd:restriction base =xsd:token>
< xsd:枚举值=0/>
< xsd:枚举值=1/>
< xsd:枚举值=2/>
< xsd:枚举值=3/>
< xsd:枚举值=10/>
< / xsd:restriction>



使用Svcutil创建WCF客户端,代码对于枚举看起来像这样:

  ///< remarks /> 
[System.CodeDom.Compiler.GeneratedCodeAttribute(System.Xml,4.0.30319.233)]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute Namespace =http://x.com/y/z/2.0)]
public enum OurEnum
{

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(0)]
Item0,

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(1)]
Item1,

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(10)]
Item10,

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(2)]
Item2,

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(3)]
Item3,

}

创建枚举值为Item0,Item1,Item10是非常讨厌的。应该如何使用Svcutil,以便生成的代码如下所示:

  ///< remarks /> 
[System.CodeDom.Compiler.GeneratedCodeAttribute(System.Xml,4.0.30319.233)]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute Namespace =http://x.com/y/z/2.0)]
public enum OurEnum
{

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(0)]
0,

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(1)]
1,

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(10)]
10,

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(2)]
2,

///< remarks />
[System.Xml.Serialization.XmlEnumAttribute(3)]
3,
}

我已经尝试使用Svcutil的以下序列化选项:



/ serializer:Auto
/ serializer:DataContractSerializer (失败不好)
/ serializer:XmlSerializer
/ importXmlTypes



但结果保持不变。



我还尝试使用Xsd.exe从wsdl引用的xsds创建c-sharp代码文件,但结果仍然保持不变。



有没有其他免费的工具,可以做到预期?



任何见解非常感谢!

解决方案

我已经解决了我的问题。这个想法取自于此处感谢和问候。



实际的问题是如何获得Item10,当给定值为10.从上述链接引用的解决方案提示时,我想出了使用以下方法,当传递XmlEnumAttribute中包含的值时,将返回枚举值:

  private static T GetEnumValueFromXmlAttrName&T ;(string attribVal)
{
T val = default(T);

if(typeof(T).BaseType.FullName.Equals(System.Enum))
{
FieldInfo [] fields = typeof(T).GetFields() ;

foreach(字段中的FieldInfo字段)
{
object [] attribs = field.GetCustomAttributes(typeof(XmlEnumAttribute),false);

foreach(对象attr in attribs)
{
if((attr as XmlEnumAttribute).Name.Equals(attribVal))
{
val = (T)field.GetValue(NULL);
return val;
}
}
}
}
else
抛出新异常(提供的类型不是枚举);

return val;
}

希望这有帮助!


I have a client supplied wsdl file containing an enum:

<xsd:simpleType name="OurEnum">
<xsd:annotation>
  <xsd:appinfo>
    <i:Base i:namespace="http://x.com/y/structures/2.0" i:name="Object"/>
  </xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:token">
  <xsd:enumeration value="0"/>
  <xsd:enumeration value="1"/>
  <xsd:enumeration value="2"/>
  <xsd:enumeration value="3"/>
  <xsd:enumeration value="10"/>
</xsd:restriction>

Using Svcutil to create the WCF client, the code for enum looks like this:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://x.com/y/z/2.0")]
public enum OurEnum
{

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("0")]
    Item0,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("1")]
    Item1,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("10")]
    Item10,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("2")]
    Item2,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("3")]
    Item3,

 }

The creation of enum values as Item0, Item1, Item10 is very annoying. How should the Svcutil be used so that the generated code looks like:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://x.com/y/z/2.0")]
public enum OurEnum
{

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("0")]
    0,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("1")]
    1,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("10")]
    10,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("2")]
    2,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("3")]
    3,
 }

I have already tried using following serialization options for with Svcutil:

"/serializer:Auto" "/serializer:DataContractSerializer (failed badly)" "/serializer:XmlSerializer" "/importXmlTypes"

but the result remains the same.

I have also tried using Xsd.exe to create c-sharp code files from the xsds referred in the wsdl, but the result still remains the same.

Is there any other free tool out there, that can do the expected?

Any insight is greatly appreciated!

解决方案

I have had a work around to my problem. The idea is taken from here with due thanks and regards.

The actual problem was how to get Item10, when given a value of 10. Taking a cue from the solution cited at the above link, I came up with the following method, which when passed a value contained in XmlEnumAttribute, would return the enum value:

private static T GetEnumValueFromXmlAttrName<T>(string attribVal)
    {
        T val = default(T);

        if (typeof(T).BaseType.FullName.Equals("System.Enum"))
        {
            FieldInfo[] fields = typeof(T).GetFields();

            foreach (FieldInfo field in fields)
            {
                object[] attribs = field.GetCustomAttributes(typeof(XmlEnumAttribute), false);

                foreach (object attr in attribs)
                {
                    if ((attr as XmlEnumAttribute).Name.Equals(attribVal))
                    {
                        val = (T)field.GetValue(null);
                        return val;
                    }
                }
            }
        }
        else
            throw new Exception("The supplied type is not an Enum.");

        return val;
    }

Hope this helps!

这篇关于Svcutil.exe - wsdl中的枚举未在c-sharp客户端中正确创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆