public Cert{
  Cert1,Cert2,Cert3
}public CertType{
  Type1,Type2,Type3
}public class CeshiInfo:baseinfo
{
  public List<Cert> name{get;set;} 
  public List<CertType> type{get;set;}
}
需要写一个方法,如SetInfo(baseinfo info,DataTable dTable)
根据dTable中的数值字符串自动赋值给name和type
比如dTable中name值是“Cert1”,type值是“Type1|Type3”
麻烦谁有好办法

解决方案 »

  1.   

     [Flags]
        public enum CertType
        {
            Type1, Type2, Type3
        }
        public enum Cert
        {
            Cert1, Cert2, Cert3
        }
        public class CeshiInfo
        {
            public List<Cert> name { get; set; }
            public List<CertType> type { get; set; }
            public CeshiInfo()
            {
                name = new List<Cert>();
                type = new List<CertType>();
            }
        }
        public static class CFoo
        {
            public static void SetInfo(this CeshiInfo info, DataTable dTable)
            {            for (int i = 0; i < dTable.Rows.Count; i++)
                {
                    string name = dTable.Rows[i]["name"].ToString();
                    string typename = dTable.Rows[i]["type"].ToString();
                    info.name.Add(AddValue(name));
                    info.type.AddRange(CertTypeAddValue(typename));            }
            }
            private static IList<CertType> CertTypeAddValue(string value)
            {
                List<CertType> temps = new List<CertType>();
                CertType temp;            string[] substrings = value.Split(new char[] { '|' });            foreach (var ontype in substrings)
                {
                    if (Enum.IsDefined(typeof(CertType), ontype))
                    {                    Enum.TryParse<CertType>(ontype, out temp);
                        temps.Add(temp);
                    }
                    else//没有找到异常投递
                    { }
                }            return temps.ToArray();
            }
            private static Cert AddValue(string value)
            {
                Cert temp = 0;
                if (Enum.IsDefined(typeof(Cert), value))
                {                Enum.TryParse<Cert>(value, out temp);
                }
                else//没有找到异常投递
                { }
                return temp;
            }    }
      

  2.   

    不是很完美,现在我用的是反射,自动赋值。枚举也实现了,在List<>枚举这块只能手动吗?就不能实现自动?
      

  3.   

    还是不能完成明白你的需要求:
    枚举又不能用 public static explicit operator ,怎么可以你所谓的“自动赋值”,除非引进第三方类,然后在第三方类中
     public static implicit operator Cert(Class1 stCerType)
     public static explicit operator Class1(string stCerType)
    再引用
    Cert=(Class1)String;
    如果真是这样,那不是把代码往麻烦了写吗