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?

有帮助吗?

解决方案

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)
 {

 }

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top