Search code examples
c#private-membersfieldinfo

Access private nested class memebers C#


Im new to C# so i may be way off with what i think is the issue but... Im using a 3rd party SDK to manage loan data. i have a method "GetPayload()" which opens a loan and gets the most recent field change info into "datalist".

public object GetPayload()
    {
        // create list to hold field change data
        List<Ignite.Auditing.DataTypes.Request.Field> DataList = new List<Ignite.Auditing.DataTypes.Request.Field>();

        //create and open new session and loan based on user entered GUID            
        Session ses = OpenSession();
        Loan loan = ses.Loans.Open(loanGuidTextBox.Text);

        //call method to get the field change data for this loan
        AuditTrailEntryList dataList = loan.AuditTrail.GetMostRecentEntries();

        Type myTypeB = typeof(AuditTrailEntry);
        FieldInfo myFieldInfo1 = myTypeB.GetField("auditRecord", BindingFlags.NonPublic | BindingFlags.Instance);


        foreach (AuditTrailEntry entry in dataList)
        {

            FieldInfo[] fields = entry.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
          //  FieldInfo value = (FieldInfo)myFieldInfo1.GetValue(entry.auditRecord.PreviousValue);

            FieldData fd = new FieldData
            {
                FieldDated = entry.Timestamp.ToUniversalTime(),
                FieldDescription = entry.Field.Descriptor.ToString(),
                FieldFormat = entry.Field.Format.ToString(),
                FieldId = entry.Field.ID.ToString(),
                NewValue = entry.Field.Value.ToString(),
                // OldValue = e.PriorValue,
                //OldValue = fce.PriorValue,

                OldValue = myFieldInfo1.GetValue(entry).ToString(),

            };                
            DataList.Add(fd);
        }           


        return _serviceInfo.RequestData;
    }

From there, i do a foreach on each entry in the datalist which has the info i need. i then populate a "FieldData" object (fd) with the needed data. the problem is that one of the fields i need seems to be in what i think is a private field based on what i saw in the debugger (the lock icon next to "auditRecord"). debugger screen shot how do i access "entry.auditRecord.PreviousValue"? ive used Type.GetField (documentation) to get the private member, which kinda works in that it gets "auditRecord", but how do i drill down to the next member i.e auditRecord.PreviousValue? i just want to get the OldValue, which i thought should be entry.auditRecord.PreviousValue but it seems to be inaccessible. No error msg just the wrong values. can someone help?


Solution

  • @xiaoy312 i finally got it! the way i got it to work was to include another dll "EllieMae.EMLite.ClientServer". i was told by my non programmer manager that all the info i needed was in the dlls he provided but he was wrong. for anyone experiencing this issue in the future i had to use

                //get the type info for the audit trail entry
                FieldInfo myFieldInfo1 = typeof(AuditTrailEntry).GetField("auditRecord", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);
    
                //get the value for auditRecord field
                var value = myFieldInfo1.GetValue(entry);
    
                //get old value field
                var oldValue = ((EllieMae.EMLite.ClientServer.AuditRecord)value).PreviousValue;
    

    to get the old value from the AuditTrailEntry object. thanks for your help!