c# generic entity for all properties code example
Example 1: c# get all class properties
using System.Reflection;
public static PropertyInfo[] ListOfPropertiesFromInstance(Type AType)
{
if (InstanceOfAType == null) return null;
return AType.GetProperties(BindingFlags.Public);
}
public static PropertyInfo[] ListOfPropertiesFromInstance(object InstanceOfAType)
{
if (InstanceOfAType == null) return null;
Type TheType = InstanceOfAType.GetType();
return TheType.GetProperties(BindingFlags.Public);
}
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;
}
Example 2: class in c#
public class Car
{
public bool isDriving = false;
public Car( string make )
{
Make = make;
}
private string _make = string.Empty;
public string Make
{
get { return _make; }
set { _make = value; }
}
public void drive()
{
if( isDriving )
{
}
else
{
isDriving = true;
}
}
public void stop()
{
if( isDriving )
{
isDriving = false;
}
else
{
}
}
}
using System;
public class Program
{
public static void Main()
{
Car newCar = new Car( "VW" );
Console.WriteLine( newCar.Make );
newCar.drive();
Console.WriteLine( newCar.isDriving );
newCar.stop();
Console.WriteLine( newCar.isDriving );
}
}
public class Car
{
public bool isDriving = false;
public Car( string make )
{
Make = make;
}
private string _make = string.Empty;
public string Make
{
get { return _make; }
set { _make = value; }
}
public void drive()
{
if( isDriving )
{
}
else
{
isDriving = true;
}
}
public void stop()
{
if( isDriving )
{
isDriving = false;
}
else
{
}
}
}