Created
July 26, 2013 01:57
-
-
Save monyskynet/6085446 to your computer and use it in GitHub Desktop.
Object's Properties to Dictionary
This file contains hidden or 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
public class PropertyBag : DynamicObject { private object _source; public PropertyBag(object source) { _source = source; } public object GetProperty(string name) { var type = _source.GetType(); var property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return property.GetValue(_source, null); } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = GetProperty(binder.Name); return true; } public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { result = GetProperty((string)indexes[0]); return true; } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment