How to

get object properties using reflection

Published: 13. December 2008 | Updated: 27. October 2011
License: Microsoft Public License (MS-PL)
Categories: Framework » Reflection
Tags: C# Reflection
Was this snippet helpful for you? YESYES / NONO

Short example how to list all properties of an object

Import namespace

using System.Reflection;

Code

Type type = typeof(Type);
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo item in properties)
{
    Console.WriteLine("public {0} {1}", item.PropertyType, item.Name);
}
Console Output:
public System.Reflection.MemberTypes MemberType
public System.Type DeclaringType
public System.Reflection.MethodBase DeclaringMethod
public System.Type ReflectedType
public System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute
public System.Guid GUID
public System.Reflection.Module Module
public System.Reflection.Assembly Assembly
public System.RuntimeTypeHandle TypeHandle
public System.String FullName
public System.String Namespace
public System.String AssemblyQualifiedName
public System.Type BaseType
public System.Reflection.ConstructorInfo TypeInitializer
public System.Boolean IsNested
public System.Reflection.TypeAttributes Attributes
public System.Reflection.GenericParameterAttributes GenericParameterAttributes
public System.Boolean IsVisible
public System.Boolean IsNotPublic
public System.Boolean IsPublic
public System.Boolean IsNestedPublic
public System.Boolean IsNestedPrivate
public System.Boolean IsNestedFamily
public System.Boolean IsNestedAssembly
public System.Boolean IsNestedFamANDAssem
public System.Boolean IsNestedFamORAssem
public System.Boolean IsAutoLayout
public System.Boolean IsLayoutSequential
public System.Boolean IsExplicitLayout
public System.Boolean IsClass
public System.Boolean IsInterface
public System.Boolean IsValueType
public System.Boolean IsAbstract
public System.Boolean IsSealed
public System.Boolean IsEnum
public System.Boolean IsSpecialName
public System.Boolean IsImport
public System.Boolean IsSerializable
public System.Boolean IsAnsiClass
public System.Boolean IsUnicodeClass
public System.Boolean IsAutoClass
public System.Boolean IsArray
public System.Boolean IsGenericType
public System.Boolean IsGenericTypeDefinition
public System.Boolean IsGenericParameter
public System.Int32 GenericParameterPosition
public System.Boolean ContainsGenericParameters
public System.Boolean IsByRef
public System.Boolean IsPointer
public System.Boolean IsPrimitive
public System.Boolean IsCOMObject
public System.Boolean HasElementType
public System.Boolean IsContextful
public System.Boolean IsMarshalByRef
public System.Type UnderlyingSystemType
public System.String Name
public System.Int32 MetadataToken
Send us feedback about this snippet »



Related Snippets: