6/26/2023 0 Comments Vice versa example![]() ![]() TargetProperty.SetValue(someObject, item.Value) Var targetProperty = someObjectType.GetProperty(key) IDictionary objectBackToDictionary = someObject.AsDictionary() īuilding on Matías Fidemraizer's answer, here is a version that supports binding to object properties other than strings. PropInfo => propInfo.GetValue(source, null)ĭictionary dictionary = new Dictionary() Return source.GetType().GetProperties(bindingAttr).ToDictionary Public static IDictionary AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) ![]() Var someObjectType = someObject.GetType() Public static T ToObject(this IDictionary source) Right, others did mostly the same solution, but this uses less reflection which is more performance-wise and way more readable: public static class ObjectExtensions ![]() Using some reflection and generics in two extension methods you can achieve that. ![]()
0 Comments
Leave a Reply. |