How can I pass in constructor arguments when I register a type in Unity?

后端 未结 2 1262
别跟我提以往
别跟我提以往 2021-02-19 17:46

I have the following type being registered in Unity:

container.RegisterType, AzureTable>();

The

2条回答
  •  悲哀的现实
    2021-02-19 18:15

    Here is an MSDN page describing what you require, Injecting Values. Take a look at using the InjectionConstructor class in your register type line. You will end up with a line like this:

    container.RegisterType, AzureTable>(new InjectionConstructor(typeof(CloudStorageAccount)));
    

    The constructor parameters to InjectionConstructor are the values to be passed to your AzureTable. Any typeof parameters leave unity to resolve the value to use. Otherwise you can just pass your implementation:

    CloudStorageAccount account = new CloudStorageAccount();
    container.RegisterType, AzureTable>(new InjectionConstructor(account));
    

    Or a named parameter:

    container.RegisterType("MyAccount");
    container.RegisterType, AzureTable>(new InjectionConstructor(new ResolvedParameter("MyAccount")));
    

提交回复
热议问题