Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

mapping dictionary to object c#

using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace WebOpsApi.Shared.Helpers
{
    public static class MappingExtension
    {
        public static T ToObject<T>(this IDictionary<string, object> source)
            where T : class, new()
        {
            var someObject = new T();
            var someObjectType = someObject.GetType();

            foreach (var item in source)
            {
                var key = char.ToUpper(item.Key[0]) + item.Key.Substring(1);
                var targetProperty = someObjectType.GetProperty(key);


                if (targetProperty.PropertyType == typeof (string))
                {
                    targetProperty.SetValue(someObject, item.Value);
                }
                else
                {

                    var parseMethod = targetProperty.PropertyType.GetMethod("TryParse",
                        BindingFlags.Public | BindingFlags.Static, null,
                        new[] {typeof (string), targetProperty.PropertyType.MakeByRefType()}, null);

                    if (parseMethod != null)
                    {
                        var parameters = new[] { item.Value, null };
                        var success = (bool)parseMethod.Invoke(null, parameters);
                        if (success)
                        {
                            targetProperty.SetValue(someObject, parameters[1]);
                        }

                    }
                }
            }

            return someObject;
        }

        public static IDictionary<string, object> AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
        {
            return source.GetType().GetProperties(bindingAttr).ToDictionary
            (
                propInfo => propInfo.Name,
                propInfo => propInfo.GetValue(source, null)
            );
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# reflection resize array 
Csharp :: unity post processing ui 2d 
Csharp :: how to look for substring in string in c# 
Csharp :: create sequence of squares in c# 
Csharp :: c# convert stream to memorystream 
Csharp :: char contains c# 
Csharp :: c# loop through list of objects 
Csharp :: c# adding to a list 
Csharp :: unity cast int to float 
Csharp :: parse datetime c# 
Csharp :: how return only value of array in laravel 
Csharp :: hello world in unity c# 
Csharp :: what is the meaning of ?? in c# 
Csharp :: connection string in asp.net with username and password 
Csharp :: how to check that string has only alphabet in c# 
Csharp :: unity pause game c# 
Csharp :: raylib c# 
Csharp :: Task.FromResult(null) 
Csharp :: fluent assertions exception 
Csharp :: write last element of dictionary c# 
Csharp :: read file using c# 
Csharp :: asp.net textarea disable resize 
Csharp :: checking if character is a digit or not in c# 
Csharp :: wpf resource dictionary 
Csharp :: unity 2d 
Csharp :: unity create 3d object in script 
Csharp :: how to set foreground from code wpf 
Csharp :: linq sum 
Csharp :: unity set sprite image from script 
Csharp :: linq when name then orderby 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =