From 77af5e4f2b87552103b749299fed876a2b3a8f04 Mon Sep 17 00:00:00 2001 From: adam Date: Wed, 30 Oct 2024 17:21:00 -0400 Subject: [PATCH] you don't have to fuck around with cast-back nonsense; expandoobject *is* a dictionary. --- Deployment/ConfigurationBootstrapper.cs | 87 ++++++++++++++++--------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/Deployment/ConfigurationBootstrapper.cs b/Deployment/ConfigurationBootstrapper.cs index c41c710..979be10 100644 --- a/Deployment/ConfigurationBootstrapper.cs +++ b/Deployment/ConfigurationBootstrapper.cs @@ -48,59 +48,84 @@ namespace greyn.Deployment Console.WriteLine($"fromfile null: {fromFile == null}"); if (fromFile == null) { - var expandoPopulated = new ExpandoObject(); - var dictionaryFromExpandoPopulated = (IDictionary)expandoPopulated; - foreach (var memberInfo in config.GetType().GetMembers()) - { - switch (memberInfo.MemberType) - { - case MemberTypes.Field: - dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((FieldInfo)memberInfo).GetValue(config)); - break; - case MemberTypes.Property: - dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((PropertyInfo)memberInfo).GetValue(config)); - break; - default: - break; - } - } - expandoPopulated = (ExpandoObject)dictionaryFromExpandoPopulated; - fromFile = expandoPopulated; - return; + fromFile = new ExpandoObject(); + // var expandoPopulated = new ExpandoObject(); + // var dictionaryFromExpandoPopulated = (IDictionary)expandoPopulated; + // foreach (var memberInfo in config.GetType().GetMembers()) + // { + // switch (memberInfo.MemberType) + // { + // case MemberTypes.Field: + // dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((FieldInfo)memberInfo).GetValue(config)); + // break; + // case MemberTypes.Property: + // dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((PropertyInfo)memberInfo).GetValue(config)); + // break; + // default: + // break; + // } + // } + // expandoPopulated = (ExpandoObject)dictionaryFromExpandoPopulated; + // fromFile = expandoPopulated; + // return; } var dictionaryFromExpandoFromFile = (IDictionary)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}"); + 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)) { Console.WriteLine($"dictionary has it - {valueFromDict}"); if (valueFromDict != null) { - Console.WriteLine($"value from configuration value is not null"); + Console.WriteLine($"value from configuration file is not null"); switch (memberInfo.MemberType) { case MemberTypes.Field: 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(BindingFlags.DeclaredOnly) .Count()} members."); - var childMember = (ExpandoObject)dictionaryFromExpandoFromFile[asField.Name]; // (ExpandoObject)asField.GetValue(fromFile); - populateExpando(asField.GetValue(config), ref childMember); - asField.SetValue(fromFile, childMember); + 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) + { + populateExpando(asField.GetValue(config), ref 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; case MemberTypes.Property: 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(BindingFlags.DeclaredOnly) .Count()} members."); - var childMember = (ExpandoObject)dictionaryFromExpandoFromFile[asProperty.Name]; // (ExpandoObject)asProperty.GetValue(fromFile); - populateExpando(asProperty.GetValue(config), ref childMember); - asProperty.SetValue(fromFile, childMember); + 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) + { + populateExpando(asProperty.GetValue(config), ref childMember); + //asProperty.SetValue(fromFile, childMember); + } } break; default: