开发者

How can I dynamically read a classes XmlTypeAttribute to get the Namespace?

开发者 https://www.devze.com 2023-02-26 05:51 出处:网络
I need to read the XmlTypeAttribute from the following class, to get the Namespace value: <System.CodeDom.Compiler.GeneratedCodeAttribute(\"wsdl\", \"2.0.50727.42\"),_

I need to read the XmlTypeAttribute from the following class, to get the Namespace value:

<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

With the following code, I can get a value for the System.SerializableAttribute but I cannot retrieve information about XmlTypeAttribute.

var ar = typeof (AvailabilityRequest).GetType();
ar.GetCustomAttributes(true);

Update 2011.12.29

The following code now works:

    var xmlAttribute = (XmlTypeAttribute)Attribute.GetCustomAttribu开发者_开发技巧te(
                          typeof(AvailabilityRequest),
                          typeof(XmlTypeAttribute)
                       );
   XNamespace ns = xmlAttribute.Namespace;
   ns.NamespaceName.Should().Be.EqualTo("http://webservices.micros.com/ows/5.1/Availability.wsdl");


The Attribute class has static method called GetCustomAttribute.

Here's the usage:

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

精彩评论

暂无评论...
验证码 换一张
取 消