我在 Unity 中注册了以下类型:
container.RegisterType<IAzureTable<Account>, AzureTable<Account>>();
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) { }
我可以在 RegisterType 行中指定构造函数参数吗?例如,我需要能够传递 tableName。
这是我上一个问题的后续。我认为这个问题已经得到解答,但我并没有真正清楚地询问如何获取构造函数参数。
这是一个 MSDN 页面,描述了您的需求,注入价值 http://msdn.microsoft.com/en-us/library/ff660882%28v=PandP.20%29.aspx。看看使用InjectionConstructor
寄存器类型行中的类。你最终会得到这样一行:
container.RegisterType<IAzureTable<Account>, AzureTable<Account>>(new InjectionConstructor(typeof(CloudStorageAccount)));
构造函数参数为InjectionConstructor
是要传递给您的值AzureTable<Account>
. Any typeof
参数保持统一以解析要使用的值。否则你可以直接通过你的实现:
CloudStorageAccount account = new CloudStorageAccount();
container.RegisterType<IAzureTable<Account>, AzureTable<Account>>(new InjectionConstructor(account));
或者命名参数:
container.RegisterType<CloudStorageAccount>("MyAccount");
container.RegisterType<IAzureTable<Account>, AzureTable<Account>>(new InjectionConstructor(new ResolvedParameter<CloudStorageAccount>("MyAccount")));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)