Fix in IOptions Copy Section

This commit is contained in:
Felix Glang
2024-04-27 21:53:12 +02:00
parent e6173ae683
commit f73b3b5578

View File

@@ -58,18 +58,26 @@ public static class ServicesExtensions
(string)typeof(TOptions).GetProperty("Host")?.GetValue(option, null)!); (string)typeof(TOptions).GetProperty("Host")?.GetValue(option, null)!);
// Dark Magic , we don't know the Property's of TOptions , and we won't cast them for each Options // Dark Magic , we don't know the Property's of TOptions , and we won't cast them for each Options
var paraexpression = Expression.Parameter(option.GetType(), "x"); // Todo eventuell schönere Lösung finden
var paraexpression = Expression.Parameter(Type.GetType(option.GetType().FullName), "x");
foreach (var prop in option.GetType().GetProperties()) foreach (var prop in option.GetType().GetProperties())
{ {
var val = Expression.Constant(prop.GetValue(option)); var val = Expression.Constant(prop.GetValue(option));
var memberexpression = Expression.PropertyOrField(paraexpression, prop.Name); var memberexpression = Expression.PropertyOrField(paraexpression, prop.Name);
var assign = Expression.Assign(memberexpression, val); if (prop.PropertyType == typeof(int) || prop.PropertyType == typeof(string) || prop.PropertyType == typeof(bool))
{
var assign = Expression.Assign(memberexpression, Expression.Convert(val, prop.PropertyType));
var exp = Expression.Lambda<Action<TOptions>>(assign, paraexpression); var exp = Expression.Lambda<Action<TOptions>>(assign, paraexpression);
builder.Services.Configure(instanceName, exp.Compile()); builder.Services.Configure(instanceName, exp.Compile());
} }
else
{
Console.WriteLine(prop.PropertyType + "No Support");
}
}
builder.Services.AllowResolvingKeyedServicesAsDictionary(); builder.Services.AllowResolvingKeyedServicesAsDictionary();
builder.Services.AddKeyedSingleton<TInterface, TService>(instanceName); builder.Services.AddKeyedSingleton<TInterface, TService>(instanceName);