|
using System.Collections.Generic;
using System.ComponentModel;
namespace MvcCheckBoxList.Library {
internal static class ToDictionary_Helper {
/// <summary>
/// Convert object to Dictionary of strings and objects
/// </summary>
/// <param name="_object">Object of Dictionary of strings and objects (e.g. 'new { name="value" }')</param>
/// <returns>Dictionary of strings and objects</returns>
internal static IDictionary<string, object> toDictionary(this object _object) {
if (_object == null) return new Dictionary<string, object>();
if (_object is IDictionary<string, object>) return (IDictionary<string, object>) _object;
var object_properties = TypeDescriptor.GetProperties(_object);
var dictionary = new Dictionary<string, object>(object_properties.Count);
foreach (PropertyDescriptor property in object_properties) {
var name = property.Name.Replace("_", "-");
// JRR - Added the Replace call. This is the standard used by MVC http://www.asp.net/whitepapers/mvc3-release-notes#0.1__Toc274034227
var value = property.GetValue(_object);
dictionary.Add(name, value ?? "");
}
return dictionary;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.