A small example of how to find all the properties on an object using .NET reflection

    I had a need to do this today, and thought I'd backup the code here...

      using System;
      using System.Reflection;
      
      namespace DumpObject
      {
      	/// 
      	/// An example of how to use .NET reflection to dump a list of all the properties of an object
      	/// 
      	class Class1
      	{
      		[STAThread]
      		static void Main(string[] args)
      		{
      			// This is the DLL to grab the object from
      			Assembly assembly = Assembly.LoadFrom (@"c:\t600\debug\trimsdknet.dll");
      
      			// The object to grab, including namespace
      			Type obj = assembly.GetType("TRIMSDK.PropertyDef", true, true);
      
      			// Now iterate through the properties of that object
      			foreach(PropertyInfo pinfo in obj.GetProperties())
      			{
      				// Output the type of the member, and it's name...
      				Console.WriteLine(pinfo.PropertyType.FullName.ToString() + "\t\t\t" +
      					pinfo.Name.ToString());
      			}
      		}
      	}
      }
      


    Tags for this post: dotnet microsoft .net c#
    Related posts: Profiling your code in mono; GetOpt# 0.1; Getting ASP.NET working on Windows XP Tablet PC edition; Regulator; SQL Down under; Microsoft not rewriting their products in .NET?

posted at: 16:26 | path: /dotnet | permanent link to this entry