我在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页面,描述您需要的内容,Injecting Values.请查看使用注册类型行中的InjectionConstructor类.你会得到这样的一行:
container.RegisterType<IAzureTable<Account>,AzureTable<Account>>(new InjectionConstructor(typeof(CloudStorageAccount)));
InjectionConstructor的构造函数参数是要传递到AzureTable< Account> ;.的值.任何类型的参数离开统一以解析要使用的值.否则你可以通过你的实现:
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")));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。