Fix in IOptions Copy Section
This commit is contained in:
@@ -58,19 +58,27 @@ 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 exp = Expression.Lambda<Action<TOptions>>(assign, paraexpression);
|
var assign = Expression.Assign(memberexpression, Expression.Convert(val, prop.PropertyType));
|
||||||
builder.Services.Configure(instanceName, exp.Compile());
|
var exp = Expression.Lambda<Action<TOptions>>(assign, paraexpression);
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user