DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore
  •   c#
  •  | 
  •  
  •  | 
  •   ( words)

Question

I have a class Trigger with some members of type SimpleAction:

public SimpleAction OnOver;
public SimpleAction OnOut;
public SimpleAction OnDown;
public SimpleAction OnUp;
public SimpleAction OnClick;
public SimpleAction OnHold;

Now, from another class I'm doing this:

MemberInfo[] members = triggerScript.GetType().GetMembers();
    foreach (MemberInfo item in members) {
            Debug.Log(item.Name);
    }

In log massages I can see list of names of all members of Trigger class. OnOver, OnOut atc are in that list too. But how can I separate members of type SimpleAction from others? item.DeclaringType is type of Trigger item.MemberType is "field" for tham. item.ReflectedType is Trigger too. How can I get a member list of SimpleAction type?

Solution

Use method GetFields then filter field with FieldType is SimpleAction

 FieldInfo[] fields = triggerScript.GetType()
            .GetFields(BindingFlags.Public | BindingFlags.Instance)
            .Where(field => field.FieldType == typeof (SimpleAction))
            .ToArray();

 foreach (var field in fields)
 {

 }

OTHER TIPS

Use GetFields instead of GetMembers, then you can filter based on .FieldType == typeof(SimpleAction).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow