I have a problem with passing an anonymous object as an argument in a method. I want to pass the object like in JavaScript. Example:
function Test(obj) {
return obj.txt;
}
console.log(Test({ txt: "test"}));
But in C#, it throws many exceptions:
class Test
{
public static string TestMethod(IEnumerable<dynamic> obj)
{
return obj.txt;
}
}
Console.WriteLine(Test.TestMethod(new { txt = "test" }));
Exceptions:
It looks like you want:
class Test
{
public static string TestMethod(dynamic obj)
{
return obj.txt;
}
}
You're using it as if it's a single value, not a sequence. Do you really want a sequence?