C#string到var对象

时间:2017-01-10 03:29:17

标签: c# asp.net-mvc

我有一个字符串“hello”和一个整数1。

我想将它们转换为

new { hello= 1 } 

动态,不使用任何条件,如

switch(p1){
case "hello":
return new {hello=p2};
}

因为有许多不同的字符串,我需要将许多项目放入超级对象集,如

var emotion = {smile=1,angry=2,worry=3}
问题是微笑,生气和担心是串。但是在添加到情感之后,它们不是字符串,而只是一个索引(如字典,但字典的索引也有dataType,这不是我预期的结果)

有可能吗?

---更新---我添加了一个指定预期输出的函数。

private void Question_1()
{
    //i have
    string a = "hello";
    int b = 1;
    // i want to convert a and b to new {a = b} programmatically, for example i can convert a and b to a Tuple like
    Tuple<string, int> x = new Tuple<string, int>(a,b);
    //but i dont know how to to convert it to new {a = b}, as i need to put the string "hello" as key to new {a=b}
    var result = new { hello = b }; //you can see i can put b after =, but i can never put the string hello at the left
}
private void Question_2()
{
    //and the final should be like this
    List<Tuple<string, int>> list = new List<Tuple<string, int>>() {
        new Tuple<string,int>("smile",1),
        new Tuple<string,int>("cry",2),
        new Tuple<string,int>("worry",3)
    };
    foreach (Tuple<string, int> item in list)
    {
        //adding item's string and int into result and finally the result is
    }
    //the final result
    var finalResult = new { smile = 1, cry = 2, worry = 3 };
}

4 个答案:

答案 0 :(得分:1)

对枚举使用.NET命名约定:它们应该是Pascal Notation。

enum Emotion
{
    Smile = 1, Angry = 2, Worry = 3
}

var l = new List<Emotion> { Emotion.Angry, Emotion.Smile, Emotion.Worry };

您还可以使用DesriptionAttribute这样的枚举的友好名称:

enum Emotion
{
    [Description("Smiling")]
    Smile = 1,
    [Description("Angry Type")]
    Angry = 2,
    [Description("Worry Type")]
    Worry = 3
}

答案 1 :(得分:1)

你有什么理由不能使用字典吗?

var hi = new Dictionary<string,int>();
hi[p1] = p2;
return hi; // Would serialize the same way as your anonymous object

如果没有,那么您可以使用expando对象在运行时动态设置属性。

var hi = new ExpandoObject() as IDictionary<string, object>;
hi.Add(p1, p2);
var p2Value = (int)((dynamic)hi).hello;

答案 2 :(得分:0)

您可以使用ExpandoObject。

class Program
    {
        static dynamic obj = new ExpandoObject();
        static void Main(string[] args)
        {
            AddProperty("City", "Sydney");
            AddProperty("Country", "Australia");
            AddProperty("hello", 1);

            Console.WriteLine(obj.City);
            Console.WriteLine(obj.Country);
            Console.WriteLine(obj.hello);

            //We can even use dynamic property names ( e.g. cityProp in below example ) 
            IDictionary<string, object> dic = obj as IDictionary<string, object>;
            Console.WriteLine("City is : " + dic[cityProp]);
        }

        public static void AddProperty(string propertyName, object value)
        {
            IDictionary<string, object> a = obj as IDictionary<string, object>;
            a[propertyName] = value;
        }
    }

答案 3 :(得分:0)

.blog-list

喜欢这个吗?

修改:根据您对其他答案的评论:

  

它不起作用,我需要它才能被接受   Url.Action(“action”,“controller”,x),其中x是我正在尝试的东西   动态创建。我不知道动态是否太复杂或者是什么。   但是Url.Action不知道怎么读它

问题显然不仅仅是C#,显然这是一个MVC问题。您应该尽可能多地添加有关所需内容的信息。

你的回答可能就在这里:

https://stackoverflow.com/a/15112223/1685167

  

@ Url.Action()方法是服务器端的进程,所以你不能   将客户端值作为参数传递给此函数。