在Unity中注册类型时,如何传入构造函数参数?

时间:2022-09-04 21:06:30

I have the following type being registered in Unity:

我在Unity中注册了以下类型:

container.RegisterType<IAzureTable<Account>, AzureTable<Account>>();

The definition and constructors for AzureTable are as follows:

AzureTable的定义和构造方法如下:

public class AzureTable<T> : AzureTableBase<T>, IInitializer where T : TableServiceEntity
{

    public AzureTable() : this(CloudConfiguration.GetStorageAccount()) { }
    public AzureTable(CloudStorageAccount account) : this(account, null) { }
    public AzureTable(CloudStorageAccount account, string tableName)
            : base(account, tableName) { }

Can I specify the constructor arguments in the RegisterType line? I need to be able to pass in the tableName for example.

我可以在RegisterType行中指定构造函数参数吗?例如,我需要能够在tableName中传递。

This is a follow-up to my last question. That question was I think answered but I didn't really clearly ask how to get the constructor arguments in.

这是我最后一个问题的后续。我认为这个问题已经得到了回答,但我并没有明确地询问如何引入构造函数参数。

2 个解决方案

#1


24  

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:

这里是一个MSDN页面,描述您需要什么,注入值。看看在您的寄存器类型行中使用注入构造函数类。你会得到这样一条线:

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

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

注入构造函数的构造函数参数是传递给AzureTable 的值。任何类型的参数都保留unity来解析要使用的值。否则你只能通过你的实现:

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

Or a named parameter:

或一个命名的参数:

container.RegisterType<CloudStorageAccount>("MyAccount");
container.RegisterType<IAzureTable<Account>, AzureTable<Account>>(new InjectionConstructor(new ResolvedParameter<CloudStorageAccount>("MyAccount")));

#2


4  

You could give this a try:

你可以尝试一下:

// Register your type:
container.RegisterType<typeof(IAzureTable<Account>), typeof(AzureTable<Account>)>()

// Then you can configure the constructor injection (also works for properties):
container.Configure<InjectedMembers>()
  .ConfigureInjectionFor<typeof(AzureTable<Account>>(
    new InjectionConstructor(myConstructorParam1, "my constructor parameter 2") // etc.
  );

More info from MSDN here.

这里是MSDN的更多信息。

#1


24  

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:

这里是一个MSDN页面,描述您需要什么,注入值。看看在您的寄存器类型行中使用注入构造函数类。你会得到这样一条线:

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

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

注入构造函数的构造函数参数是传递给AzureTable 的值。任何类型的参数都保留unity来解析要使用的值。否则你只能通过你的实现:

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

Or a named parameter:

或一个命名的参数:

container.RegisterType<CloudStorageAccount>("MyAccount");
container.RegisterType<IAzureTable<Account>, AzureTable<Account>>(new InjectionConstructor(new ResolvedParameter<CloudStorageAccount>("MyAccount")));

#2


4  

You could give this a try:

你可以尝试一下:

// Register your type:
container.RegisterType<typeof(IAzureTable<Account>), typeof(AzureTable<Account>)>()

// Then you can configure the constructor injection (also works for properties):
container.Configure<InjectedMembers>()
  .ConfigureInjectionFor<typeof(AzureTable<Account>>(
    new InjectionConstructor(myConstructorParam1, "my constructor parameter 2") // etc.
  );

More info from MSDN here.

这里是MSDN的更多信息。