public static dynamic ToDynamic <T>(this T obj, Dictionary <string, object> additionProperties = null) { var dynamicColumn = new PropertySetting { AdditionPropertyDictionary = additionProperties ?? new Dictionary <string, object>() }; return(ToDynamic(obj, dynamicColumn)); }
private static dynamic ConvertToDynamic <T>(T obj, PropertySetting setting, Func <PropertyInfo[]> getProperties) { var dictionary = ConvertToDictionary(obj, setting, getProperties); var expando = new ExpandoObject(); dictionary.ForEach(item => { expando = CreateNewProperty(expando as dynamic, item); }); return(expando); }
//private static dynamic ConvertToDictionary<T>(T obj, PropertySetting setting, Func<PropertyInfo[]> getProperties) private static IDictionary <string, object> ConvertToDictionary <T>(T obj, PropertySetting setting, Func <PropertyInfo[]> getProperties) { IDictionary <string, object> expando = new Dictionary <string, object>(); var properties = setting.Properties.Count == 0 ? getProperties() : typeof(T).GetProperties().Where(o => setting.Properties.Contains(o.Name)); var concurrentDictionary = new ConcurrentDictionary <string, object>(); Parallel.ForEach(properties, propertyInfo => { var propertyExpression = Expression.Property(Expression.Constant(obj), propertyInfo); var constant = (ConstantExpression)propertyExpression.Expression; var value = ((PropertyInfo)propertyExpression.Member).GetValue(constant.Value); concurrentDictionary.TryAdd(propertyInfo.Name, value != null ? value.ToString() : null); }); concurrentDictionary.ToList().ForEach(expando.Add); //TODO check null setting.AdditionPropertyDictionary.ToList().ForEach(keyValuePair => CreateNewProperty(expando, keyValuePair)); return(expando); }
public static dynamic ToDynamic <T>(this T obj, PropertySetting setting) { return(ConvertToDynamic(obj, setting, typeof(T).GetProperties)); }