P
Powers
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 XmlEnumAttribute
xEnum.Name = "0"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"Unspecified", xAttrs)
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "1"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"FullTime", xAttrs)
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "2"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"SelfEmployed", xAttrs)
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "4"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"PieceWorker", xAttrs)
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "8"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_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 XMLAttributeOverrides object, and the Enum type.
The code is as below:
Private Sub SerializeEnum(ByVal pEnum As System.Enum, ByVal
pxOver As XmlAttributeOverrides)
'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 XmlEnumAttribute
strEnumContents = [Enum].GetValues(GetType(pEnum))
For Each strEnum In strEnumContents
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = [Enum].Format(GetType(pEnum), strEnum,
"d")
xAttrs.XmlEnum = xEnum
pxOver.Add(GetType(pEnum),
[Enum].GetName(GetType(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
Service. I am reworking the XML serialization of these enums.
At present, each enum has hardcoded values, for example:
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "0"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"Unspecified", xAttrs)
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "1"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"FullTime", xAttrs)
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "2"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"SelfEmployed", xAttrs)
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "4"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"PieceWorker", xAttrs)
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = "8"
xAttrs.XmlEnum = xEnum
objXmlAttributeOverrides.Add(GetType(Enum_Employment_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 XMLAttributeOverrides object, and the Enum type.
The code is as below:
Private Sub SerializeEnum(ByVal pEnum As System.Enum, ByVal
pxOver As XmlAttributeOverrides)
'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 XmlEnumAttribute
strEnumContents = [Enum].GetValues(GetType(pEnum))
For Each strEnum In strEnumContents
xAttrs = New XmlAttributes
xEnum = New XmlEnumAttribute
xEnum.Name = [Enum].Format(GetType(pEnum), strEnum,
"d")
xAttrs.XmlEnum = xEnum
pxOver.Add(GetType(pEnum),
[Enum].GetName(GetType(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