Passing System.Enum as a parameter type... Help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Powers

    #1

    Passing System.Enum as a parameter type... Help!


    I currently have a large number of enums implemented into my Web
    Service. I am reworking the XML serialization of these enums.

    At present, each enum has hardcoded values, for example:

    xAttrs = New XmlAttributes
    xEnum = New XmlEnumAttribut e
    xEnum.Name = "0"
    xAttrs.XmlEnum = xEnum

    objXmlAttribute Overrides.Add(G etType(Enum_Emp loyment_Status) ,
    "Unspecifie d", xAttrs)

    xAttrs = New XmlAttributes
    xEnum = New XmlEnumAttribut e
    xEnum.Name = "1"
    xAttrs.XmlEnum = xEnum

    objXmlAttribute Overrides.Add(G etType(Enum_Emp loyment_Status) ,
    "FullTime", xAttrs)

    xAttrs = New XmlAttributes
    xEnum = New XmlEnumAttribut e
    xEnum.Name = "2"
    xAttrs.XmlEnum = xEnum

    objXmlAttribute Overrides.Add(G etType(Enum_Emp loyment_Status) ,
    "SelfEmploy ed", xAttrs)

    xAttrs = New XmlAttributes
    xEnum = New XmlEnumAttribut e
    xEnum.Name = "4"
    xAttrs.XmlEnum = xEnum

    objXmlAttribute Overrides.Add(G etType(Enum_Emp loyment_Status) ,
    "PieceWorke r", xAttrs)

    xAttrs = New XmlAttributes
    xEnum = New XmlEnumAttribut e
    xEnum.Name = "8"
    xAttrs.XmlEnum = xEnum

    objXmlAttribute Overrides.Add(G etType(Enum_Emp loyment_Status) ,
    "ContractWorker ", xAttrs)


    ....this streams for some 2000+ lines, hence why the rework.

    I've removed the need to hardcode values, and I've created a method to
    deal with the serialization, allowing me to simply make a method call
    per enum, and removing the need to hardcode the enum values. The
    method accepts the XMLAttributeOve rrides object, and the Enum type.

    The code is as below:

    Private Sub SerializeEnum(B yVal pEnum As System.Enum, ByVal
    pxOver As XmlAttributeOve rrides)
    'Powers - unable to do this, can't pass in the enum type...

    Try
    Dim strEnumContents As String()
    Dim strEnum As String
    Dim xAttrs As XmlAttributes
    Dim xEnum As XmlEnumAttribut e

    strEnumContents = [Enum].GetValues(GetT ype(pEnum))

    For Each strEnum In strEnumContents
    xAttrs = New XmlAttributes
    xEnum = New XmlEnumAttribut e
    xEnum.Name = [Enum].Format(GetType (pEnum), strEnum,
    "d")
    xAttrs.XmlEnum = xEnum
    pxOver.Add(GetT ype(pEnum),
    [Enum].GetName(GetTyp e(pEnum), strEnum), xAttrs)
    Next

    Catch ex As Exception
    'LogException ex

    End Try

    End Sub


    The problem now arises at each GetType(pEnum) - the compiler complains
    that "Type 'pEnum' is not defined".

    Is there a way I can do this, or am I going to have to hardcode the
    loop for each individual enum?? I've tried using System.Type instead
    of System.Enum, but no luck...

    Any help would be great...

    TIA,
    Powers

  • Andrew Backer

    #2
    Re: Passing System.Enum as a parameter type... Help!

    I think you can do something like this, if you need generic access to
    an enum's types. At least it is something that works in my case.
    You need to get at what you need via the static methods, which you
    can't call on an instance here (?). So, use

    =System.Enum.An yStaticMethod( System.Type of your enum )

    Public Enum DBA
    SqlSeverLimbs = 2
    MyCrashQl = 3
    PostgresCantPro nounce = 4
    End Enum

    Public Function EnumNames(ByVal enumType System.Type ) as String()
    Dim names as String() = System.Enum.Get Names(enumType)
    End Function

    Dim servers as String() = EnumNames( GetType(DBA) )

    Hope this helps.

    - Andrew

    Powers wrote:
    I currently have a large number of enums implemented into my Web
    Service. I am reworking the XML serialization of these enums.
    >
    At present, each enum has hardcoded values, for example:
    >
    xAttrs = New XmlAttributes
    xEnum = New XmlEnumAttribut e
    ....
    Private Sub SerializeEnum(B yVal pEnum As System.Enum, ByVal
    pxOver As XmlAttributeOve rrides)
    'Powers - unable to do this, can't pass in the enum type...
    ....
    strEnumContents = [Enum].GetValues(GetT ype(pEnum))
    >
    ....
    The problem now arises at each GetType(pEnum) - the compiler complains
    that "Type 'pEnum' is not defined".
    >
    Is there a way I can do this, or am I going to have to hardcode the
    loop for each individual enum?? I've tried using System.Type instead
    of System.Enum, but no luck...

    Comment

    Working...