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

后端 未结 3 1349
囚心锁ツ
囚心锁ツ 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:35

    Since the field is private, you need to use the overload of GetFields() that allows you to specify BindingFlags.NonPublic.

    To make this work, change it to:

    FieldInfo[] fi = cc.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
    

提交回复
热议问题