加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:56:10 所属栏目:百科 来源:网络整理
导读:我在Unity中注册了以下类型: container.RegisterTypeIAzureTableAccount,AzureTableAccount(); AzureTable的定义和构造函数如下: public class AzureTableT : AzureTableBaseT,IInitializer where T : TableServiceEntity{ public AzureTable() : this(Clo
我在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")));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读