What's wrong with this reflection code? GetFields() is returning an empty array

后端 未结 3 1310
囚心锁ツ
囚心锁ツ 2021-02-03 23:51

C#, Net 2.0

Here\'s the code (I took out all my domain-specific stuff, and it still returns an empty array):

using System;
using System.Collections.Gene         


        
3条回答
  •  悲哀的现实
    2021-02-04 00:36

    The parameterless GetFields() returns public fields. If you want non-public ones, use:

    cc.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
    

    or whatever appropriate combination you want - but you do need to specify at least one of Instance and Static, otherwise it won't find either. You can specify both, and indeed public fields as well, to get everything:

    cc.GetType().GetFields(BindingFlags.Instance | 
                           BindingFlags.Static |
                           BindingFlags.NonPublic |
                           BindingFlags.Public);
    

提交回复
热议问题