0

私のTestLayerクラスの名前空間は「BLL.Infrastructure.TestLayer」で、アセンブリ内にあります:「BLL」

public class LayerFactory<T, U>
{

  public static IBaseLayer<T, U> Get()
  {
       var obj = Activator.CreateInstance("BLL", "BLL.Infrastructure.TestLayer", new object[]   { (IBaseLayer<T, U>)null });


  }
}

コードを実行すると、アクティベーターは詳細なしでTypeLoadExceptionをスローします

これが、作成する必要のある具体的なクラスです。

GenericBaseLayerはIBaseLayerを実装します。

public class TestLayer<T, U> : GenericBaseLayer<MyRequest, MyInfo.ActionType>
{
  public TestLayer(IBaseLayer<MyRequest, MyInfo.ActionType> layer)
            : base(layer)
   { }
}

何が間違っていますか?

LayerFactoryはアセンブリ内にあります:BLL

したがって、アセンブリはすでにロードされている必要があります。

アップデート

Type d1 = typeof(TestLayer<,>);
Type[] typeArgs = { typeof(T), typeof(U) };
Type constructed = d1.MakeGenericType(typeArgs);
var obj = Activator.CreateInstance(constructed, new object[] { (IBaseLayer<T, U>)null });

これは動作します:)

4

1 に答える 1

0

ここに示されているようにタイプ名の文字列を指定します:その記事は

"System.Collections.Generic.List`1[System.String]"

の文字列識別子ですList<string>

T実行時にUのみ知っている場合でもType.GetType、ジェネリック型定義の文字列表現で使用できます。

Type d1 = Type.GetType(yourNamespace + ".TestLayer`2");

MakeGenericTypeその後、質問にd1示されているように呼び出すことができます。

于 2012-09-02T22:32:28.377 に答える