您的位置:首页 > 移动开发 > Objective-C

如何动态创建Control对象,并为其指定初始属性值

2008-07-23 11:35 351 查看
见代码:

 
void DynamicControl(string tag,string ctrType)
        {
            string assName = typeof(System.Windows.Forms.Form).AssemblyQualifiedName;
            string assInfo = assName.Substring(assName.IndexOf(","));
            Type type = System.Type.GetType("System.Windows.Forms." + ctrType + assInfo, true);  
            Control ctrl = (Control)Activator.CreateInstance(type);
            Type t = ctrl.GetType();
            t.InvokeMember("Tag", BindingFlags.SetProperty, null, ctrl, new object[1] { tag });
            t.InvokeMember("Name", BindingFlags.SetProperty, null, ctrl, new object[1] { name });
            t.InvokeMember("Text", BindingFlags.SetProperty, null, ctrl, new object[1] { "Test" });
            this.Controls.Add(ctrl);
        }
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string object null