我有一个情况,不知道我的方法是否正确请指导我完成这个。
假设我有一个包含许多控件的Panel控件,
在运行期间,我使用了对该面板中的每个控件执行迭代
Panel1.Controls
财产,
现在,在这些控件中,它们可以是TextBox
,Button
,DropDown
等。
现在我想在运行时找到哪些控件属于哪种类型,然后查找该控件中是否包含任何特定属性&如果该属性存在,则设置该属性的值。
我想我必须在这里使用Reflection
做一些事情,但不知道从哪里开始。
示例代码:
foreach (Control cntrl in Panel1.Controls)
{
//find type of the control
// find any specific property's existence in that control
// if property exists than set value of that property
}
对于在运行时执行此任务,也欢迎任何其他更相关的方法。
抱歉,我忘了提
我不想在这里使用is
关键字,因为控件是可能的类型,我想创建一个全局函数,可以用于任何面板,而不知道该面板中存在的控件类型。
提前谢谢。
答案 0 :(得分:2)
通过使用反射,您可以实现此目的。
Get the metadata for the property
,然后设置它。这样您就可以检查是否存在属性,并验证是否可以设置:
foreach (Control cntrl in Panel1.Controls)
{
PropertyInfo prop = cntrl.GetType().GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
if(null != prop && prop.CanWrite)
{
prop.SetValue(cntrl, "MyName", null);
}
}
如果您不想使用反射
,请尝试此操作foreach (Control cntrl in Panel1.Controls)
{
if(cntrl is TextBox)
{
TextBox txt = (TextBox)ctrl;
txt.Text = "Your value here";
// your textbox code here
}
else if(cntrl is DropDown)
{
// your DropDown code here
}
if(cntrl is Button)
{
// your Button code here
}
}
注意: 是关键字检查对象是否与给定类型兼容。例如,以下代码可以确定对象是MyObject类型的实例,还是从MyObject派生的类型:
答案 1 :(得分:0)
您正在寻找is关键字。
foreach(Control ctrl in Panel1.Controls)
{
if(ctrl is TextBox) //Is the control of type TextBox?
{
(TextBox)ctrl.Text = "TextBox Test!"; //Cast it to a TextBox
} else if(ctrl is Button) //Is the control of type Button?
{
(Button)ctrl.Text = "Button Test!";
}
}
从A型到B型有两种铸造方式;
(TextBox)ctrl
或
ctrl as TextBox;
不同之处在于,如果转换失败,第一种方法会抛出异常,第二种方法返回null。在你的情况下,你很确定它是正确的类型,所以你使用它并不重要。
答案 2 :(得分:0)
您可以使用OfType
扩展程序:
List<TextBox> textBoxes = myPanel.Controls.OfType<TextBox>().ToList();
string allText = string.Join("\n", textBoxes.ConvertAll(t => t.Text));
上面的示例将找到面板中的所有TextBox控件,然后将其文本读取为单个字符串。
答案 3 :(得分:0)
您可以使用这样的Type.GetProperties方法,来自MSDN:
public class TypeMain
{
public static void Main()
{
Type myType =(typeof(MyTypeClass));
// Get the public properties.
PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
Console.WriteLine("The mumber of public properties is {0}.", myPropertyInfo.Length);
// Display the public properties.
DisplayPropertyInfo(myPropertyInfo);
// Get the nonpublic properties.
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length);
// Display all the nonpublic properties.
DisplayPropertyInfo(myPropertyInfo1);
}
public static void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo)
{
// Display information for all properties.
for(int i=0;i<myPropertyInfo.Length;i++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
Console.WriteLine("The property name is {0}.", myPropInfo.Name);
Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType);
}
}
PropetyInfo类还有一些有用的属性,如CanRead,CanWrite。查看PropertyInfo
的MSDN页面答案 4 :(得分:0)
我自己找到了一个解决方案,请查看它,并告诉我从性能的角度来看是否正确。
foreach (Control cntrl in Panel1.Controls)
{
System.Reflection.PropertyInfo[] props = cntrl.GetType().GetProperties();
IEnumerable<System.Reflection.PropertyInfo> searchedProp = props.Where(delegate(System.Reflection.PropertyInfo p)
{
return p.Name.Contains("YourPropertyName");
});
if (searchedProp != null && searchedProp.Count() > 0)
searchedProp.First().SetValue(cntrl, true, null); // Here true should be replaced by the value that is allowed by the property you are setting at runtime
}
答案 5 :(得分:0)
对于每个控件,获取控件类型den类型的名称。 通过反射得到控制的值,如果它是你想要的值 对所需属性执行set value操作。
foreach (Control control in panel1.Controls)
{
Type controlType = control.GetType();
switch (controlType.Name)
{
case "CheckBox":
if ((bool)controlType.GetProperty("Checked").GetValue(control,null))
controlType.GetProperty("Name").SetValue(control,"Check1",null);
break;
case "TextBox":
//TODO
break;
case "ComboBox":
//TODO
break;
default:
break;
}
}
}
答案 6 :(得分:-1)
您可以尝试如下,
using System.Reflection; // reflection namespace
// get all public static properties of MyClass type
PropertyInfo[] propertyInfos;
foreach (Control cntrl in Panel1.Controls)
{
if( cntrl is TextBox)
{
propertyInfos = typeof(TextBox).GetProperties(BindingFlags.Public |
BindingFlags.Static);
//Loop thru property info n find the name using PropertyInfo.Name
if(property.Name == "property name")
//assign values
}
else
{
//others
}
}