0

キー「MyKey」があります。値「Key1」または「Key2」を持つことができます。

ここで、コンテナ「MyKey」と値「Key1」に渡す必要があります。

これは、コンテナに Interface オブジェクトを作成せずに実行できますか??

どんな方法でも構いません。

 public class KeyMode:IKeyMode
{

        private string keyMode;
        public KeyMode(string keyMode)
        {
            this.keyMode= keyMode;

        }

        public string getKeyMode()
        {
            return keyMode;
        }



}


public interface IKeyMode
    {
        string getKeyMode();
    }


 KeyMode rcm = new KeyMode("key1");
                    container.RegisterInstance<IKeyMode>(rcm);

IUnityContainer オブジェクト「コンテナー」とクラス KeyMode とインターフェイスを作成しました... Keymode オブジェクトを作成し、値をコンテナーに登録して渡します。KeyMode オブジェクトの作成の代わりに。キーと値のペアでコンテナー オブジェクトに直接渡す方法はありますか

4

2 に答える 2

0

ResolverOverrides を使用して、解決時にオブジェクトを渡すことができます。オーバーライドを使用してオブジェクトを解決する を参照してください。

Unity はクラスの作成方法を知っているので、必要がなければインターフェイスを登録する必要はありません。次のようなことができます。

IUnityContainer container = new UnityContainer();
KeyMode keyMode = container.Resolve<KeyMode>(
    new ParameterOverride("keyMode", "Key1"));
于 2013-11-11T18:51:11.270 に答える
0

あなたの下のようなものだと思います。

        IUnityContainer container = new UnityContainer();
        container.RegisterType<IKeyMode, KeyMode>(new InjectionConstructor("key1"));

        var keyMode = container.Resolve<IKeyMode>();
        Console.WriteLine(keyMode.GetKeyMode());
于 2013-11-07T12:22:47.550 に答える