2012-01-03 5 views
9

나는 usercontrols를 사용하여 웹 페이지를 동적으로 만드는 웹 응용 프로그램을 보유하고 있습니다. 내 코드 내에서Type.GetType() null을 반환합니다.

나는 다음과 같습니다 반환되는

private void Render_Modules() 
    { 
     foreach (OnlineSystemPageCustom.OnlineSystemPageHdr.OnlineSystemPageModule item in custompage.Header.Modules) 
     { 
      if (item.ModuleCustomOrder != 99 && !item.ModuleOptional) 
      { 
       string typeName = item.ModuleInternetFile; 
       Type child = Type.GetType(typeName); 
       webonlinecustombase ctl = (webonlinecustombase)Page.LoadControl("../IPAM_Controls/webtemplatecontrols/" + child.Name.ToString() + ".ascx"); 
       ctl.Event = Event; 
       ctl.custompage = custompage; 
       ctl.custommodule = item; 
       this.eventprogrammodules.Controls.Add(ctl); 
      } 
     } 
    } 

은 "유형 이름"(예시)는 다음과 같이 사용자 컨트롤에 대한

IPAMIntranet.IPAM_Controls.webtemplatecontrols.eventorgcommittee

네임 스페이스는 :

namespace IPAMIntranet.IPAM_Controls 

내가 겪고있는 문제는 T ype.GetType (typeName)이 null을 리턴합니다. 내가 여기서 무엇을 놓치고 있니?

+0

예 죄송합니다. 입력 오류입니다. null을 의미합니다. – mattgcon

답변

24

Type.GetType(string)은 문자열 내에 어셈블리 이름을 지정하지 않은 경우 현재 실행중인 어셈블리와 mscorlib 만 찾습니다.

옵션 :

  • 해당 어셈블리 어셈블리 자격을 갖춘 이름 대신
  • 전화 Assembly.GetType(name)를 사용하는 대신

당신이 관련 어셈블리의 보류를 얻는 쉬운 방법이있는 경우 (예 : typeof(SomeKnownType).Assembly 통해) 두 번째 옵션은 아마 더 간단합니다.

+0

어셈블리가 정규화 된 이름입니까? 어디서 구할 수 있니? 이게 도움이된다면 웹 응용 프로그램 자체 내에서 만든 사용자 정의 사용자 정의 컨트롤입니다. – mattgcon

+2

@mattgcon : 'Type.AssemblyQualifiedName'을 사용할 수 있지만 특정 어셈블리에 해당하는 경우'typeof (SomeClassInTheAssembly) .Assembly'를 사용할 수 있습니다. 그 어셈블리를 얻으려면,'Assembly.GetType (string)'을 사용하십시오. 어셈블리에서 참조하는 데 사용하는 클래스는 중요하지 않습니다. –

+0

어셈블리를 얻은 후에는 무엇을해야합니까? Type child = Assembly.GetType (typeName)은 사용자 정의 컨트롤을 가져올 수 있습니까? – mattgcon

4

Type.GetType은 호출 어셈블리 및 몇 가지 시스템 어셈블리로 표시됩니다. 그 외의 경우에는 assemblyInstance.GetType(typeName)을 사용해야하며, 형식을 찾을 수있는 어셈블리 세부 정보를 포함하는 형식의 "어셈블리 정규화 된 이름"을 사용해야합니다. 그렇지 않은 경우는 발견되지 않고, null가 돌려 주어집니다.

string aqn = someType.AssemblyQualifiedName; 
+0

사용자 정의 컨트롤의 어셈블리 정규화 된 이름을 얻는 방법 – mattgcon

+0

@mattgcon someType이 my typeName 인 경우 응답자 –

+0

에 포함 시켰습니다. , 나는 그 typeName 동적 될 거라고 말하고 그게 뭔지 전혀 모르겠다 디자인 타임. – mattgcon

0

나는 내가 ASPX보다는 정적 유틸리티 클래스의 코드 숨김 내 사용자 지정 사용자 컨트롤의 클래스를 인스턴스화하는 데 필요한 것을 제외하고, 원래 포스터에 매우 비슷한 문제가 있었다 : 당신은에서 것을 얻을 수 있습니다 페이지, 그래서 LoadControl 나를 사용할 수 없습니다. 그것은하지 꽤 아니라 매우 효율적이고, (당신이 다음 단지 그들 모두를 볼 수 있지만)이 App_Web_ *이 명명 규칙이 변경되는 경우 휴식 의무가

public static class Utils 
{ 
    public static string MyFunc(string controlClassName) 
    { 
     string result = ""; 
     // get a list of all assemblies in this application domain 
     Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); 
     // the trouble is that we don't know which assembly the class is defined in, 
     // because we are using the "Web Site" model in Visual Studio that compiles 
     // them on the fly into assemblies with random names 
     // -> however, we do know that the assembly will be named App_Web_* 
     // (http://msdn.microsoft.com/en-us/magazine/cc163496.aspx) 
     foreach (Assembly assembly in assemblies) 
     { 
      if (assembly.FullName.StartsWith("App_Web_")) 
      { 
       // I have specified the ClassName attribute of the <%@ Control %> 
       // directive in the relevant ASCX files, so this should work 
       Type t = assembly.GetType("ASP." + controlClassName); 
       if (t != null) 
       { 
        // use reflection to create the instance (as a general object) 
        object o = Activator.CreateInstance(t); 
        // cast to the common base type that has the property we need 
        CommonBaseType ctrl = o as CommonBaseType; 
        if (ctrl != null) 
        { 
         foreach (string key in ctrl.PropertyWeNeed) 
         { 
          // finally, do the actual work 
          result = "something good"; 
         } 
        } 
       } 
      } 
     } 
     return result; 
    } 
} 

: 그것은 않지만 여기에 내가하고 결국 무엇 작업 ...

관련 문제