c#

image

아래와 같이 생성

            string controlName = "WindowsFormsApplication1.UserControl1"; 

            Assembly assembly = Assembly.GetExecutingAssembly();
            Type t = assembly.GetType(controlName);

            UserControl obj = (UserControl)Activator.CreateInstance(t);
 

프로젝트가 다른경우! 아래와 같이 처리한다.

image

void Form2_Load(object sender, EventArgs e)
{

    string controlName = "ClassLibrary1.UserControl1";
    string dllPath = string.Format("{0}\\{1}", 
Application.StartupPath, "ClassLibrary1.dll"); System.Runtime.Remoting.ObjectHandle oh
= Activator.CreateInstanceFrom(dllPath, "ClassLibrary1.UserControl1"); ClassLibrary1.UserControl1 st = (ClassLibrary1.UserControl1)oh.Unwrap(); st.TestMethod(); }
 
 

WPF

  string fullUri = string.Format("{0};component\\{1}.xaml", assamName, userControlName);
  var p = (UserControl)System.Windows.Application.LoadComponent(
                   new Uri(fullUri, System.UriKind.RelativeOrAbsolute));

+ Recent posts