Tuesday, August 26, 2008

How to Determine If an Enumeration Is a Bit Field

In .NET you can tell the compiler to treat an enumeration as a bit field by adding a Flags attribute to its declaration. For more information about bit fields in .NET see http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspxhttps://docs.microsoft.com/en-us/dotnet/api/system.flagsattribute. The following function will tell you if an enumeration value is a bit (or flag) field.

Function IsFlagEnum(ByVal value As [Enum]) As Boolean

    If value.GetType().IsDefined( _
       
GetType(FlagsAttribute), True) = True Then
        Return True
    Else
        Return False
    End If

End
Function


For example, given the following definitions:

<Flags()> _
Public Enum FlagEnum
    None = 0
    ValueA = 1
    ValueB = 2
    ValueC = 4
End Enum

Public
Enum RegularEnum
    Invalid = 0
    ValueA = 1
    ValueB = 2
    ValueC = 3
End Enum


IsFlagEnum(FlagEnum.ValueB) will return True, but IsFlagEnum(RegularEnum.ValueB) will return False.
 

 

Thursday, July 17, 2008

Get the XmlEnum Value for an Enum

When you serialize an enumeration (Enum in VB.NET, enum in C#), by default the XML string representation of the enumerated member is the name of the enumeration member (i.e. the same as calling the ToString function of the enumeration member). For example, if the Color property of the following object was set to Green, the XML representation of that property would be <Color>Green</Color>:

Public Enum PrimaryColor
    Invalid = 0
    Red = 1
    Green = 2
    Blue = 3
End Enum

<Serializable()> _
Public Class Sample

   
Public Property Color() As PrimaryColor
        Get
            Return mColor
        End Get
        Set(ByVal value As PrimaryColor)
            mColor = value
        End Set
    End Property
    Private mColor As PrimaryColor

End
Class

This default behavior can be modified using the XmlEnumAttribute Class. For example, if the enumeration defined above was changed as shown below, the XML representation of the Color property would be <Color>G</Color>.

Public Enum PrimaryColor
    Invalid = 0
    <System.Xml.Serialization.XmlEnum("R")> _
    Red = 1
    <System.Xml.Serialization.XmlEnum("G")> _
    Green = 2
    <System.Xml.Serialization.XmlEnum("B")> _
    Blue = 3
End Enum

Even with the new XmlEnum values applied to the enumeration members, the ToString function will still return the name of the enumeration member, not the XmlEnum value. The following function will take an enumeration value and return its XML string value (either the XmlEnum value, if present, or the ToString value):

Public Shared Function ToXmlString( _
    ByVal value As [Enum]) As String

    Dim XmlEnumAttributes() As _
        System.Xml.Serialization.XmlEnumAttribute
    Dim EnumFieldInfo As System.Reflection.FieldInfo

   
' Verify Input Arguments
    If value Is Nothing Then
        Throw New ArgumentNullException("value")
    End If

    ' Get the Field Information for the 
    '
enumeration member
    EnumFieldInfo = _ 
        value.GetType().GetField(value.ToString())

    ' Get the XmlEnum attributes for the 
    ' enumeration member
    XmlEnumAttributes = CType( _
        EnumFieldInfo.GetCustomAttributes( _
        GetType(System.Xml.Serialization.XmlEnumAttribute), True), _
        System.Xml.Serialization.XmlEnumAttribute())

    ' Check to see if an XmlEnum attribute was found
    If XmlEnumAttributes.Length < 1 Then
        ' Return the default value
        Return value.ToString()
    Else
        ' Return the XmlEnum value
        Return XmlEnumAttributes(0).Name
    End If

End Function

Friday, December 28, 2007

.NET Method Parameters and Their SOAP

This article provides an overview of the different ways .NET Web Services SOAP will be generated based on how the parameters or return values are declared.

No Output
The following is the SOAP for a web method with no output:

VB.NET Code

<WebMethod()> _
Public Sub NoOutput()

End Sub

C# Code

[WebMethod]
public void NoOutput()
{
}

SOAP Input

  <soap:Body>
    <NoOutput xmlns="urn:sample" />
  </soap:Body>

SOAP Output

  <soap:Body>
    <NoOutputResponse xmlns="urn:sample" />
  </soap:Body>

Output Only
The following is the SOAP for a web method with no parameters that returns a string value:

VB.NET Code

<WebMethod()> _
Public Function StringOutput() As String
    Return "test"
End Function

C# Code

[WebMethod]
public string StringOutput()
{
    return "test";
}

SOAP Input

  <soap:Body>
    <StringOutput xmlns="urn:sample" />
  </soap:Body>

SOAP Output

  <soap:Body>
    <StringOutputResponse xmlns="urn:sample">
      <StringOutputResult>string</StringOutputResult>
    </StringOutputResponse>
  </soap:Body>

Input Only Parameter 
The following is the SOAP for a web method with a single integer input-only parameter and no return value:

VB.NET Code

<WebMethod()> _
Public Sub InputOnly(ByVal InputInt As Integer)
End Sub

C# Code

[WebMethod]
public void InputOnly(int InputInt)
{
}

SOAP Input

  <soap:Body>
    <InputOnly xmlns="urn:sample">
      <InputInt>int</InputInt>
    </InputOnly>
  </soap:Body>

SOAP Output

  <soap:Body>
    <InputOnlyResponse xmlns="urn:sample" />
  </soap:Body>

Output Only Parameter
The following is the SOAP for a web method with a single integer output-only parameter and no return value:

VB.NET Code

<WebMethod()> _
Public Sub OutputOnly( _
    <Runtime.InteropServices.Out()> ByRef _
    OutputInt
As Integer)
    OutputInt = 0
End Sub

C# Code

[WebMethod]
public void OutputOnly(out int OutputInt)
{
    OutputInt = 0;
}

SOAP Input

  <soap:Body>
    <OutputOnly xmlns="urn:sample" />
  </soap:Body>

SOAP Output

  <soap:Body>
    <OutputOnlyResponse xmlns="urn:sample">
      <OutputInt>int</OutputInt>
    </OutputOnlyResponse>
  </soap:Body>

Input-Output Parameter
The following is the SOAP for a web method with a single integer input-output parameter and no return value:

VB.NET Code

<WebMethod()> _
Public Sub InputOutput(ByRef InOutInt As Integer)
End Sub

C# Code

[WebMethod]
public void InputOutput(ref int InOutInt)
{
}

SOAP Input

  <soap:Body>
    <InputOutput xmlns="urn:sample">
      <InOutInt>int</InOutInt>
    </InputOutput>
  </soap:Body>

SOAP Output

  <soap:Body>
    <InputOutputResponse xmlns="urn:sample">
      <InOutInt>int</InOutInt>
    </InputOutputResponse>
  </soap:Body>