Get a value from a non-public member

SCG 0 Reputation points
2024-07-27T02:28:00.4766667+00:00

Hello all,

I am trying to access a loaded third party object that I need to pull information out of it. I can’t figure out how to get/pull a value deep in the object under the Non-Public Members area.

Here is what I mean.

customer→ “Non-Public members” → m_customerModel ->UserDefinedFlds→ UsrDfnd1 that has value of “Hello There” that I am looking for.

From Debug->Watch and entering the following I get the value am looking for but can’t figure out how to put in a var..

cusled.m_customerModel.UserDefinedFlds.UsrDfnd1

Any suggestions on how I can get this into the var?.

Thanks everyone…

SCG

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,654 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-07-29T07:27:52.1166667+00:00

    Hi @SCG , Welcome to Microsoft Q&A,

    In C#, if you need to access non-public members of an object, you usually use reflection. Through reflection, you can access private fields, properties, or methods of a class.

    using System;
    using System.Reflection;
    
    class Program
    {
        static void Main()
        {
            var customer = new Customer();// Assume you have a Customer object
            Type customerType = customer.GetType();
            
            // Get the value of the m_customerModel field
            var mCustomerModelField = customerType.GetField("m_customerModel", BindingFlags.NonPublic | BindingFlags.Instance);
            var mCustomerModelValue = mCustomerModelField.GetValue(customer);
    
            // Get the value of the UserDefinedFlds field
            var userDefinedFldsField = mCustomerModelValue.GetType().GetField("UserDefinedFlds", BindingFlags.NonPublic | BindingFlags.Instance);
            var userDefinedFldsValue = userDefinedFldsField.GetValue(mCustomerModelValue);
    
            // Get the value of the UsrDfnd1 field
            var usrDfnd1Field = userDefinedFldsValue.GetType().GetField("UsrDfnd1", BindingFlags.NonPublic | BindingFlags.Instance);
            var usrDfnd1Value = usrDfnd1Field.GetValue(userDefinedFldsValue);
    
            Console.WriteLine(usrDfnd1Value); // Output: Hello There
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.