Ejemplo n.º 1
0
        private static T NewSetProperties <T>(IEnumerable <KeyValuePair <string, object> > values)
        {
            var    writeable = Types.WriteablePublicFieldsAndProperties(typeof(T)).ToDictionary(m => m.Name, StringComparer.OrdinalIgnoreCase); // case insensitive
            object item      = Activator.CreateInstance(typeof(T));                                                                             // force boxing so setting values via reflection works for structs

            foreach (var pair in values)
            {
                MemberInfo member;
                if (writeable.TryGetValue(pair.Key, out member))
                {
                    member.SetValue(item, pair.Value);
                }
            }
            return((T)item); // unbox struct
        }