Перечисление всех объектов класса - C#
Формулировка задачи:
Вечер добрый.
Как можно реализовать метод, возвращающий имена всех свойств класса?
Решение задачи: «Перечисление всех объектов класса»
textual
Листинг программы
class Program
{
static void Main(string[] args)
{
SomeObjectInherit2 b = new SomeObjectInherit2("ob1","Yellow");
b.ShowProps(b);
Console.ReadKey();
}
abstract class SomeObject : IProper
{
private string name;
public string Name
{
get { return name; }
set { name = value;}
}
public SomeObject()
{
Name = "object";
}
public SomeObject(string s)
{
Name = s;
}
public void ShowProps<T>(T a)
{
PropertyInfo[] Props = typeof (T).GetProperties();
foreach (var propname in Props)
{
Console.WriteLine(propname.Name + " is " + propname.GetValue(a));
}
}
}
class SomeObjectInherit : SomeObject
{
private double r;
public double R
{
get {return r; }
set {r = value;}
}
public SomeObjectInherit() : base() { }
public SomeObjectInherit(string s) : base(s) {}
public SomeObjectInherit(string s, double rad)
: base(s)
{
R = rad;
}
}
class SomeObjectInherit2 : SomeObject
{
private string color;
public string Color
{
get { return color; }
set { color = value; }
}
public SomeObjectInherit2() : base() { }
public SomeObjectInherit2(string s) : base(s) {}
public SomeObjectInherit2(string s, string c)
: base(s)
{
Color = c;
}
}
interface IProper
{
void ShowProps<T>(T a);
}
}