2011-04-26 2 views
1

하이에서 컨트롤 만들기 컨트롤을 만들C 번호는 문자열 값

+0

질문에 대한 답변이 명확하지 않습니다. –

+0

나에게 완벽한 의미가 있습니다 – RustyH

답변

2

당신은 반사를 사용할 수

var controlType = typeof(Control); 
     var type = controlType 
      .Assembly 
      .GetTypes() 
      .Where(t => controlType.IsAssignableFrom(t) && 
         t.Namespace == "System.Windows.Forms" 
         t.Name == "ControlName" 
      ).FirstOrDefault(); 

var inst = Activator.CreateInstance(type); 

이 답변 때문에 이전 질문이다.

+0

고마워요 고마워요. 고마워요. 사실 이죠. 한 가지 질문이 있습니다. 텍스트 상자를 정의하지만 텍스트 상자 나 텍스트 크기를 "텍스트"같은 문자열에서 어떻게 변경할 수 있습니까? – deneme

+2

@ deneme, 한 번만 할 수 있습니다. 다시 새로 생성 된 인스턴스에 대한 리플렉션을 사용합니다 :'TextBoxType.GetProperty ("Text"). SetValue (textBox, "Hello World", null);'. 나는 반사에 대해 더 많이 읽는 것이 좋습니다. http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx –

+0

Excellent, thanks Darin! – BillW

0
var assembly = Assembly.GetExecutingAssembly(); 
var type = assembly.GetType("System.Windows.Forms.TextBox"); 
var inst = Activator.CreateInstance(type); 

그것을 테스트 할 수 없습니다 아직. 하지만 그 트릭을해야합니다.

var textBoxType = typeof(Control).Assembly.GetType("System.Windows.Forms.TextBox", true); 
var textBox = Activator.CreateInstance(textBoxType); 
1

은 다음과 같이 수행합니다 :

0
Type t = Type.GetType(yourTypeStingHere); 

ConstructorInfo info = t.GetConstructor(new Type[] { }); 
object instance = info.Invoke(new object[]{})