我正在开发一个具有多个 (11) Web 服务调用的 Web 服务应用程序。
对于每个 Web 服务,我需要从字符串数组填充 Soap Body,如下所示:
if (aMessage[(int)DCSSCustomerUpdate_V3.Branch].ToString().Length != 0)
{
wsSoapBody.Branch = aMessage[(int)DCSSCustomerUpdate_V3.Branch].ToString();
}
aMessage[int]
是字符串数组,[int] 由枚举常量定义 - 在本例中,它的定义如下:
private enum DCSSCustomerUpdate_V3
{
MsgType = 0,
MsgVersion = 1,
WSName = 2,
ReplyTo = 3,
SourceSystem = 4,
...
}
分部类中的属性名称与枚举常量匹配,所以我想我也会传入枚举常量?
The 部分类在 wsdl 中定义如下:
public partial class DCSSCustomerUpdateType
{
private string instIdField;
private string branchField;
...
}
而不是分别对每个项目执行此操作(在 11 个自定义项目中的每一个项目中)service类),我想知道有没有办法传递部分类wsSoapBody(以及字符串数组)并循环遍历类的所有成员,从字符串数组中分配值?
EDIT:
我搜索并发现SO:531384/如何循环遍历一个类的所有属性? https://stackoverflow.com/questions/531384/how-to-loop-through-all-the-properties-of-a-class
所以我尝试了这个:
public static void DisplayAll(Object obj, string[] aMessage)
{
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
string value = aMessage[property.Name].ToString();
System.Diagnostics.Debug.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
}
}
but string value = aMessage[property.Name].ToString();
不会编译 - 因为它正在寻找从枚举常量返回的 int ...
那么我该去哪里呢?