I am working on a web-service application with several (11) web-service calls.
For each web-service I need to populate the Soap Body from a string array like this:
if (aMessage[(int)DCSSCustomerUpdate_V3.Branch].ToString().Length != 0)
{
wsSoapBody.Branch = aMessage[(int)DCSSCustomerUpdate_V3.Branch].ToString();
}
aMessage[int]
is the string array, and [int] is defined by an enumerated constant - in this case it is defined like this:
private enum DCSSCustomerUpdate_V3
{
MsgType = 0,
MsgVersion = 1,
WSName = 2,
ReplyTo = 3,
SourceSystem = 4,
...
}
The property names in the partial class are matched by the enumerated constant, so I guess I'd pass in the enumerated constant as well?
The partial class is defined in the wsdl like this:
public partial class DCSSCustomerUpdateType
{
private string instIdField;
private string branchField;
...
}
Rather than doing this for each one separately (in each of 11 custom service classes), I wonder is there a way to pass in the partial class wsSoapBody (along with the string array) and loop through all the members of the class, assigning values from the string array?
EDIT:
I searched and found SO: 531384/how-to-loop-through-all-the-properties-of-a-class?
So I tried this:
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();
won't compile - as it is looking for an int returned from an enumerated constant...
so where do I go from there?
so thanks to Fabio and Sloth, here is the final code we built:
and to call it, we use:
Try so
you can use also the method Enum.TryParse
For generic Enum type more or less so
see this post Create Generic method constraining T to an Enum
I don't know if I understood your question:
So you have this enum
DCSSCustomerUpdate_V3
which members match the property names of thewsSoapBody
class, and you don't want to repeat code like the one above, but use a loop, correct?You could simply loop over all elements of
DCSSCustomerUpdate_V3
and set the value of the properties like: