Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by CorruptedTNC · Jun 18, 2014 at 09:26 PM · c#reflection

C# Reflection GetProperties returning unwanted info

So a while ago I requested help with reflection and I made some decent progress. At the time I was only using reflection to retrieve FieldInfo using the following:

 MonoBehaviour[] scripts = selected.GetComponents<MonoBehaviour>();
 for(int i = 0; i < scripts.Length; i++)
 {
     const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
     FieldInfo[] fields = scripts[i].GetType().GetFields(flags);
     //Rest of logic here
 }

This works as expected, I get info from the public fields of the scripts. However when attempting to retrieve properties with the following line of code:

 PropertyInfo[] properties = scripts[i].GetType().GetProperties(flags);

Returns a very long list of properties which as far as I knew didn't even exist as part of a script. Here's the list of names returned:

alt text

Now the only return that I was expecting was the very first property on the list. While I could just keep using fields some of the objects in my game require code which is triggered when a variable changes, so properties are obviously much more suited. Any help is greatly appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Kiwasi · Jun 18, 2014 at 10:21 PM

Add BindingFlags.DeclaredOnly to flags. This will filter out all the inherited properties.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 18, 2014 at 11:46 PM 0
Share

Damn, forgot about that one ;)

+1

avatar image Kiwasi · Jun 19, 2014 at 12:00 AM 0
Share

I'm just getting my head around reflection myself. Needless to say I've spent a lot of time on the reference page lately.

avatar image CorruptedTNC · Jun 19, 2014 at 08:26 AM 0
Share

Certainly a much easier way of handling things, thank you! :D

avatar image
0

Answer by Bunny83 · Jun 18, 2014 at 09:40 PM

Well, those properties are there because your class is derived from MonoBehaviour which is derived from Behaviour which is derived from Component which is derived from UnityEngine.Object and each one adds some more properties to your class. Those are all there, so i'm not sure what exactly you try to figure out?

If MonoBehaviour or any class up the hierarchy would have public fields, you would have get them with GetFields as well. However they don't have any public fields since in OOP you "usually" don't use public fields at all :D --> encapsulation

Anyways, if you want to "filter out" properties which aren't declared in the inspecting class itself, you might want to check the DeclaringType of your property if it's your class.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CorruptedTNC · Jun 18, 2014 at 10:25 PM 0
Share

That explains a lot. I have very little experience using reflection so I wasn't aware of the inheritance nor the hierarchy tree. Thanks very much, I'll make some changes and come back to mark this as an answer once I've had some sleep.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

C# Using Reflection to Automate finding classes 1 Answer

Finding a method by custom attribute and passing to a UI button 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges