0
Q:

c# get all class properties

//through reflection
using System.Reflection;

//as a reusable method/function
Type[] GetInheritedClasses(Type MyType) 
{
  	//if you want the abstract classes drop the !TheType.IsAbstract but it is probably to instance so its a good idea to keep it.
	return Assembly.GetAssembly(MyType).GetTypes().Where(TheType => TheType.IsClass && !TheType.IsAbstract && TheType.IsSubclassOf(MyType));
}

//or as a selection of a type in code.
Type[] ChildClasses Assembly.GetAssembly(typeof(YourType)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(YourType))));

//Example of usage
foreach (Type Type in
                Assembly.GetAssembly(typeof(BaseView)).GetTypes()
                .Where(TheType => TheType.IsClass && !TheType.IsAbstract && TheType.IsSubclassOf(typeof(BaseView))))
            {

                AddView(Type.Name.Replace("View", ""), (BaseView)Activator.CreateInstance(Type));
            }
4
//through reflection
using System.Reflection;

//Get a List of the properties from a type
public static PropertyInfo[] ListOfPropertiesFromInstance(Type AType)
{
  	if (InstanceOfAType == null) return null;
    return AType.GetProperties(BindingFlags.Public);
}

//Get a List of the properties from a instance of a class
public static PropertyInfo[] ListOfPropertiesFromInstance(object InstanceOfAType)
{
  	if (InstanceOfAType == null) return null;
  	Type TheType = InstanceOfAType.GetType();
    return TheType.GetProperties(BindingFlags.Public);
}

//purrfect for usage example and Get a Map of the properties from a instance of a class
public static Dictionary<string, object> DictionaryOfPropertiesFromInstance(object InstanceOfAType)
{
    if (InstanceOfAType == null) return null;
    Type TheType = InstanceOfAType.GetType();
    PropertyInfo[] Properties = TheType.GetProperties(BindingFlags.Public);
    Dictionary<string, PropertyInfo> PropertiesMap = new Dictionary<string, PropertyInfo>();
    foreach (PropertyInfo Prop in Properties)
    {
        PropertiesMap.Add(Prop.Name, Prop);
    }
    return PropertiesMap;
}
1
using System.Reflection;

FieldInfo[] property_infos = typeof(Player).GetFields();
0
// List the properties.
// Use the class you want to study instead of Form1.

PropertyInfo[] property_infos = typeof(Form1).GetProperties(
    BindingFlags.FlattenHierarchy |
    BindingFlags.Instance |
    BindingFlags.NonPublic |
    BindingFlags.Public |
    BindingFlags.Static);
0

New to Communities?

Join the community