c# – 当我在Unity中注册一个类型时,如何传递构造函数参数?

我在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&gt ;.的值.任何类型的参数离开统一以解析要使用的值.否则你可以通过你的实现:

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 举报,一经查实,本站将立刻删除。

相关推荐


c语言数组越界会怎么样
c语言指针怎么等于数组
c语言数组怎么存入文字
c语言中怎么显示数组
c语言数组元素怎么选
c语言数组怎么累加
c语言数组符号怎么输入
c语言怎么用长数组
c语言数组函数怎么输入
c语言数组怎么去掉差异
c语言怎么求解数组
c语言数组怎么用变量
c语言怎么申明数组
c语言怎么控制数组
c语言怎么计算数组长度
c语言数组怎么插星号
c语言数组怎么加长度
c语言中怎么输出数组
c语言怎么记住数组边界
c语言数组怎么输入符号