2011-07-12 13 views
0

私はあなたのアイデアを開いています:私は本当にこの使用法が嫌いです。属性を使ってenumを文字列に変換する方法しかし、私の質問StringValueAttributeには複数のコンストラクタがあることに注意してください。どのようにStringValueAttributeを介してGetStringValue拡張メソッドを開発するか?敬具は...属性を持つ列挙型の使用方法Reflections?



using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 

namespace UsingAttributes 
{ 
     static void Main(string[] args) 
     { 
      Console.WriteLine(UserType.Login.GetStringValue()); 
      Console.Read(); 
     } 
    } 
    public enum UserType : int 
    { 
     [StringValueAttribute("xyz", "test")] 
     Login = 1 
    } 
    public class StringValueAttribute : Attribute 
    { 
     public string UserName { get; protected set; } 
     public string PassWord { get; protected set; } 
     public decimal Something { get; protected set; } 

      public StringValueAttribute(string Username, string Password,decimal something) 
     { 
      UserName = Username; 
      PassWord = Password; 
      Something =something; 
     } 
     public StringValueAttribute(string Username, string Password) 
     { 
      UserName = Username; 
      PassWord = Password; 
     } 
     public StringValueAttribute(string Username) 
     { 
      UserName = Username; 

     } 

    } 

    public static class Extentions 
    { 
     public static String[] GetStringValue(this Enum value) 
     { 
      // Get the type 
      Type type = value.GetType(); 

      // Get fieldinfo for this type 
      FieldInfo fieldInfo = type.GetField(value.ToString()); 

      // Get the stringvalue attributes 
      StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
       typeof(StringValueAttribute), false) as StringValueAttribute[]; 

      // Return the first if there was a match. 


       PropertyInfo[] pi = attribs[0].GetType().GetProperties(); 
       String[] Vals = new String[pi.Length]; 
       int i = 0; 
       foreach (PropertyInfo item in pi) 
       { 
        // Vals[i] = How to return String[] 

       } 
      // i dislike return values one by one : attribs[0].UserName 
      //return attribs.Length > 0 ? attribs[0].UserName : null; // i have more values 
     } 
    } 
} 

答えて

0

はまずdecimalが属性の引数として使用することはできないことに注意してください。 doubleを使用するか、文字列を使用して10進数に変換できます。あなたがC#4.0を使用している場合は、次の3つのコンストラクタを必要としない

Object val = item.GetValue(attribs[0], null); 
    Vals[i++] = val != null ? val.ToString() : ""; 

// Vals[i] = How to return String[] 

を交換する場合

あなたGetStringValue()が動作するはずです。あなたは、オプションの引数を使用することができます:あなたが使用することができます

public StringValueAttribute(string Username, string Password = "nothing", double something = 0)   
{    
    ... 
}   

とC#の古いバージョンではOptionalAttribute

public StringValueAttribute(string Username, [Optional] string Password, [Optional] double something)   
{    
    ... 
}   
1

私は、これはそれを行うには奇妙な方法であると言わなければならない、あなただけだろうそれからちょうどあなたの情報を与える属性クラスに機能を追加し

static void Main(string[] args) 
     { 
      object[] Objects = UserType.Login.GetStringValue(); 
      for (int x = 0; x < Objects.Length; ++x) 
       Console.WriteLine(Objects[x].ToString()); 
      Console.Read(); 
     } 
    } 

    public static class Extentions 
    { 
     public static object[] GetStringValue(this Enum value) 
     { 
      // Get the type 
      Type type = value.GetType(); 

      // Get fieldinfo for this type 
      FieldInfo fieldInfo = type.GetField(value.ToString()); 

      // Get the stringvalue attributes 
      StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
       typeof(StringValueAttribute), false) as StringValueAttribute[]; 

      // Return the first if there was a match. 

      object[] Vals = new object[3]; 
      Vals[0] = attribs[0].UserName; 
      Vals[1] = attribs[0].PassWord; 
      Vals[2] = attribs[0].Something; 
      return Vals; 
     } 
    } 
+0

しかし、私は考えのユーザー名、パスワード、または何か – programmerist

+0

を持っていません。このような何かにあなたのコードを変更する必要があります。それが不可能な場合は、値を取得するためにリフレクションコールを追加するだけです。 – JaCraig

関連する問題