-1

My goal is a little mix between the lists and enum.

Example when I create a list:

List<string> toto= new List<string>;
toto.add("test1");
toto.add("test2");

==>but it's not possible to call "test1" like this: toto.test1

With the enum it's possible like this:

public enum toto{
test,test2
}

but it's not possible of create dynamic an enum in terms of string

an idea for my problem?

Thank for your help

4

3 に答える 3

6

あなたが何をしようとしているのかわからない。オブジェクトを動的に拡張する場合は、次のようにします。

ExpandoObject.NET 4.0 で試してみませんか?

dynamic toto = new ExpandoObject();
toto.test1 = 10;
toto.test2 = 300;
toto.test3 = "Hello";
于 2013-03-21T15:09:49.277 に答える
0

リストの文字列を列挙型メンバーに変換する拡張関数を作成できます。

    public static MyEnum ToMyEnum(this String myEnumMemberAsString, Boolean ignoreCase = true)
    {
        if (String.IsNullOrEmpty(myEnumMemberAsString))
            throw new ArgumentException("ToMyEnum: String null or empty!");
        return (MyEnum)Enum.Parse(typeof(MyEnum), myEnumMemberAsString, ignoreCase);
    }

あなたの質問であなたのリストを仮定すると:

public static String GetFromList(this List<String> list, MyEnum elemToGet)
{
    return list.SingleOrDefault(e => e == elemToGet.ToString());
}

次に、たとえばString found = list.GetFromList(MyEnum.test2);

于 2013-03-21T15:10:17.543 に答える