将自定义列表值复制到var或动态并循环?

编程入门 行业动态 更新时间:2024-10-26 12:31:35
本文介绍了将自定义列表值复制到var或动态并循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的objectdatasource我使用_selected事件从列表中获得一些值,该对象返回。

所以我使用 e.Returnvalue 。

protected void ObjTrailerList_Selected(object sender,ObjectDataSourceStatusEventArgs e) { dynamic details = e.ReturnValue; var d = e.ReturnValue;}

现在我想复制整个自定义列表值到var或动态n迭代。 如何做?我不想创建对象MovieTrailers列表并复制其中的值。

我的自定义列表是

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

解决方案

p> private static void TestDynamic(动态列表) { foreach(列表中的var项) { if(item是string) { string foo = item; //使用它作为字符串 Console.WriteLine(字符串是:{0},foo); } else { Console.WriteLine(item); } } } static void Mian() { //传递字符串列表 TestDynamic new List< string> {Foo,Bar,Baz}); //传递匿名类的列表 TestDynamic(new List< dynamic> {new {Age = 25,BirthDay = new DateTime(1986,1,3)}},new {Age = 0,BirthDay = DateTime.Now}}); // TestDynamic(25); //这将导致运行时在foreach行的异常} // output:字符串是:Foo 字符串是:Bar​​ 字符串是:Baz {Age = 25,BirthDay = 3/1/1986 00:00:00} {Age = 0,BirthDay = 23/6/2011 01:23:18}

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

解决方案

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 }

更多推荐

将自定义列表值复制到var或动态并循环?

本文发布于:2023-07-15 10:15:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   动态   列表   var

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!