- Home /
Reflection Ignore Unsupported Properties
I am using reflection in an editor script. The script looks up all of the components on a GameObject and returns their properties with GetProperties();
Some properties are obsolete and return an error when used in conjunction with GetValue such as:
minVolume is not supported anymore. Use min-, maxDistance and rolloffMode instead.
System.Reflection.MonoProperty:GetValue(Object, Object[])
How can we check for these obsolete properties or simply avoid having to see these error messages?
try{
propertyValue = propertyInfo.GetValue(myComponent,null);
} catch {
// is not catching the error
}
Edit: The problematic properties I've found were "rolloffFactor","minVolume", and "maxVolume" which are all from AudioSource. My current workaround is to check for these property names and skip them.
Answer by whydoidoit · Jun 25, 2012 at 12:09 PM
You need to use:
if(!propertyInfo.IsDefined(typeof(ObsoleteAttribute), true))
propertyValue = propertyInfo.GetValue(myComponent,null);
Doesn't work with for Animator and some other: "Setting and getting Body Position/Rotation, IK Goals, Lookat and BoneLocalRotation should only be done in OnAnimatorIK or OnStateIK"
Answer by Synthesizer · Jun 25, 2012 at 12:06 PM
I accidently just posted some code first, but the idea is to look for an Obsolete attribute on your property.
Here's some more info: http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getcustomattributes.aspx
And a code snippet:
object[] attr = propertyInfo.GetCustomAttributes (typeof(ObsoleteAttribute), true);
if(attr.Length > 0)
continue;
You are better off using IsDefined though - there's no array allocated so it removes the chance of extra GC ;)