public List <IOptionsProvider> AddOptionObject(IOptionsProvider provider, string prefix, object obj, uint?priority = default(uint?), uint jumpComplexTypes = uint.MaxValue) { if (prefix == null) { throw new ArgumentNullException("prefix"); } if (obj == null) { throw new ArgumentNullException("obj"); } List <IOptionsProvider> res = new List <IOptionsProvider>(); var type = obj.GetType(); var converter = TypeConvertersCache.GetConverter(type); if (converter != null) { var pres = AddOption(provider, prefix, converter(obj), priority); if (pres != null) { res.Add(pres); } return(res); } if (typeof(IEnumerable).IsAssignableFrom(type)) { if (type.GetTypeInfo().IsGenericType&& (new Type[] { typeof(IEnumerable <>), typeof(ICollection <>) }).Contains(type.GetGenericTypeDefinition())) { var innerType = type.GetGenericArguments()[0]; converter = TypeConvertersCache.GetConverter(type); if (converter == null) { return(res); } StringBuilder sb = new StringBuilder(); foreach (var x in obj as IEnumerable) { if (sb.Length > 0) { sb.Append("|"); } sb.Append(converter(x)); } var pres = AddOption(provider, prefix, sb.ToString(), priority); if (pres != null) { res.Add(pres); } return(res); } else { return(res); } } if (jumpComplexTypes > 0) { foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { res.AddRange(AddOptionObject(provider, prefix + "." + propertyName(prop), prop.GetValue(obj), priority, jumpComplexTypes - 1)); } } return(res); }
public object GetOptionObject(string prefix, Type type, object instance = null) { if (prefix == null) { throw new ArgumentNullException("prefix"); } if (type == null) { throw new ArgumentNullException("type"); } var converter = TypeConvertersCache.GetInverseConverter(type); if (converter != null) { OptionEntry res; if (store.TryGetValue(prefix, out res)) { return(converter(res.Value)); } else { return(null); } } if (typeof(IEnumerable).IsAssignableFrom(type)) { if (type.GetTypeInfo().IsGenericType&& (new Type[] { typeof(IEnumerable <>), typeof(ICollection <>) }).Contains(type.GetGenericTypeDefinition())) { var innerType = type.GetGenericArguments()[0]; converter = TypeConvertersCache.GetInverseConverter(innerType); if (converter == null) { return(null); } OptionEntry pres; if (!store.TryGetValue(prefix, out pres) || pres == null || pres.Value == null) { return(null); } var arrs = pres.Value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); var res = Array.CreateInstance(innerType, arrs.Length); int index = 0; foreach (var x in arrs) { res.SetValue(converter(x), index); } return(res); } else { return(null); } } //complex object var obj = instance ?? Creator(type); if (obj == null || instance != null) { if (instance == null) { obj = Activator.CreateInstance(type); } foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { var val = GetOptionObject(prefix + "." + propertyName(prop), prop.PropertyType, instance == null ? null : prop.GetValue(instance)); if (val != null) { prop.SetValue(obj, val); } } } return(obj); }