computer-mag
سه شنبه 17 دی 1392, 10:04 صبح
سلام خسته نباشید کد زیر را در نظر بگیرید:
UserControlAdd n = new UserControlAdd();
من می خوام این کد رو جوری بنویسم که قسمت UserControlAdd() که بعد از New اومده رو از پارامتری در کانستراکتو فرم خودم بخونم یعنی هر بار این آبجکت n از یک نوع یوزرکنترل ها new شود. ممنون میشم کمکم کنید
من خودم کد زیر رو پیدا کردم ولی ارور میده اگه ارور این کد رو هم رفع کنید مشکل من حل میشه مرسی
var scan_class = "UserControlAdd";
var bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
var constructorArgs = new object[] { "salam" };
var constructorTypes = from p in constructorArgs select p.GetType();
var type = Type.GetType(scan_class);
var method = type.GetMethod(scan_class, bindingFlags, System.Type.DefaultBinder, constructorTypes.ToArray(), null);
var myObject = method.Invoke(null, bindingFlags, System.Type.DefaultBinder, constructorArgs, CultureInfo.CurrentCulture);
flowLayoutPanel1.Controls.Add(myObject as Control);
UserControlAdd n = new UserControlAdd();
من می خوام این کد رو جوری بنویسم که قسمت UserControlAdd() که بعد از New اومده رو از پارامتری در کانستراکتو فرم خودم بخونم یعنی هر بار این آبجکت n از یک نوع یوزرکنترل ها new شود. ممنون میشم کمکم کنید
من خودم کد زیر رو پیدا کردم ولی ارور میده اگه ارور این کد رو هم رفع کنید مشکل من حل میشه مرسی
var scan_class = "UserControlAdd";
var bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
var constructorArgs = new object[] { "salam" };
var constructorTypes = from p in constructorArgs select p.GetType();
var type = Type.GetType(scan_class);
var method = type.GetMethod(scan_class, bindingFlags, System.Type.DefaultBinder, constructorTypes.ToArray(), null);
var myObject = method.Invoke(null, bindingFlags, System.Type.DefaultBinder, constructorArgs, CultureInfo.CurrentCulture);
flowLayoutPanel1.Controls.Add(myObject as Control);