6

I have populated the follow list with objects of type AnonymousType

List<object> someList = new List<object>();

someList.Add(new { foo = 1 });

My problem is that I can't make it stronly typed to do something like this:

someList.Where(x=> x.foo == 1);

However, it's possible on this list:

var someList = new[] { new { foo = 1 } };

Can I cast my first list to make it behave like the second list? I want to be able to use lambda expressions on the properties like I showed above.


Check smarty Syntax Error in Html

From php, Is there any way by which i can detect if there is any smarty syntax error in html ?

4

2 に答える 2

4

Jon Skeets を使用できますCastByExample:

public static T CastByExample<T>(object input, T example)
{
    return (T)input;
}

List<object> someList = new List<object>() { 
    new { foo = 1 },new { foo = 2 },new { foo = 3 }
};

var example = new { foo = 0 };

foreach (object obj in someList)
{
    var x = CastByExample(obj, example);
    Console.WriteLine("Foo: " + x.foo);
}
于 2013-07-05T12:48:58.687 に答える