今天犯了一个极其低级的错误: foreach (Control c in grpqry.Controls) { ComboBox cmb=(ComboBox)c; if (c.GetType().Name.IndexOf("ComboBox")!=-1) { Trace.WriteLine(cmb.SelectedItem.ToString()); }; } An error occurred please contact the adminstrator with the following information: 指定的转换无效。 Stack Trace: at PMS.frm_Svn.btnSql_Click(Object sender, EventArgs e) in :line 387 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 我真是白痴一个!!! foreach (Control c in grpqry.Controls) { if (c.GetType().Name.IndexOf("ComboBox")!=-1) { ComboBox cmb=(ComboBox)c; Trace.WriteLine(cmb.SelectedItem.ToString()); }; } 不过也顺便学习了一下反射: Type t=c.GetType(); MemberInfo[] myMembers = t.GetMember("SelectedItem", MemberTypes.All, BindingFlags.Public &line; BindingFlags.Instance); 不过不知道怎么去获得这个实际的属性值 Type t = typeof(MyType); // Create an instance of a type. Object[] args = new Object[] { 8 }; Console.WriteLine("The value of x before the constructor is called is { 0 }.", args[0]); Object obj = t.InvokeMember(null, BindingFlags.DeclaredOnly &line; BindingFlags.Public &line; BindingFlags.NonPublic &line; BindingFlags.Instance &line; BindingFlags.CreateInstance, null, null, args); Console.WriteLine("Type: " + obj.GetType().ToString()); Console.WriteLine("The value of x after the constructor returns is { 0 }.", args[0]); // Read and write to a field. t.InvokeMember("myField", BindingFlags.DeclaredOnly &line; BindingFlags.Public &line; BindingFlags.NonPublic &line; BindingFlags.Instance &line; BindingFlags.SetField, null, obj, new Object[] { 5 }); Int32 v = (Int32) t.InvokeMember("myField", BindingFlags.DeclaredOnly &line; BindingFlags.Public &line; BindingFlags.NonPublic &line; BindingFlags.Instance &line; BindingFlags.GetField, null, obj, null); Console.WriteLine("myField: " + v); 好像对COMBOBOX~~~~~~~ 字体:大 中 小 |