12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Text;
- namespace TEAMModelOS.SDK
- {
- public static class ObjectToDictionaryHelper
- {
- public static IDictionary<string, object> ToDictionary(this object source)
- {
- return source.ToDictionary<object>();
- }
- public static IDictionary<string, T> ToDictionary<T>(this object source)
- {
- if (source == null)
- ThrowExceptionWhenSourceArgumentIsNull();
- var dictionary = new Dictionary<string, T>();
- foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
- AddPropertyToDictionary<T>(property, source, dictionary);
- return dictionary;
- }
- private static void AddPropertyToDictionary<T>(PropertyDescriptor property, object source, Dictionary<string, T> dictionary)
- {
- object value = property.GetValue(source);
- if (IsOfType<T>(value))
- dictionary.Add(property.Name, (T)value);
- }
- private static bool IsOfType<T>(object value)
- {
- return value is T;
- }
- private static void ThrowExceptionWhenSourceArgumentIsNull()
- {
- throw new ArgumentNullException("source", "Unable to convert object to a dictionary. The source object is null.");
- }
- }
- }
|