Net.Bluewalk.DotNetUtils/Extensions/DictionaryExtensions.cs
/// <summary>
/// Convert dictionary to specified object type
/// </summary>
/// <param name="source"></param>
/// <param name="type"></param>
/// <param name="bindingFlags"></param>
/// <returns></returns>
Convert dictionary to specified object type
Convert dictionary to specified object type
public static object ToObject(this IDictionary<string, object> source, Type type,
            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
        {
            var someObject = Activator.CreateInstance(type);
            var someObjectType = someObject.GetType();
            foreach (var item in source)
            {
                someObjectType
                    .GetProperty(item.Key, bindingFlags)
                    ?.SetValue(someObject, item.Value, null);
            }
            return someObject;
        }
public static object ToObject ( this IDictionary < string , object > source , Type type , BindingFlags bindingFlags = BindingFlags . Instance | BindingFlags . Static | BindingFlags . Public ) { var someObject = Activator . CreateInstance ( type ) ; var someObjectType = someObject . GetType ( ) ; foreach ( var item in source ) { someObjectType . GetProperty ( item . Key , bindingFlags ) ? . SetValue ( someObject , item . Value , null ) ; } return someObject ; }
Convert dictionary to specified object type
Convert dictionary to specified object type
None
IDictionary<string, object>
this
type
Type
None
bindingFlags
BindingFlags
None
source
None