Example #1
0
        /// <summary>
        /// Tries to wrap the specified member.
        /// Returns the wrapped result if it succeeds (valid field/property)
        /// otherwise null
        /// </summary>
        public static RuntimeMember WrapMember(System.Reflection.MemberInfo member, object target)
        {
            var field = member as System.Reflection.FieldInfo;

            if (field != null)
            {
                RuntimeMember wrappedField;
                if (RuntimeMember.TryWrapField(field, target, out wrappedField))
                {
                    return(wrappedField);
                }
            }
            else
            {
                var property = member as System.Reflection.PropertyInfo;
                if (property == null)
                {
                    return(null);
                }

                RuntimeMember wrappedProperty;
                if (RuntimeMember.TryWrapProperty(property, target, out wrappedProperty))
                {
                    return(wrappedProperty);
                }
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// Returns false if the field was constant (literal) while setting 'result' to null.
        /// Otherwise true while setting result to a new RuntimeMember wrapping the specified field
        /// using the appropriate method of building the [s|g]etters (delegates in case of editor/standalone, reflection otherwise)
        /// </summary>
        public static bool TryWrapField(System.Reflection.FieldInfo field, object target, out RuntimeMember result)
        {
            if (field.IsLiteral)
            {
                result = null;
                return(false);
            }

            result         = new RuntimeMember(field, field.FieldType, target);
            result._setter = field.SetValue;
            result._getter = field.GetValue;
            return(true);
        }
Example #3
0
        /// <summary>
        /// Returns false if the property isn't readable or if it's an indexer, setting 'result' to null in the process.
        /// Otherwise true while setting result to a new RuntimeMember wrapping the specified property
        /// using the appropriate method of building the [s|g]etters (delegates in case of editor/standalone, reflection otherwise)
        /// Note that readonly properties (getter only) are fine, as the setter will just be an empty delegate doing nothing.
        /// </summary>
        public static bool TryWrapProperty(System.Reflection.PropertyInfo property, object target, out RuntimeMember result)
        {
            if (!property.CanRead || property.IsIndexer())
            {
                result = null;
                return(false);
            }

            result = new RuntimeMember(property, property.PropertyType, target);

            if (property.CanWrite)
            {
                result._setter = (x, y) => property.SetValue(x, y, null);
            }
            else
            {
                result._setter = (x, y) => { }
            };
            result._getter = x => property.GetValue(x, null);
            return(true);
        }