Object - GetProperties
A T extension method that gets a property.
public static void Main() { //C# Extension Method: Object - GetProperties var properties = typeof(PropertyModel<int>).GetProperties(); foreach(var property in properties) { Console.WriteLine(property.GetDeclaraction()); } } public class PropertyModel<T> : AbstractPropertyModel { public static int StaticProperty { get; set; } public override int OverrideProperty { get; set; } public virtual int VirtualProperty { get; set; } internal int InternalProperty { get; set; } private int PrivateProperty { get; set; } protected int ProtectedProperty { get; set; } protected internal int ProtectedInternalProperty { get; set; } public int PublicProperty { get; set; } public int PublicGetterPrivateSetterProperty { get; private set; } public int PrivateGetterPublicSetterProperty { private get; set; } public T GenericProperty { get; set; } public T this[T param1, int param2] { get { return param1; }} public override int AbstractProperty { get; set; } } public abstract class AbstractPropertyModel { public abstract int AbstractProperty { get; set; } public virtual int OverrideProperty { get; set; } }
View Source
using System.Reflection; public static partial class Extensions { /// <summary>An object extension method that gets the properties.</summary> /// <param name="this">The @this to act on.</param> /// <returns>An array of property information.</returns> public static PropertyInfo[] GetProperties(this object @this) { return @this.GetType().GetProperties(); } /// <summary>An object extension method that gets the properties.</summary> /// <param name="this">The @this to act on.</param> /// <param name="bindingAttr">The binding attribute.</param> /// <returns>An array of property information.</returns> public static PropertyInfo[] GetProperties(this object @this, BindingFlags bindingAttr) { return @this.GetType().GetProperties(bindingAttr); } }