简体   繁体   中英

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. 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. Use GetField method, it already takes exact name of the field. Flags can be reused as well.

To get value, an object is needed to be passed where that field is defined.

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.

Something like this should work, however, I was unable to test it because I do not have access to an Oracle Database.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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