2012-01-03 19 views
9

私は動的にusercontrolsを使用してWebページを作成するWebアプリケーションを持っています。私のコード内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経由)、2番目のオプションはおそらく簡単です。

  • +0

    アセンブリ修飾名?それはどこで手に入りますか?これらは、Webアプリケーション自体の中で作成したカスタムユーザコントロールです。 – mattgcon

    +2

    @mattgcon: 'Type.AssemblyQualifiedName'を使用できますが、特定のアセンブリ用であることがわかっている場合は、' typeof(SomeClassInTheAssembly).Assembly'そのアセンブリを取得するには、 'Assembly.GetType(string)'を使用します。どのクラスをアセンブリから使用して参照するかは関係ありません。 –

    +0

    私はアセンブリを取得した後、私は何をしますか?ユーザーコントロールを取得するために、タイプchild = Assembly.GetType(typeName)と記述していますか? – mattgcon

    4

    Type.GetTypeは、というアセンブリと、一部のシステムアセンブリを呼び出すのように見えます。それ以外の場合は、assemblyInstance.GetType(typeName)を使用するか、型の「アセンブリ修飾名」を使用する必要があります。アセンブリ名には、型が見つかるアセンブリの詳細が含まれます。さもなければ、それは見つけられず、ヌルを返します。

    string aqn = someType.AssemblyQualifiedName; 
    
    +0

    ユーザーコントロールのアセンブリ修飾名を取得するにはどうすればいいですか? – mattgcon

    +0

    @mattgcon答えは –

    +0

    です。someTypeがmy typeName私は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; 
        } 
    } 
    

    :それはありませんが、ここに私がやってしまったものです仕事...

    関連する問題