一般地填入不同的类成员 [英] Generically populate different classes members

查看:165
本文介绍了一般地填入不同的类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作与几个(11)的Web服务调用的Web服务应用程序。

I am working on a web-service application with several (11) web-service calls.

对于每一个网络服务,我需要从一个字符串数组这样的填充SOAP体:

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] 是字符串数组,[INT]是由一个枚举常量定义 - 在这种情况下,它的定义是这样的:

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?

部分类的是在这样的WSDL定义的:

The partial class is defined in the wsdl like this:

public partial class DCSSCustomerUpdateType 
{
    private string instIdField;
    private string branchField;
    ...
}

而不是每一个单独做这个(每11个自定义的服务的类),我不知道有没有办法通过在部分类的 wsSoapBody (沿以字符串数组)和遍历类的所有成员,从字符串数组赋值?

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?

修改

我搜查,发现<一href="http://stackoverflow.com/questions/531384/how-to-loop-through-all-the-properties-of-a-class">SO: 531384 /如何对环通全的 - 属性 - 即用A级?

所以,我想这样的:

    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));
        }
     }

字符串值= aMessage [property.Name]的ToString(); 将无法编译 - 因为它是在寻找一个int从列举的常数返回..

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?

推荐答案

尽量让

DCSSCustomerUpdate_V3 t = (DCSSCustomerUpdate_V3)Enum.Parse(typeof(DCSSCustomerUpdate_V3), property.Name);
 string value = aMessage[(int)t].ToString();

您也可以使用方法枚举。的TryParse

有关通用枚举类型的更多或更少,以便

For generic Enum type more or less so

 public static void DisplayAll<TEnum>(Object obj, string[] aMessage) where TEnum : struct,  IComparable, IFormattable, IConvertible
        {
            if (!typeof(TEnum).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                TEnum t = (TEnum)Enum.Parse(typeof(TEnum), property.Name);
                string value = aMessage[t.ToInt32(Thread.CurrentThread.CurrentCulture)].ToString();
                System.Diagnostics.Debug.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
            }
        }

看到这个帖子<一个href="http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum">Create泛型方法约束T可枚举

这篇关于一般地填入不同的类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆