编程语言
首页 > 编程语言> > C#遍历枚举的所有字段,从字符串数组分配值

C#遍历枚举的所有字段,从字符串数组分配值

作者:互联网

我正在构建用于Web服务的Soap Body,并且有数十个可选字段.

目前,我一直在像这样处理这些:

wsSoapBody.OrderType = aMessage[(int)cardCreate.OrderType].ToString();
wsSoapBody.ActivateFlag = Convert.ToInt32(aMessage[(int)cardCreate.ActivateFlag].ToString()); //P-02925;

if (aMessage[(int)cardCreate.ShipDate].ToString() != ""){
                wsSoapBody.ShipmentDate = Convert.ToDateTime(aMessage[(int)cardCreate.ShipDate].ToString()); //P-02925;
        }

wsSoapBody.ShipmentMethodCard = aMessage[(int)cardCreate.ShipMethodCard].ToString();
wsSoapBody.ShipmentMethodPin = aMessage[(int)cardCreate.ShipMethodPIN].ToString();

在这些值分配中看到的CardCreate是cardCreate类中的枚举常量,定义如下:

namespace EvryCardManagement
{
    class CardCreate
    {
        #region Variables

        private DCSSCardCreateType req;
        private DCSSCardCreateResponseType rsp;
        private DCSSCardCreate_V3_0Service stub;

        public string tokenID { get; set; }

        private enum cardCreate
        {
            MsgType = 0,
            MsgVersion = 1,
            WSName = 2,
            ReplyTo = 3,
            SourceSystem = 4,
            Timestamp = 5,
            UniqueMessageID = 6,
            SFDCContext = 7,
            InstitutionID = 8,
            CardNumber = 9,
            Version = 10,
            ProductID = 11,
            AccountNumber = 12,
            CustomerID = 13,
            CustomerNumber = 14,
            EmbossName1 = 15,
            Expiry = 16,
            FeeMonth = 17,
            ChargeAccountNo = 18,
            PINMethod = 19,
            CardFlag = 20,
            AddressTypeCard = 21,
            AddressTypePIN = 22,
            OrderType = 23,
            ActivateFlag = 24,
            ShipDate = 25,
            ShipMethodCard = 26,
            ShipMethodPIN = 27,
            FirstName = 28,
            LastName = 29,
            CardAddress1 = 30,
            CardAddress2 = 31,
            CardAddress3 = 32,
            CardAddress4 = 33,
            CardAddress5 = 34,
            CardAddress6 = 35,
            CardPostCode = 36,
            CardCity = 37,
            CardCountry = 38,
            PINName = 39,
            PINAddress1 = 40,
            PINAddress2 = 41,
            PINAddress3 = 42,
            PINAddress4 = 43,
            PINAddress5 = 44,
            PINAddress6 = 45,
            PINPostCode = 46,
            PINCity = 47,
            PINCountry = 48,
            Validfrom = 49,
            Note = 50,
            MakeCheckStatus = 51,
            EmbossName2 = 52,
            PAmount = 53,
            PAmountLength = 54,
            GKIndicator = 55,
            CreditLimit = 56,
            CardDesignNo = 57,
            ExtPictureID = 58,
            BulkID = 59,
            AccountNo2 = 60
        }

因此,与其像我一直做的那样,不一遍又一遍地做,不如遍历wsSoapBody(在Web服务中定义),并为每一个从aMessage(定义的a)中获取相应的值.像这样的字符串数组[] aMessage)

编辑

我有下面的代码循环通过,但是我想分配给wsSoapBody,但我被卡住了:

foreach (cardCreate cItem in (cardCreate[])Enum.GetValues(typeof(cardCreate)))
 {
 }

(以上更正是史蒂夫·利利斯(Steve Lillis)建议的,由于冲突而被拒绝)

所以我不知道如何将值分配给每个元素,例如我要设置的

wsSoapBody[cItem].value = aMessage[(int)CardCreate[cItem]` 

或者我也尝试过:

wsSoapBody[cItem] = aMessage[(int)cItem].ToString();

但是由于缺乏知识而使其无法工作(甚至无法编译)

编辑#2:

我还查看了GetNames,因为我可能想要这些名称并尝试过:

        foreach (string name in Enum.GetNames(typeof(cardCreate)))

        {
            wsSoapBody[name] = aMessage[(int)name].ToString();
        }

但是我无法将[]的索引应用于类型为’DCSSCardCreateType’的表达式

谢谢

解决方法:

为什么不将值放在枚举本身上然后枚举呢?

例如,使用System.ComponentModel Description属性,我们可以将该信息添加到枚举本身,例如:

public enum cardCreate
{
  [Description("General Response")]
  MsgType = 0,

  [Description("V2.0")]
  WSName = 2,

  [Description("OmegaMan")]
  ReplyTo = 3,

  [Description("Windows 10")]
  SourceSystem = 4,
}

因此,当我们调用特殊方法枚举枚举时,我们可以提取该文本并在以后适当地使用它,例如:

myextensions.GetEnumValues<cardCreate>()
            .Select (ceEnum => new
                        {
                            Original   = ceEnum,
                            IndexValue = (int)ceEnum,
                            Text       = ceEnum.GetAttributeDescription()
                        })

投影(选择)之后,动态实体将如下所示:

甜!现在,我们将所有信息都放在一个易于消耗的实体中,该实体提供了所需的所有信息.

什么?您需要的不仅仅是字符串描述?然后在枚举上创建一个自定义属性,并根据需要返回所有数据项/类型.为此,请参阅我的博客文章C# Using Extended Attribute Information on Objects.

这是以上示例中使用的扩展方法:

public static class myextensions
{
   public static IEnumerable<T> GetEnumValues<T>()
   {
       Type type = typeof( T );

       if (!type.IsEnum)
           throw new Exception( string.Format("{0} is not an enum.", type.FullName ));

       FieldInfo[] fields =
           type.GetFields( BindingFlags.Public | BindingFlags.Static );


       foreach (var item in fields)
           yield return (T)item.GetValue( null );
   }


  /// <summary>If an attribute on an enumeration exists, this will return that
   /// information</summary>
   /// <param name="value">The object which has the attribute.</param>
   /// <returns>The description string of the attribute or string.empty</returns>
   public static string GetAttributeDescription( this object value )
   {
       string retVal = string.Empty;
       try
       {
           retVal = value.GetType()
                         .GetField( value.ToString() )
                         .GetCustomAttributes( typeof( DescriptionAttribute ), false )
                         .OfType<DescriptionAttribute>()
                         .First()
                         .Description;

       }
       catch (NullReferenceException)
       {
           //Occurs when we attempt to get description of an enum value that does not exist
       }
       finally
       {
           if (string.IsNullOrEmpty( retVal ))
               retVal = "Unknown";
       }

       return retVal;
   }

}

标签:enums,for-loop,ienumerable,c
来源: https://codeday.me/bug/20191120/2047271.html