//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)); }
//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); }
//Get an object's Field value by a provided name and type. public static T GetFieldValue <T>(this System.Object obj, string name) { FieldInfo field = obj.GetField(name); if (field != null) { if (field.FieldType.IsAssignableFrom(typeof(T))) { return((T)field.GetValue(obj)); } Debug.LogWarning("ReflectionF.GetFieldValue: Field " + name + " on instance of " + obj.GetType().ShortName() + " does not match expected type."); return(default(T)); } Debug.LogWarning("ReflectionF.GetFieldValue: Field " + name + " on instance of " + obj.GetType().ShortName() + " does not exist."); return(default(T)); }
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); }
//Set an object's Property value by the provided name and type. public static bool SetFieldValue(this System.Object obj, string name, System.Object value) { FieldInfo field = obj.GetField(name); if (field != null) { if (field.FieldType.IsAssignableFrom(value.GetType())) { field.SetValue(obj, value); return(true); } Debug.LogWarning("ReflectionF.SetFieldValue: Field " + name + " on instance of " + obj.GetType().ShortName() + " does not match expected type."); return(true); } Debug.LogWarning("ReflectionF.SetFieldValue: Field " + name + " on instance of " + obj.GetType().ShortName() + " does not exist."); return(false); }
//Does this object have a field called name public static bool HasField(this System.Object obj, string name) { return(obj.GetField(name) != null); }