c# use reflection to get a private member variable from a derived class

后端 未结 2 1481
慢半拍i
慢半拍i 2020-12-21 03:55

I have the following structure:

abstract class Parent {}


class Child : Parent
{   
    // Member Variable that I want access to:
    OleDbCommand[] _comman         


        
2条回答
  •  庸人自扰
    2020-12-21 04:36

    It's possible, though it's a decidedly bad idea.

        var field = GetType().GetField("_commandCollection", BindingFlags.Instance | BindingFlags.NonPublic);
    

    I think what you really want to do is provide a method for the child classes to provide the parent with the required data:

    protected abstract IEnumerable GetCommands();
    

提交回复
热议问题