using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Dynamic; namespace WindowsFormsTest { public partial class dynamic类 : Form { public dynamic类() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { dynamic a = GetData(this.textBox1.Text); dynamic o = new ExpandoObject(); o.Id = a.Id; o.Title = a.Title; o.Time = DateTime.Now; MessageBox.Show(o); } private static dynamic GetData(string a) { if (a == "0") { News n = new News(); n.Id = 1; n.Title = "测试新闻"; return n; } else { Product p = new Product(); p.Id = 123; p.Title = "测试产品"; return p; } } } }