Rocco
Rocco
CC#
Created by Rocco on 3/18/2023 in #help
✅ Getting array values through Reflection
I'm making an "interpreter" using reflection but I don't know how to access the value of array elements. I've tried with IEnumerators this way:
C++

//at this point splitFunction[index] should be something like "arrayName[2]"
//nestedValue is the object that contains the array
string[] splitEnumerator = splitFunction[index].Split('[');
FieldInfo field = nestedValue.GetType().GetField(splitEnumerator[0]);
int enumIndex = int.Parse(splitEnumerator[1].Replace("]", ""));
IEnumerator theEnum = ((IEnumerable)field.GetValue(nestedValue)).GetEnumerator();
theEnum.Reset();
for(int m = 0; m < enumIndex; m++)
{
theEnum.MoveNext();
}
nestedValue = theEnum.Current;
C++

//at this point splitFunction[index] should be something like "arrayName[2]"
//nestedValue is the object that contains the array
string[] splitEnumerator = splitFunction[index].Split('[');
FieldInfo field = nestedValue.GetType().GetField(splitEnumerator[0]);
int enumIndex = int.Parse(splitEnumerator[1].Replace("]", ""));
IEnumerator theEnum = ((IEnumerable)field.GetValue(nestedValue)).GetEnumerator();
theEnum.Reset();
for(int m = 0; m < enumIndex; m++)
{
theEnum.MoveNext();
}
nestedValue = theEnum.Current;
But I get this error:
InvalidOperationException: Enumeration has not started. Call MoveNext.
System.Array+ArrayEnumerator.get_Current () (at <f3d0e7ec4efc44199bb22a77b1c8eff4>:0)
HabilityInterpreter.<Execute>g__GetValue|0_1 (System.String _command, HabilityInterpreter+<>c__DisplayClass0_0& ) (at Assets/Scripts/Classes/HabilityInterpreter.cs:179)
HabilityInterpreter.<Execute>g__CheckIfTrue|0_3 (System.String _command, HabilityInterpreter+<>c__DisplayClass0_0& ) (at Assets/Scripts/Classes/HabilityInterpreter.cs:238)
HabilityInterpreter+<Execute>d__0.MoveNext () (at Assets/Scripts/Classes/HabilityInterpreter.cs:57)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <d8d38b92d94a4ff8a91e61d39c6d19cd>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
MatchManager:Update() (at Assets/Scripts/Match/MatchManager.cs:242)
InvalidOperationException: Enumeration has not started. Call MoveNext.
System.Array+ArrayEnumerator.get_Current () (at <f3d0e7ec4efc44199bb22a77b1c8eff4>:0)
HabilityInterpreter.<Execute>g__GetValue|0_1 (System.String _command, HabilityInterpreter+<>c__DisplayClass0_0& ) (at Assets/Scripts/Classes/HabilityInterpreter.cs:179)
HabilityInterpreter.<Execute>g__CheckIfTrue|0_3 (System.String _command, HabilityInterpreter+<>c__DisplayClass0_0& ) (at Assets/Scripts/Classes/HabilityInterpreter.cs:238)
HabilityInterpreter+<Execute>d__0.MoveNext () (at Assets/Scripts/Classes/HabilityInterpreter.cs:57)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <d8d38b92d94a4ff8a91e61d39c6d19cd>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
MatchManager:Update() (at Assets/Scripts/Match/MatchManager.cs:242)
On the line I retrieve the value. (also any other way of making an interpreter in unity that you can think of, please tell)
32 replies