简体   繁体   English

使用反射获取 c# 上的字段值

[英]Using Reflection get value of the field on c#

I want to get value of the subfield('m_fullDescriptor') however it throws an error like below.我想获取subfield('m_fullDescriptor')值,但是它会引发如下错误。 How can i get the value of the subfield?我怎样才能得到子字段的值?

Structure;结构;

enter image description here在此处输入图像描述在此处输入图像描述 Code;代码;

 public class FieldsClass
        {
            public string fieldA;

            public FieldsClass()
            {
                fieldA = "A public field";
            }
        }


FieldsClass fieldsInst = new FieldsClass();

var specFields = con.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "pmCS").FieldType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "m_pm").FieldType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "m_fullDescriptor");

fieldsInst.fieldA= (string)specFields.GetValue(con);

I get below Error;我得到以下错误;

System.ArgumentException: 'Field 'm_fullDescriptor' defined on type 'OracleInternal.ConnectionPool.PoolManager`3[OracleInternal.ConnectionPool.OraclePoolManager,OracleInternal.ConnectionPool.OraclePool,OracleInternal.ServiceObjects.OracleConnectionImpl]' is not a field on the target object which is of type 'Oracle.ManagedDataAccess.Client.OracleConnection'.'

Don't use Single from Linq to find the field you need.不要使用 Linq 中的Single来查找您需要的字段。 Use GetField method, it already takes exact name of the field.使用GetField方法,它已经获取了字段的确切名称。 Flags can be reused as well.标志也可以重复使用。

To get value, an object is needed to be passed where that field is defined.要获得价值,需要在定义该字段的位置传递 object。

I believe this should work, not tested.我相信这应该有效,而不是经过测试。

var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

var pmCS = connection.GetType().GetField("pmCS", flags).GetValue(connection);
var m_pm = pmCS.GetType().GetField("m_pm", flags).GetValue(pmCS);
var m_fullDescriptor = m_pm.GetType().GetField("m_fullDescriptor", flags).GetValue(m_pm);

You are trying to get the value of the m_fullDescriptor which exists on the OracleInternal.ConnectionPool.OraclePoolManager type, but you are passing an object ( con ) to GetValue that is not of that type.您正在尝试获取存在于OracleInternal.ConnectionPool.OraclePoolManager类型上的m_fullDescriptor的值,但您将 object ( con ) 传递给GetValue不是该类型。

Something like this should work, however, I was unable to test it because I do not have access to an Oracle Database.像这样的东西应该可以工作,但是,我无法测试它,因为我无权访问 Oracle 数据库。

var pmCSField = connection.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "pmCS");

var m_pmField = pmCSField.FieldType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "m_pm");

var m_fullDescriptorField = m_pmField.FieldType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "m_fullDescriptor");

var pmCSFieldValue = pmCSField.GetValue(connection);

var m_pmFieldValue = m_pmField.GetValue(pmCSFieldValue);

var m_fullDescriptorFieldValue = m_fullDescriptorField.GetValue(m_pmFieldValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM