baidu
C# 反射 获取 list 字段
示例代码:
private Type CheckTypeString(string typeStr)
{
Type ret = Type.GetType(typeStr + ", UnityEditor");
if (ret == null)
{
ret = Type.GetType("UnityEditor." + typeStr + ", UnityEditor");
}
if (ret == null)
{
UnityEngine.Debug.Log(string.Format("Cannot find {0}", typeStr));
return null;
}
return ret;
}
protected System.Reflection.MethodInfo CheckTypeMethod(Type type, string methodName)
{
return type.GetMethod(methodName);
}
protected System.Reflection.FieldInfo CheckTypeField(Type type, string fieldName)
{
return type.GetField(
fieldName,
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic
);
}
protected void TestAnimEditor()
{
Type typeAnimEditor = CheckTypeString("AnimEditor");
if (null != typeAnimEditor)
{
System.Reflection.MethodInfo method = CheckTypeMethod(typeAnimEditor, "GetAllAnimationWindows");
if (null != method)
{
object obj = method.Invoke(null, null);
//List
System.Reflection.FieldInfo fieldState = CheckTypeField(typeAnimEditor, "m_State");
if (null != fieldState)
{
object fieldValueState = fieldState.GetValue(curAnimEditor);
Type animationWindowStateType = CheckTypeString("AnimationWindowState");
if (null != animationWindowStateType)
{
System.Reflection.FieldInfo fieldControl = CheckTypeField(animationWindowStateType, "m_ControlInterface");
if (null != fieldControl)
{
object fieldValueControl = fieldControl.GetValue(fieldValueState);
Type animationWindowControlType = CheckTypeString("AnimationWindowControl");
if (null != animationWindowControlType)
{
System.Reflection.FieldInfo fieldAnimationClip = CheckTypeField(animationWindowControlType, "activeAnimationClip");
if (null != fieldAnimationClip)
{
object animationClipValue = fieldAnimationClip.GetValue(animationWindowControlType);
AnimationClip animationClip = animationClipValue as AnimationClip;
UnityEngine.Debug.Log(string.Format("Cannot find {0}", ""));
}
}
}
}
}
UnityEngine.Debug.Log(string.Format("Cannot find {0}", ""));
}
}
}
protected void TestAnimEditorV2()
{
Type typeAnimEditor = CheckTypeString("AnimEditor");
if (null != typeAnimEditor)
{
System.Reflection.MethodInfo method = CheckTypeMethod(typeAnimEditor, "GetAllAnimationWindows");
if (null != method)
{
object obj = method.Invoke(null, null);
//List
//IEnumerable
System.Collections.IEnumerable objEnm = obj as System.Collections.IEnumerable;
System.Collections.IEnumerator curIte = objEnm.GetEnumerator();
curIte.MoveNext();
object curAnimEditor = curIte.Current;
System.Reflection.FieldInfo fieldState = CheckTypeField(typeAnimEditor, "state");
if (null != fieldState)
{
object fieldValueState = fieldState.GetValue(curAnimEditor);
Type animationWindowStateType = CheckTypeString("AnimationWindowState");
if (null != animationWindowStateType)
{
System.Reflection.FieldInfo fieldAnimationClip = CheckTypeField(animationWindowStateType, "activeAnimationClip");
if (null != fieldAnimationClip)
{
object animationClipValue = fieldAnimationClip.GetValue(typeof(AnimationClip));
AnimationClip animationClip = animationClipValue as AnimationClip;
UnityEngine.Debug.Log(string.Format("Cannot find {0}", ""));
}
}
}
UnityEngine.Debug.Log(string.Format("Cannot find {0}", ""));
}
}
}