x


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.

more ▼

asked May 17 '12 at 02:43 PM

absameen gravatar image

absameen
444 50 79 88

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You need to use:

if(!propertyInfo.IsDefined(typeof(ObsoleteAttribute), true))
    propertyValue = propertyInfo.GetValue(myComponent,null); 
more ▼

answered Jun 25 '12 at 12:09 PM

whydoidoit gravatar image

whydoidoit
33.7k 14 23 105

(comments are locked)
10|3000 characters needed characters left

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;
more ▼

answered Jun 25 '12 at 12:06 PM

Synthesizer gravatar image

Synthesizer
16 1

You are better off using IsDefined though - there's no array allocated so it removes the chance of extra GC ;)

Jun 25 '12 at 12:53 PM whydoidoit

Ah, good to know, thanks :)

Jun 25 '12 at 01:04 PM Synthesizer
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x148
x11
x2

asked: May 17 '12 at 02:43 PM

Seen: 519 times

Last Updated: Jun 25 '12 at 01:04 PM