Dependency injection 什么';这相当于IHostBuilder中温莎的字典自适应工厂?

Dependency injection 什么';这相当于IHostBuilder中温莎的字典自适应工厂?,dependency-injection,castle-windsor,.net-5,Dependency Injection,Castle Windsor,.net 5,我正在将控制台应用程序从.NET4.6移动到.NET5。 同时,我们的想法是摆脱Castle.Windsor,开始使用Microsoft.Extensions中的内置依赖注入 我会诚实的。他们两个我都不习惯。该应用程序有一个IApplicationConfiguration,它表示了我们从app.config文件中需要的内容 如何将其转换为IHostBuilder 提前感谢好的,在调查这一点之后,我找到了一个使用反射的解决方案 以下是一个例子: 使用系统; 运用系统反思; 使用System.Re

我正在将控制台应用程序从.NET4.6移动到.NET5。 同时,我们的想法是摆脱Castle.Windsor,开始使用Microsoft.Extensions中的内置依赖注入

我会诚实的。他们两个我都不习惯。该应用程序有一个IApplicationConfiguration,它表示了我们从app.config文件中需要的内容

如何将其转换为IHostBuilder


提前感谢

好的,在调查这一点之后,我找到了一个使用反射的解决方案

以下是一个例子:

使用系统;
运用系统反思;
使用System.Reflection.Emit;
名称空间TestConsoleNet5
{
公共课程
{
公共静态void Main()
{
AssemblyName aName=新的AssemblyName(“DynamicCasSemblyExample”);
装配生成器ab=
AssemblyBuilder.DefinedDynamicAssembly(
阿南,
AssemblyBuilderAccess.RunAndCollect);
模块生成器mb=
ab.DefinedDynamicModule(aName.Name+“模块”);
TypeBuilder tb=mb.DefineType(
“MyDynamicType”,
类型属性(公共);
BuildPropertyAndConstructor(tb,“这是一个测试”);
tb.AddInterfaceImplementation(类型化(ITest));
var type=tb.CreateType();
ITest test=Activator.CreateInstance(类型)作为ITest;
控制台写入线(test.propTest);
}
私有静态void BuildPropertyAndConstructor(TypeBuilder TypeBuilder,字符串defaultValue)
{
字符串propName=“propTest”;
FieldBuilder field=typeBuilder.DefineField(“m”+propName,typeof(string),FieldAttributes.Private);
PropertyBuilder PropertyBuilder=typeBuilder.DefineProperty(propName,PropertyAttributes.None,typeof(string),null);
MethodAttributes getSetAttr=MethodAttributes.Public|
MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Virtual;
MethodBuilder getter=typeBuilder.DefineMethod(“get_uzy”+propName,getSetAttr,typeof(string),Type.EmptyTypes);
ILGenerator getIL=getter.GetILGenerator();
getIL.Emit(opcode.Ldstr,defaultValue);
getIL.Emit(操作码.Ret);
SetGetMethod(getter);
}
}
公共接口测试
{
字符串测试{get;}
}
}
因此,在找到“这是一个测试”的地方,应该传递配置文件中的值

此外,接口应为IApplicationConfiguration

虽然有点凌乱,但它可以工作