0

いくつかのジェネリック インターフェイスを登録して解決しようとしています。

登録機能があります

private static void RegisterFolderAssemblies(Type t,string folder)
    {
        var scanner = new FolderGenericInterfaceScanner();
        var scanned = scanner.Scan(t,folder); // gets the implementations from a specific folder
        scanned.ForEach(concrete =>
        {
            if (concrete.BaseType != null || concrete.IsGenericType)
            {
                myContainer.RegisterType(t, Type.GetType(concrete.AssemblyQualifiedName), concrete.AssemblyQualifiedName);
            }
        });
    }

ブートストラッパーによって呼び出されます

RegisterFolderAssemblies(typeof(IConfigurationVerification<>),Environment.CurrentDirectory);

登録は問題ないようですが、解決しようとすると

Type generic = typeof(IConfigurationVerification<>);
Type specific = generic.MakeGenericType(input.Arguments[0].GetType());

var verifications = BootStrap.ResolveAll(specific);

input.Arguments[0] は、ジェネリックが実装されている型のオブジェクトです。代わりに typeof(IConfigurationVerification<>) を使用してみましたが、同じエラーが発生しました。

ResolveAll の場合

public static List<object> ResolveAll(Type t)
{
        return myContainer.ResolveAll(t).ToList();
}

「現在のタイプ、Infrastructure.Interfaces.IConfigurationVerification`1[Infrastructure.Configuration.IMLogPlayerConfiguration+LoadDefinitions] はインターフェイスであり、構築できません。タイプ マッピングがありませんか?」というメッセージと共に ResolutionFailedException が表示されます。

どんな助けでも素晴らしいでしょう。

前もって感謝します

4

1 に答える 1

1

インターフェイスのインスタンスを持つことはできませんが、インターフェイスを実装する型からはできます。

interface IFoo{
}

class A : IFoo{
}

Activator.CreateInstance(typeof(IFoo)) //fails;
Activator.CreateInstance(typeof(A)) //succeeds;

Unity (または他の DI コンテナー) 内のどこかで Activator が使用されます。

インスタンス化できる型 (非抽象クラスまたは構造体) でスキャンする型をフィルター処理します。そうしないと、インスタンス化できないタイプも登録します。

あなたが持っているエラーが発生します。

于 2013-11-03T15:49:53.490 に答える