I am trying to pull a list of Enum types that derive from a parent type. The issue is my method keeps returning back a RuntimeType instead of System.Enum.
I understand why, But I don’t know a way around it.
Here is an example if what I am currently doing:
var monoScript = (MonoScript)target;
var classType = monoScript.GetClass();
var memberInfos = classType.GetMembers();
var enumTypes = memberInfos.where(x => x.GetType() == typeof(Enum));
I have dug into every property of the MemberInfo class and could not find anything that can get me what I need. Using the debugger and breaking on ‘memberInfos’ variable, I can see the correct type being shown, even has the IsEnum bool flagged. I Just don’t know how to return that info instead of a RunTime Type.
I’m not sure what you mean by list of enum types. Do you really mean a list of System.Type objects of all members of your target class that are an enum type? In this case you have to grab either the fieldType or the propertyType of the actual member. Your main issue is that you used the general GetMembers method which returns a list of MemberInfo instances. However the type information of each member depends on the kind of member. So it may be a FieldInfo or a PropertyInfo. Though there are other member types like MethodInfo, ConstructorInfo, …
You can use usual OOP methods to check for the right type before you down cast the MemberInfo into the proper type or use the MemberType property. Those are the possible member types.
Though usually you would use the specialized methods GetFields or GetProperties which directly return an array of only FieldInfo or PropertyInfo instances.
Note that the usage of reflection should be avoided whenever possible. Are you sure you want to get the various enum types of the members of your class? That generally sounds like a strange usage. If that’s not what you want you really need to be more detailed about what exactly your class looks like and what information you’re looking for.
For example if your target class looks like this
public class Example : MonoBehaviour
{
public MyEnumType1 someVariable;
public MyEnumType2 someOtherVariable;
public MyEnumType1 someProperty {get; set;}
}
public enum MyEnumType1
{
Foo, Bar
}
public enum MyEnumType2
{
Some, Other, Constants
}
When you use GetMembers on this class you would get two FieldInfos and one PropertyInfo. In the case of the FieldInfos you would need to read the FieldType, in the case of the PropertyInfo you have to read the PropertyType.
Both will give you a System.Type object that describes the actual type of the field / property.
I would have used Get Fields/Properties if I was after an instance of the declared enum. However, I am trying to get the declared type.
For Example:
public Class SomeClass{
public enum SomeEnum {}
}
//Other script
void Foo() {
var target; //This is a stored refernce to serialized info of a MonoScript.
var classType = (MonoScript)target.GetClass(); //This would return a type of 'SomeClass'
//From here I wanted to use reflection to get a reference to the Type SomeEnum
}
So what I am actually trying to achieve is this. I am making a custom editor for this script, something that is dynamically used. So there is an object field that they can select a MonoScript from the assets folder. MonoScript types have a method called GetClass() to return that Type. Once one is selected, show a dropdown field of all Enums types declared in that Type/Script.
didn’t really make much sense. A nested type is not a member of a class, it’s just a nested type. It’s essentially a completely seperate type. The only two differences are for classes is that the nested class has “internal” access to private fields of the outer class and that the outerclass name is essentially part of the classname of the nested class.
Besides iterating through all types defined in an assembly, you can use the GetNestedTypes method.