示例#1
0
    //Set an object's Property value by the provided name and type.
    public static bool SetPropertyValue(this System.Object obj, string name, System.Object value)
    {
        PropertyInfo prop = obj.GetProperty(name);

        if (prop != null)
        {
            MethodInfo method = prop.GetSetMethod();
            if (method != null)
            {
                if (prop.PropertyType.IsAssignableFrom(value.GetType()))
                {
                    method.Invoke(obj, new System.Object[] { value });

                    return(true);
                }

                //Debug.LogWarning("ReflectionF.SetPropertyValue: Cannot assign " + value + " to property " + name + " on instance of " + obj.GetType().ShortName());
                Debug.LogWarning("ReflectionF>SetPropertyValue: Property " + name + " on instance of " + obj.GetType().ShortName() + " does not match expected type.");
                return(false);
            }

            Debug.LogWarning("ReflectionF.SetPropertyValue: Property " + name + " on instance of " + obj.GetType().ShortName() + " does not have set method.");
            return(false);
        }

        Debug.LogWarning("ReflectionF.SetPropertyValue: Property " + name + " on instance of " + obj.GetType().ShortName() + " does not exist.");
        return(false);
    }
示例#2
0
    //Get an object's Property or Field value by a provided name and type.
    public static T GetObjectValue <T>(this System.Object obj, string name)
    {
        if (obj.GetField(name) != null)
        {
            return(obj.GetFieldValue <T>(name));
        }
        else if (obj.GetProperty(name) != null)
        {
            return(obj.GetPropertyValue <T>(name));
        }

        Debug.LogWarning("ReflectionF.GetObjectValue: No field or property named " + name + " exists on instance of " + obj.GetType().ShortName());
        return(default(T));
    }
示例#3
0
    //Set an object's Property or Field by the provided name to a value
    public static bool SetObjectValue(this System.Object obj, string name, System.Object value)
    {
        if (obj.GetField(name) != null)
        {
            return(obj.SetFieldValue(name, value));
        }
        else if (obj.GetProperty(name) != null)
        {
            return(obj.SetPropertyValue(name, value));
        }

        Debug.LogWarning("ReflectionF.SetObjectValue: No field or property named " + name + " exists on instance of " + obj.GetType().ShortName());
        return(false);
    }
示例#4
0
        public static T GetPropertyOrDefault <T>(this System.Object @this_, string name, T defaultT)
        {
            bool success;
            T    ret = @this_.GetProperty <T>(name, out success);

            if (success)
            {
                return(ret);
            }
            else
            {
                return(defaultT);
            }
        }
        protected bool ComparedObjectValueIsTrue(SerializedProperty property)
        {
            var conditionalHelpAttribute = attribute as ConditionalHelpAttribute;

            System.Object objectInstance = property.GetTargetObjectWithProperty();
            FieldInfo     field          = objectInstance.GetField(conditionalHelpAttribute.TargetConditionName);
            PropertyInfo  accessor       = objectInstance.GetProperty(conditionalHelpAttribute.TargetConditionName);

            var objectValue = field != null?field.GetValue(objectInstance) :
                                  accessor.GetValue(objectInstance);

            if (!objectValue.ToBool(out bool memberValue))
            {
                return(false);
            }

            return(memberValue);
        }
示例#6
0
    //Get an object's Property value by a provided name and type.
    public static T GetPropertyValue <T>(this System.Object obj, string name)
    {
        PropertyInfo prop = obj.GetProperty(name);

        if (prop != null)
        {
            MethodInfo method = prop.GetGetMethod();
            if (method != null)
            {
                if (prop.PropertyType.IsAssignableFrom(typeof(T)))
                {
                    return((T)method.Invoke(obj, null));
                }
                Debug.LogWarning("ReflectionF.GetPropertyValue: Property " + name + " on instance of " + obj.GetType().ShortName() + " does not match expected type.");
                return(default(T));
            }

            Debug.LogWarning("ReflectionF.GetPropertyValue: Property " + name + " on instance of " + obj.GetType().ShortName() + " does not have get method.");
            return(default(T));
        }

        Debug.LogWarning("ReflectionF.GetPropertyValue: Property " + name + " on instance of " + obj.GetType().ShortName() + " does not exist.");
        return(default(T));
    }
示例#7
0
 //Does this object have a property called name
 public static bool HasProperty(this System.Object obj, string name)
 {
     return(obj.GetProperty(name) != null);
 }
示例#8
0
        public static T GetProperty <T>(this System.Object @this_, string name)
        {
            bool dummy;

            return(@this_.GetProperty <T>(name, out dummy));
        }