/// <summary>
/// 根据属性名设置属性的值
/// </summary>
/// <param name="ClassInstance">实例对象</param>
/// <param name="PropertyName">属性名</param>
/// <param name="Value">新值</param>
public void SetPropertyValue(object ClassInstance,string PropertyName,object Value)
{
Type myType = ClassInstance.GetType();
//获取指定的属性信息
PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName,BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);
//对属性进行赋值
myPropertyInfo.SetValue(ClassInstance,Value,null);
}
/// 根据属性名设置属性的值
/// </summary>
/// <param name="ClassInstance">实例对象</param>
/// <param name="PropertyName">属性名</param>
/// <param name="Value">新值</param>
public void SetPropertyValue(object ClassInstance,string PropertyName,object Value)
{
Type myType = ClassInstance.GetType();
//获取指定的属性信息
PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName,BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);
//对属性进行赋值
myPropertyInfo.SetValue(ClassInstance,Value,null);
}
而mainmenu的items都有按照index或者key来访问的索引。
你在生成menuitem的时候,要么为每一个item手动定制一个key(stirng型),要么将每个item的index映射到一个数组中,要获取某一个item引用时访问索引mainmenu1.items["somekey"],或者mainmenu1.items[12]来获得。或者另外一个办法,建一个mainmenuitem数组(建议2维,当然用hash表也可以),再手动将这个数组里面的menuitem加到menu上面。比如你的菜单是file(open,save,close),edit(cut,copy,paste);那么建一个数组,0维是file和edit,file的1维是open,save,close,edit的1维是cut,copy,paste.以此类推,虽然麻烦一点但是控制起来比较灵活。
用hash表的确比我以前的一堆IF判断先进多了,谢谢如果是能用反射的话,我想代码会更简介,也更灵活,我的真正用途是,每个用户登陆后在数据库中读取他拥有权限的菜单项名称(的是字符窜的)
现在就要根据这些字符串来显示对应的菜单项,我数据库中保存的字符串和对应的菜单项的名字是一模一样的,
关键是如何根据菜单项的名字(字符串)得到对应的引用?
我再研究研究,同时也希望大家帮帮忙!
* 按控件实例名称及其属性名称实现动态赋值
*
* 入口参数:ClassInstance 控件所在的类实例
* ControlName 控件实例名称,区分大小写
* PropertyName 要设值的控件属性名称,区分大小写
* Value 新值,类型是一个Object
*/
// public void SetControlValue(object ClassInstance,string ControlName,string PropertyName,object Value)
// {
// // object Result = null;
// Type myType = ClassInstance.GetType();
// FieldInfo myFieldInfo = myType.GetField(ControlName,BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance);
//
// if(myFieldInfo != null)
// {
// PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myType);
// PropertyDescriptor myProperty = properties.Find(PropertyName, false);
//
// if(myProperty != null)
// {
// object ctr;
// ctr = myFieldInfo.GetValue(ClassInstance);
//
// try
// {
// myProperty.SetValue(ctr,Value);
// }
// catch(Exception e)
// {
// // MessageBox.Show("SetControlValue Method error!");
// }
// }
// }
// }
/*
* 按控件实例名称及其属性名称实现动态赋值
*
* 入口参数:ClassInstance 控件所在的类实例
* ControlName 控件实例名称,区分大小写
* PropertyName 要设值的控件属性名称,区分大小写
* Value 新值,类型是一个Object
*/
// public void SetControlValue(object ClassInstance,string ControlName,string PropertyName,object Value)
// {
// // object Result = null;
// Type myType = ClassInstance.GetType();
// FieldInfo myFieldInfo = myType.GetField(ControlName,BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance);
//
// if(myFieldInfo != null)
// {
// PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myType);
// PropertyDescriptor myProperty = properties.Find(PropertyName, false);
//
// if(myProperty != null)
// {
// object ctr;
// ctr = myFieldInfo.GetValue(ClassInstance);
//
// try
// {
// myProperty.SetValue(ctr,Value);
// }
// catch(Exception e)
// {
// // MessageBox.Show("SetControlValue Method error!");
// }
// }
// }
// }
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'MenuItem3这里要注意大小写,前头的"_"在VB中要,在C#中不要。
'Me是当前实例,就是当前Form,如果是别的Form,可以这样
'Dim Frm As New Form2,就是取得实例后将Me用Frm来代替,这样也可以取Form2的了
Dim t As Type = Me.GetType
Dim f As FieldInfo = t.GetField("_" & "MenuItem3", _
BindingFlags.NonPublic Or _
BindingFlags.Instance Or _
BindingFlags.Public)
Dim itemMenu As MenuItem = CType(f.GetValue(Me), MenuItem)
itemMenu.Enabled = false
itemMenu.PerformClick()
End Sub
这是vb的语法,你可以转一下就可以了!
http://blog.csdn.net/lzmtw/archive/2004/08/17/76498.aspx