Created
January 17, 2017 18:09
-
-
Save deepumi/08e73e22403203adf7fe6ab6de53f449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
public class DictionaryParameter | |
{ | |
private readonly Dictionary<string, object> _parameters = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); | |
public DictionaryParameters(object param) | |
{ | |
if (param == null) return; | |
AddValues(param); | |
} | |
public IDictionary<string, object> Parameters => _parameters; | |
private void AddValues(object values) | |
{ | |
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values)) | |
{ | |
Add(descriptor.Name, descriptor.GetValue(values)); | |
} | |
} | |
public void Add(string key, object value) | |
{ | |
_parameters.Add($"@{key}", value); | |
} | |
} | |
public static class KeyValueParameterExtensions | |
{ | |
//Another apporach using Lazy pattern IEnumerable<KeyvVluePair<string,object>> | |
public static IEnumerable<KeyValuePair<string, object>> ToKeyValueList(this object param) | |
{ | |
if (param == null) yield break; | |
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(param)) | |
{ | |
yield return new KeyValuePair<string, object>($"@{descriptor.Name}", descriptor.GetValue(param)); | |
} | |
} | |
} | |
ex : | |
var param = new {ApplicationName = "FFF",minDate = "ff",Another = 1,CreateDate = DateTime.Now,ABC = Guid.NewGuid(),AnotherGuid = Guid.NewGuid()}.ToKeyValueList(); | |
var dict = new DictionaryParameters(new {ApplicationName = "FFF",minDate = "ff",Another = 1,CreateDate = DateTime.Now,ABC = Guid.NewGuid(),AnotherGuid = Guid.NewGuid()}.ToKeyValues()); | |
var dictParameters = dict.Parameters; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment