Copy custom list value to var or dynamic and loop through? [closed]

五迷三道 提交于 2019-12-12 02:26:30

问题


In my objectdatasource i am using _selected event to get some value from the list which object returns.

So i am using e.Returnvalue.

protected void ObjTrailerList_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {

        dynamic details = e.ReturnValue;
       var d = e.ReturnValue;}

Now i want to copy entire custom list value to var or dynamic n iterate through. How to do? I don't want to create object List of MovieTrailers and copy the value in it.

my custom list is

public class MovieTrailers
{
   public int? TrailerId
   {
       get;
       set;
   }
   public string MovieName
   {
       get;
       set;
   }
   public string TrailerUrl
   {
       get;
       set;
   }
}

回答1:


private static void TestDynamic(dynamic list)
{
    foreach (var item in list)
    {
        if (item is string)
        {
            string foo = item;//use it as string
            Console.WriteLine("The string is: {0}", foo);
        }
        else
        {
            Console.WriteLine(item);
        }
    }
}

static void Mian()
{
    //pass a list of strings
    TestDynamic(new List<string> { "Foo", "Bar", "Baz" });

    //pass a list of anonymous class
    TestDynamic(new List<dynamic> { new { Age = 25, BirthDay = new DateTime(1986, 1, 3) }, new { Age = 0, BirthDay = DateTime.Now } });

    //TestDynamic(25);//this will cause exception at run time at the foreach line
}


//output:
The string is: Foo
The string is: Bar
The string is: Baz
{ Age = 25, BirthDay = 3/1/1986 00:00:00 }
{ Age = 0, BirthDay = 23/6/2011 01:23:18 }


来源:https://stackoverflow.com/questions/6445514/copy-custom-list-value-to-var-or-dynamic-and-loop-through

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!