在Unity中注册类型时,如何传递构造函数参数?我在Unity中注册了以下类型:container.RegisterType();AzureTable的定义和构造函数如下:publicclassAzureTable:AzureTableBase,IInitializerwhereT:TableServiceEntity{publicAzureTable():this(CloudConfiguration.GetStorageAccount()){}publicAzureTable(CloudStorageAccountaccount):this(account,null){}publicAzureTable(CloudStorageAccountaccount,stringtableName):base(account,tableName){}我可以在RegisterType行中指定构造函数参数吗?我需要能够传递tableName作为示例。这是对我之前的问题的跟进。我想回答这个问题,但我真的不明白如何获取构造函数参数。这是一个描述您需要的MSDN页面,InjectingValues。查看在寄存器类型行中使用InjectionConstructor类。您最终会得到这样一行:container.RegisterType>(newInjectionConstructor(typeof(CloudStorageAccount)));InjectionConstructor的构造函数参数将传递给AzureTable。任何类型的参数都保持统一以解析要使用的值。否则你可以传递你的实现:CloudStorageAccountaccount=newCloudStorageAccount();container.RegisterType>(newInjectionConstructor(account));或命名参数:container.RegisterType("MyAccount");container.RegisterType>(newInjectionConstructor(newResolvedParameter("MyAccount")));你可以试试这个://注册你的类型:container.RegisterType)>()//然后你可以配置构造函数注入(也适用于属性):container.Configure().ConfigureInjectionFor(newInjectionConstructor(myConstructorParam1,"myconstructor参数2")//等等);来自MSDN的更多信息。以上是C#学习教程:Unity注册类型时如何传递构造函数参数?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: