you don't have to fuck around with cast-back nonsense; expandoobject *is* a dictionary.

This commit is contained in:
adam 2024-10-30 17:21:00 -04:00
parent 9f69150202
commit 77af5e4f2b

View File

@ -48,59 +48,84 @@ namespace greyn.Deployment
Console.WriteLine($"fromfile null: {fromFile == null}"); Console.WriteLine($"fromfile null: {fromFile == null}");
if (fromFile == null) if (fromFile == null)
{ {
var expandoPopulated = new ExpandoObject(); fromFile = new ExpandoObject();
var dictionaryFromExpandoPopulated = (IDictionary<string, object?>)expandoPopulated; // var expandoPopulated = new ExpandoObject();
foreach (var memberInfo in config.GetType().GetMembers()) // var dictionaryFromExpandoPopulated = (IDictionary<string, object?>)expandoPopulated;
{ // foreach (var memberInfo in config.GetType().GetMembers())
switch (memberInfo.MemberType) // {
{ // switch (memberInfo.MemberType)
case MemberTypes.Field: // {
dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((FieldInfo)memberInfo).GetValue(config)); // case MemberTypes.Field:
break; // dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((FieldInfo)memberInfo).GetValue(config));
case MemberTypes.Property: // break;
dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((PropertyInfo)memberInfo).GetValue(config)); // case MemberTypes.Property:
break; // dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((PropertyInfo)memberInfo).GetValue(config));
default: // break;
break; // default:
} // break;
} // }
expandoPopulated = (ExpandoObject)dictionaryFromExpandoPopulated; // }
fromFile = expandoPopulated; // expandoPopulated = (ExpandoObject)dictionaryFromExpandoPopulated;
return; // fromFile = expandoPopulated;
// return;
} }
var dictionaryFromExpandoFromFile = (IDictionary<string, object?>)fromFile; var dictionaryFromExpandoFromFile = (IDictionary<string, object?>)fromFile;
foreach (var memberInfo in config.GetType().GetMembers(BindingFlags.DeclaredOnly)) Console.WriteLine($"{config.GetType().GetMembers().Count()} members on {config.GetType()}");
foreach (var memberInfo in config.GetType().GetMembers())
{ {
Console.WriteLine($"checking property: {memberInfo.Name}"); Console.WriteLine($"checking property: {memberInfo.Name}");
if(memberInfo.DeclaringType == typeof(System.Object))
{
Console.WriteLine($"junk from system.object, don't care");
continue;
}
if(memberInfo.MemberType != MemberTypes.Field && memberInfo.MemberType != MemberTypes.Property)
{
Console.WriteLine($"type I don't know how to handle ({memberInfo.MemberType})");
continue;
}
if (dictionaryFromExpandoFromFile.TryGetValue(memberInfo.Name, out object? valueFromDict)) if (dictionaryFromExpandoFromFile.TryGetValue(memberInfo.Name, out object? valueFromDict))
{ {
Console.WriteLine($"dictionary has it - {valueFromDict}"); Console.WriteLine($"dictionary has it - {valueFromDict}");
if (valueFromDict != null) if (valueFromDict != null)
{ {
Console.WriteLine($"value from configuration value is not null"); Console.WriteLine($"value from configuration file is not null");
switch (memberInfo.MemberType) switch (memberInfo.MemberType)
{ {
case MemberTypes.Field: case MemberTypes.Field:
var asField = (FieldInfo)memberInfo; var asField = (FieldInfo)memberInfo;
if ((asField.FieldType.GetMembers(BindingFlags.DeclaredOnly)?.Count() ?? 0) > 0) //if ((asField.FieldType.GetMembers(BindingFlags.DeclaredOnly)?.Count() ?? 0) > 0)
{
Console.WriteLine($"{asField.Name} (as field) has {asField.FieldType.GetMembers() .Count()} members.");
Console.WriteLine($"{dictionaryFromExpandoFromFile[asField.Name]}");
var childMember = dictionaryFromExpandoFromFile[asField.Name] as ExpandoObject;
if(childMember != null)
{ {
Console.WriteLine($"{asField.Name} (as field) has {asField.FieldType.GetMembers(BindingFlags.DeclaredOnly) .Count()} members.");
var childMember = (ExpandoObject)dictionaryFromExpandoFromFile[asField.Name]; // (ExpandoObject)asField.GetValue(fromFile);
populateExpando(asField.GetValue(config), ref childMember); populateExpando(asField.GetValue(config), ref childMember);
asField.SetValue(fromFile, childMember); //asField.SetValue(fromFile, childMember); //you don't have to set it back. the expandoobject *implements* IDictionary
// so when you cast it, it's not a copy; you just access it as a dictionary. So you can change it dynamically.
// and we ref'd it so it got the underlying one.
//https://stackoverflow.com/questions/4938397/dynamically-adding-properties-to-an-expandoobject
}
} }
break; break;
case MemberTypes.Property: case MemberTypes.Property:
var asProperty = (PropertyInfo)memberInfo; var asProperty = (PropertyInfo)memberInfo;
if ((asProperty.PropertyType.GetMembers(BindingFlags.DeclaredOnly)?.Count() ?? 0) > 0) //if ((asProperty.PropertyType.GetMembers(BindingFlags.DeclaredOnly)?.Count() ?? 0) > 0)
{
Console.WriteLine($"{asProperty.Name} (as property) has {asProperty.PropertyType.GetMembers() .Count()} members.");
Console.WriteLine($"{dictionaryFromExpandoFromFile[asProperty.Name]}");
var childMember = dictionaryFromExpandoFromFile[asProperty.Name] as ExpandoObject; // (ExpandoObject)asProperty.GetValue(fromFile);
if(childMember != null)
{ {
Console.WriteLine($"{asProperty.Name} (as property) has {asProperty.PropertyType.GetMembers(BindingFlags.DeclaredOnly) .Count()} members.");
var childMember = (ExpandoObject)dictionaryFromExpandoFromFile[asProperty.Name]; // (ExpandoObject)asProperty.GetValue(fromFile);
populateExpando(asProperty.GetValue(config), ref childMember); populateExpando(asProperty.GetValue(config), ref childMember);
asProperty.SetValue(fromFile, childMember); //asProperty.SetValue(fromFile, childMember);
}
} }
break; break;
default: default: