Example #1
0
        /// <summary>Get a private static property.</summary>
        /// <typeparam name="TValue">The property type.</typeparam>
        /// <param name="type">The type which has the property.</param>
        /// <param name="name">The property name.</param>
        /// <param name="required">Whether to throw an exception if the private property is not found.</param>
        public IPrivateProperty <TValue> GetPrivateProperty <TValue>(Type type, string name, bool required = true)
        {
            // get field from hierarchy
            IPrivateProperty <TValue> property = this.GetPropertyFromHierarchy <TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

            if (required && property == null)
            {
                throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static property.");
            }
            return(property);
        }
Example #2
0
        /****
        ** Properties
        ****/
        /// <summary>Get a private instance property.</summary>
        /// <typeparam name="TValue">The property type.</typeparam>
        /// <param name="obj">The object which has the property.</param>
        /// <param name="name">The property name.</param>
        /// <param name="required">Whether to throw an exception if the private property is not found.</param>
        public IPrivateProperty <TValue> GetPrivateProperty <TValue>(object obj, string name, bool required = true)
        {
            // validate
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj), "Can't get a private instance property from a null object.");
            }

            // get property from hierarchy
            IPrivateProperty <TValue> property = this.GetPropertyFromHierarchy <TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            if (required && property == null)
            {
                throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance property.");
            }
            return(property);
        }
Example #3
0
 /// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
 /// <typeparam name="T">The property value type.</typeparam>
 /// <param name="property">The property being accessed.</param>
 /// <returns>Returns the same property instance for convenience.</returns>
 private IPrivateProperty <T> AssertAccessAllowed <T>(IPrivateProperty <T> property)
 {
     this.AssertAccessAllowed(property?.PropertyInfo);
     return(property);
 }