/* Copyright (c) 2016 JOSEPH ALBAHARI Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following condition: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ public static System.Dynamic.ExpandoObject ToExpando (object value, string include = null, string exclude = null, bool nameOrder = false, bool includePrivate = false) { if (value == null) return null; string[] includeMembers = (include ?? "").Split (", ".ToCharArray (), StringSplitOptions.RemoveEmptyEntries); string[] excludeMembers = (exclude ?? "").Split (", ".ToCharArray (), StringSplitOptions.RemoveEmptyEntries); var type = value.GetType (); var memberInfos = includeMembers.Any () ? includeMembers.SelectMany (name => type.GetMember (name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) : value.GetType () .GetMembers (BindingFlags.Instance | BindingFlags.Public | (includePrivate ? BindingFlags.NonPublic : 0)) .Where (m => m is PropertyInfo || m is FieldInfo); if (excludeMembers.Any ()) memberInfos = memberInfos.Where (m => !excludeMembers.Contains (m.Name)); if (nameOrder) memberInfos = memberInfos.OrderBy (m => m.Name); return ToExpando (value, memberInfos.ToArray ()); } public static System.Dynamic.ExpandoObject ToExpando (object value, IEnumerable include) { IDictionary expando = new System.Dynamic.ExpandoObject (); foreach (var member in include.Where (IsInstance)) try { if (member is FieldInfo) expando [member.Name] = ((FieldInfo)member).GetValue (value); else if (member is PropertyInfo && ((PropertyInfo)member).GetIndexParameters ().Length == 0) expando [member.Name] = ((PropertyInfo)member).GetValue (value); else if (member is MethodInfo && ((MethodInfo)member).GetParameters ().Length == 0) expando [member.Name] = ((MethodInfo)member).Invoke (value, null); // Ignore other kinds of member } catch (Exception ex) // If it faults, record the error in the expando. { expando.Add (member.Name, ex.WithoutWrappers ()); } return (System.Dynamic.ExpandoObject)expando; } static bool IsInstance (MemberInfo mi) => mi is FieldInfo fi ? (fi.Attributes & FieldAttributes.Static) != FieldAttributes.Static : mi is PropertyInfo pi ? (pi.GetMethod != null && !pi.GetMethod.IsStatic || pi.SetMethod != null && !pi.SetMethod.IsStatic) : mi is MethodInfo mei ? !mei.IsStatic : false; // We only care about fields, properties and methods.