C#字符串到var对象

C#字符串到var对象,c#,asp.net-mvc,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} 问题是微笑、愤怒和担忧被束缚了。但添加到情感之后,它们不是字符串,只是一个索引(就像dictionary一样,dicti

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

我想把它们转换成

new { hello= 1 } 
动态地,并且不使用任何条件,如

switch(p1){
case "hello":
return new {hello=p2};
}
因为有许多不同的字符串,我需要将许多项放入一个超级对象集中,如

var emotion = {smile=1,angry=2,worry=3}
问题是微笑、愤怒和担忧被束缚了。但添加到情感之后,它们不是字符串,只是一个索引(就像dictionary一样,dictionary的索引也有数据类型,这不是我期望的结果)

可能吗

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

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 };
}
private void问题_1()
{
//我有
字符串a=“你好”;
int b=1;
//我想以编程方式将a和b转换为新的{a=b},例如,我可以将a和b转换为类似于
Tuple x=新的Tuple(a,b);
//但是我不知道如何将它转换为new{a=b},因为我需要将字符串“hello”作为new{a=b}的键
var result=new{hello=b};//您可以看到我可以将b放在=,但我永远不能将字符串hello放在左边
}
私人问题2()
{
//决赛应该是这样的
列表=新列表(){
新元组(“微笑”,1),
新元组(“cry”,2),
新元组(“担心”,3)
};
foreach(列表中的元组项)
{
//将项的字符串和int添加到结果中,最终结果为
}
//最终结果
var finalResult=new{微笑=1,哭泣=2,担忧=3};
}

对枚举使用.NET命名约定:它们应该是Pascal表示法

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

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

对枚举使用.NET命名约定:它们应该是Pascal表示法

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

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

你为什么不能只用字典

var hi = new Dictionary<string,int>();
hi[p1] = p2;
return hi; // Would serialize the same way as your anonymous object
var hi=newdictionary();
hi[p1]=p2;
返回hi;//将以与匿名对象相同的方式序列化
如果没有,则可以使用expando对象在运行时动态设置属性

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

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

var hi = new Dictionary<string,int>();
hi[p1] = p2;
return hi; // Would serialize the same way as your anonymous object
var hi=newdictionary();
hi[p1]=p2;
返回hi;//将以与匿名对象相同的方式序列化
如果没有,则可以使用expando对象在运行时动态设置属性

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

您可以使用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;
        }
    }
类程序
{
静态动态对象=新的ExpandooObject();
静态void Main(字符串[]参数)
{
AddProperty(“城市”、“悉尼”);
AddProperty(“国家”、“澳大利亚”);
AddProperty(“你好”,1);
控制台写入线(对象城市);
控制台写入线(对象国家/地区);
Console.WriteLine(obj.hello);
//我们甚至可以使用动态属性名称(例如下面的示例中的cityProp)
IDictionary dic=作为IDictionary的obj;
Console.WriteLine(“城市为:+dic[cityProp]);
}
公共静态void AddProperty(字符串propertyName,对象值)
{
IDictionary a=作为IDictionary的obj;
[propertyName]=值;
}
}

您可以使用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;
        }
    }
类程序
{
静态动态对象=新的ExpandooObject();
静态void Main(字符串[]参数)
{
AddProperty(“城市”、“悉尼”);
AddProperty(“国家”、“澳大利亚”);
AddProperty(“你好”,1);
控制台写入线(对象城市);
控制台写入线(对象国家/地区);
Console.WriteLine(obj.hello);
//我们甚至可以使用动态属性名称(例如下面的示例中的cityProp)
IDictionary dic=作为IDictionary的obj;
Console.WriteLine(“城市为:+dic[cityProp]);
}
公共静态void AddProperty(字符串propertyName,对象值)
{
IDictionary a=作为IDictionary的obj;
[propertyName]=值;
}
}
像这样

编辑:根据您对另一个答案的评论:


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

这个问题显然不仅仅是C#,显然这是一个MVC问题。你真的应该尽可能多地添加关于你需要什么的信息

你的答案可能是:

@Url.Action()方法在服务器端是process,因此您不能 将客户端值作为参数传递给此函数

像这样

编辑:根据您对另一个答案的评论:


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

这个问题显然不仅仅是C#,显然这是一个MVC问题。你真的应该尽可能多地添加关于你需要什么的信息

你的答案可能是:

@Url.Action()方法在服务器端是process,因此您不能 将客户端值作为参数传递给此函数


您可以使用
enum
如果文本和值是动态的,您可以尝试使用字典。输入字符串是随机的,因此它将是一个非常大的枚举,我认为它不起作用…主要的一点是,我不想为了转换对象而硬编码任何内容(例如枚举)。Thanksounds很像这个问题:您可以使用
enum
如果文本和值是动态的,您可以尝试使用字典。输入字符串是随机的,因此它将是一个非常大的enum,我认为它不起作用……主要的一点是,我不想硬编码任何东西(例如enum)来转换objec