SKLTFZ
SKLTFZ

Reputation: 950

C# string to var object

I have a string "hello", and a integer 1.

I want to convert them into

new { hello= 1 } 

dynamically, and without using any condition like

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

as there is many different string and I need to put many items into a super object set like

var emotion = {smile=1,angry=2,worry=3}

the problem is smile, angry and worry was string. but after added to emotion, they are not string, but just an index (like dictionary, however dictionary's index also has dataType, which is not my expected result)

Is it possible?

--- Updated --- i have added a function to specify the expected output.

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 };
}

Upvotes: 0

Views: 444

Answers (4)

Tyress
Tyress

Reputation: 3653

dynamic emotion = new { smile = 1, angry = 2, worry = 3 };
Console.WriteLine(emotion.smile);

Like this?

Edit: Based on your comment on another answer:

it is not worked, i need it able to be accepted by Url.Action("action","controller", x), where x is thing that i'm trying to create dynamically . i don't know if dynamic too complex or what. but Url.Action dont know how to read it

There's obviously more to the question than just C#, clearly this an MVC question. You should really add as much information as you can about what you need.

Your answer is probably here:

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

The @Url.Action() method is proccess on the server side, so you cannot pass a client side value to this function as a parameter.

Upvotes: 0

user2243747
user2243747

Reputation: 2977

You can use 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;
        }
    }

Upvotes: 0

Jason W
Jason W

Reputation: 13209

Any reason you can't just use a dictionary?

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

If not, then you could use the expando object to dynamically set properties at runtime.

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

Upvotes: 1

CodingYoshi
CodingYoshi

Reputation: 27039

Use .NET naming conventions for enums: They should be Pascal Notation.

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

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

You can also use a friendly name for your enum with the DesriptionAttribute like this:

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

Upvotes: 1

Related Questions