开发者

Parsing a dynamic enumeration

We are using a Microsoft ERP which dynamically exposes web services. The services generated by the service is out of our control. We have no say so in how the objects, including the type definitions, are created and exposed. When a new method is added or 开发者_StackOverflow中文版removed from the web service, all of the type enumerations are renumbered and everything that uses the web service, after updating to the new definitions, is hosed up. So essentially,

enumeration Type1
  Item1
  Item2
  Item3

... could become

enumeration Type6
  Item1
  Item2
  Item3

...with the enumeration type name changing, but members of the type remaining static. The service outputs a service which looks exactly like the end result of using the XSD.exe to generate objects. So anytime someone exposes a new method on the service (via the ERP GUI), the objects are rebuilt, types are assigned to the service definitions in alphabetical order, reexposed, leaving the entire code base shot.

I attempted to use reflection to determine the type and then parse out the static member into the new business object, but it doesn't work because I can't type cast the enumeration without knowing the actual name of the type. The following won't work.

System.Type t = service.BusinessObjectEnumeration.GetType();
service.SomeField = Enum.Parse(t,"Item1");

...as the compiler throws an error because I'm not explicitly casting the enumeration.

Any ideas how I can overcome this issue while dynamically casting the type to the correct enumeration?

Again, I cannot modify the actual objects exposed by the service, only the code subscribing to the service.


Re the example code:

System.Type t = service.BusinessObjectEnumeration.GetType();
service.SomeField = Enum.Parse(t,"Item1");

Maybe the way to do this is via reflection:

var prop = service.GetType().GetProperty("SomeField");
prop.SetValue(service, Enum.Parse(prop.PropertyType, "Item1"), null);


I had a similar issue with Nav web services. The solution I used to to create a new web reference for each service you expose through Nav rather than just a single one for all services. This prevents the name collisions you are experiencing without using reflection or hacks.


Why should you need to parse things?
If I understand Enums correctly, they pass on the value (and not the Enum itself).

EDIT: What I mean is, enum is not same as a class. For a class, one expects an instance to be passed/received. For an enum, it is one or combination of its members, which is passed in form of an int value.

EDIT2: Are you trying to use the enum as some kind of struct here?

EDIT3: You will have to see what type is the enum in debug mode, to figure out how to reflect on it.

object enumValueReturned = service.BusinessObjectEnumeration;

Put the enumValueReturned in watch window & play with it using reflection (GetMembers) to see, how would you reach Item1.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜