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
Comment