我需要从以下类中读取XmlTypeAttribute,以获取Namespace值:

<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42"),  _
System.SerializableAttribute(),  _
System.Diagnostics.DebuggerStepThroughAttribute(),  _
System.ComponentModel.DesignerCategoryAttribute("code"),  _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://webservices.micros.com/ows/5.1/Availability.wsdl")>  _
Partial Public Class AvailabilityRequest
Inherits AvailRequestSegmentList

Private summaryOnlyField As Boolean

Private xsnField As System.Xml.Serialization.XmlSerializerNamespaces

'''<comentarios/>
<System.Xml.Serialization.XmlAttributeAttribute()>  _
Public Property summaryOnly() As Boolean
    Get
        Return Me.summaryOnlyField
    End Get
    Set
        Me.summaryOnlyField = value
    End Set
End Property

使用下面的代码,我可以获取System.SerializableAttribute的值,但无法检索有关XmlTypeAttribute的信息。
var ar = typeof (AvailabilityRequest).GetType();
ar.GetCustomAttributes(true);

更新2011.12.29
下面的代码现在可以工作了:
    var xmlAttribute = (XmlTypeAttribute)Attribute.GetCustomAttribute(
                          typeof(AvailabilityRequest),
                          typeof(XmlTypeAttribute)
                       );
   XNamespace ns = xmlAttribute.Namespace;
   ns.NamespaceName.Should().Be.EqualTo("http://webservices.micros.com/ows/5.1/Availability.wsdl");

最佳答案

attribute类具有名为getcustomattribute的静态方法。
用法如下:

XmlTypeAttribute xmlAttribute = (XmlTypeAttribute)Attribute.GetCustomAttribute(
                                   typeof(theType),
                                   typeof(XmlTypeAttribute)
                                 );
XNamespace ns = xmlAttribute.Namespace;

关于xml - 如何动态读取XmlTypeAttribute类以获取命名空间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5706725/

10-09 23:45