2010-02-16 2 views
2

노드가 포함 된 TreeView가 있습니다. 사용자가 노드를 클릭하면 해당 CrystalReport 문서가 만들어져 표시되어야합니다.형식이 문자열 매개 변수에있는 개체를 만듭니다.

예를 들어, 내 노드의 이름은 "PeriodReport1", "PeriodReport2", "PeriodReport3"입니다. 내 CrystalReport 문서의 이름은 "PeriodReport1", "PeriodReport2", "PeriodReport3"과 같습니다.

크리스탈 리포트 문서의 올바른 유형을 만들고 표시하려면 어떻게해야합니까? 나는 다음과 같이 할 수있다 :

select case reportName 
    case "PeriodReport1" 
     dim myReport as new PeriodReport1 
    case "PeriodReport2" 
     dim myReport as new PeriodReport2 
    ... 
end select 

그러나 아마 이것을 할 수있는 더 좋은 방법이있다. 이 작업을 수행하기 위해 리플렉션을 사용할 수 있습니까?

+0

중복 식으로 뭔가 : http://stackoverflow.com/questions/2247598/c-instantiate-class-from-string –

답변

6

만큼 보고서가 기본 paramaterless 생성자를 가지고 ... 새로운 PeriodReport을 말할 수있는 즉,이 같은

Activator.CreateInstance(Type.GetType("namespace.typename")) 
+0

@anonymous downvoter. 응? –

2

뭔가 : 당신은 또한 어떤의 개체를 숨길 수

Type t = Type.GetType("object type name"); 
TBaseObj new_obj = (TBaseObj) Activator.CreateInstance(t); 
2

TreeNode의 tag 속성에 입력하십시오. 이는 종종 이런 종류의 일에 유용합니다. MVC가 아니지만 모델과 뷰를 별도로 유지하려고하지 않는 경우에 적합 할 수 있습니다 (WinForms가 권장하지 않는).

0

캐스팅은 어떻게됩니까? 'Object'인스턴스를 어떻게 캐스팅합니까?

string myCustomClassName = getMyCustomClassName(); 
var myCustomInstance = (???????)myGenericObject; //What do I replace ?????? with? 
관련 문제