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: microsoft .net c#]

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

    Add a comment to this post:

    Your name:

    Your email: Email me new comments on this post
      (Your email will not be published on this site, and will only be used to contact you directly with a reply to your comment if needed. Oh, and we'll use it to send you new comments on this post it you selected that checkbox.)


    Your website:

    Comments: