Search code examples
c#propertyinfo

How to get PropertyInfo of C# object


I am trying to extract the PropertyInfo of an object but the propertyInfo returns no properties:

 [TestMethod]
    public void TestGetValueMethod()
    {
      var value = 23;
      System.Reflection.PropertyInfo[] propertyInfo = value.GetType ().GetProperties();
      //DTOPropertyInfo info = new DTOPropertyInfo(propertyInfo[0]);
      System.Diagnostics.Debug.WriteLine(propertyInfo.Length);
    }

propertyInfo.Length returns 0. What am I missing?


Solution

  • using System;
    using System.Reflection;
    
    class Example
    {
        public static void Main()
        {
            string test = "abcdefghijklmnopqrstuvwxyz";
    
            // Get a PropertyInfo object representing the Chars property.
            PropertyInfo pinfo = typeof(string).GetProperty("Chars");
    
            // Show the first, seventh, and last letters
            ShowIndividualCharacters(pinfo, test, 0, 6, test.Length - 1);
    
            // Show the complete string.
            Console.Write("The entire string: ");
            for (int x = 0; x < test.Length; x++)
            {
                Console.Write(pinfo.GetValue(test, new Object[] {x}));
            }
            Console.WriteLine();
        }
    
        static void ShowIndividualCharacters(PropertyInfo pinfo, 
                                             object value,
                                             params int[] indexes)
        {
           foreach (var index in indexes) 
              Console.WriteLine("Character in position {0,2}: '{1}'",
                                index, pinfo.GetValue(value, new object[] { index }));
           Console.WriteLine();                          
        }                                      
    }