示例#1
0
        internal static void SetProperty(this IObjectSummary obj, string name, object value)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var prop = obj.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);

            if (prop != null && prop.CanWrite)
            {
                if (value is SwitchParameter sp)
                {
                    value = sp.IsPresent;
                }

                prop.SetValue(obj, value, null);
            }
            else
            {
                throw new ArgumentException("No writeable property found.");
            }
        }
示例#2
0
        internal static T GetProperty <T>(this IObjectSummary obj, string name)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var prop = obj.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);

            if (prop != null && prop.CanRead)
            {
                return((T)prop.GetValue(obj));
            }
            else
            {
                throw new ArgumentException("No property found.");
            }
        }