0

so I recently found out about reflection in c# and I got a problem. So I have a class that holds classes as fields (all of these fields have a method reset()), and I tried getting all the fields names and then get their type and invoke their reset() method like this:

List<string> fieldnames = new List<string>();
Type t = typeof(AnimationHolder);

foreach(MemberInfo m in t.GetFields())
{
   fieldnames.Add(m.Name);
}

for (int i = 0; i < fieldnames.Count; i++)
{
    Type atype = Type.GetType(fieldnames[i]);
    MethodInfo reset = atype.GetMethod("Reset");
    
    reset.Invoke(type.GetField(fieldnames[i]), null);
}

this just throws a NullReferenceException, well I wasn't expecting much, but I'm really eager for the answer. Please help.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
tyruji
  • 9
  • 1
  • Method names are case sensitive, `"reset" != "Reset"`. Please post the `reset()` method signature – Mathias R. Jessen Aug 19 '20 at 00:32
  • Your edit to change the code has completely changed the question. **Do not do this.** If the problem with the code you posted has been resolved, but you still need help, **post a new question**. In the new question, you can provide the updated code. – Peter Duniho Aug 19 '20 at 03:05
  • Please see, for example: https://meta.stackoverflow.com/questions/283450/editing-question-with-significant-change-possibly-affecting-existing-answers, https://meta.stackoverflow.com/questions/274958/chameleon-question-changed-from-one-duplicate-to-another, https://meta.stackoverflow.com/questions/266767/what-is-the-the-best-way-to-ask-follow-up-questions, https://meta.stackoverflow.com/questions/290746/follow-on-question-vs-edit-to-original-when-to-use-which, https://meta.stackoverflow.com/questions/285653/is-it-wrong-for-op-to-incorporate-suggested-changes-into-their-posted-code, etc. – Peter Duniho Aug 19 '20 at 03:11

0 Answers0