解析动态枚举

发布于 2024-09-06 21:52:40 字数 784 浏览 2 评论 0原文

我们正在使用动态公开 Web 服务的 Microsoft ERP。服务所产生的服务不在我们的控制范围内。对于如何创建和公开对象(包括类型定义),我们没有发言权。当在 Web 服务中添加或删除新方法时,所有类型枚举都会重新编号,并且在更新到新定义后,所有使用 Web 服务的内容都会被整理出来。因此本质上,

enumeration Type1
  Item1
  Item2
  Item3

... 可能会变成

enumeration Type6
  Item1
  Item2
  Item3

...,枚举类型名称发生变化,但类型的成员保持静态。该服务输出的服务看起来与使用 XSD.exe 生成对象的最终结果完全相同。因此,每当有人在服务上公开新方法(通过 ERP GUI)时,都会重新构建对象,将类型按字母顺序分配给服务定义,然后重新公开,留下整个代码库镜头。

我尝试使用反射来确定类型,然后将静态成员解析为新的业务对象,但它不起作用,因为我无法在不知道类型的实际名称的情况下对枚举进行类型转换。下面的不行。

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

...因为编译器抛出错误,因为我没有显式转换枚举。

有什么想法可以克服这个问题,同时将类型动态转换为正确的枚举吗?

同样,我无法修改服务公开的实际对象,只能修改订阅服务的代码。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如若梦似彩虹 2024-09-13 21:52:40

重新查看示例代码:

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

也许可以通过反射来实现此目的:

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

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);
把回忆走一遍 2024-09-13 21:52:40

我在 Nav Web 服务方面也遇到了类似的问题。我用来为您通过 Nav 公开的每一项服务创建一个新的 Web 引用的解决方案,而不是为所有服务创建一个新的 Web 引用。这可以防止您在不使用反射或黑客的情况下遇到名称冲突。

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.

长梦不多时 2024-09-13 21:52:40

为什么需要解析事物?
如果我正确理解枚举,它们会传递值(而不是枚举本身)。

编辑:我的意思是,enumclass 不同。对于,人们期望传递/接收一个实例。对于enum,它是其成员之一或组合,以int 值的形式传递。

EDIT2:您是否尝试在此处使用 enum 作为某种 struct

EDIT3:您必须在调试模式下查看枚举的类型,以弄清楚如何反映它。

object enumValueReturned = service.BusinessObjectEnumeration;

enumValueReturned 放入监视窗口中使用反射 (GetMembers) 来尝试一下,看看如何到达 Item1

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文