Перебрать переменные в классе - C#
Формулировка задачи:
Добрый день.
Помогите пожалуйста, прочитать все переменные записанные в List
В строке 46 нужно получить все значения из l - если это возможно
С уважением, Александр.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Test
{
public class List0
{
public string s0 { get; set; }
public int i0 { get; set; }
public List0(string s0, int i0)
{
this.s0 = s0;
this.i0 = i0;
}
}
public class List1
{
public string s1 { get; set; }
public int i1 { get; set; }
public double d1 { get; set; }
public List1(string s1, int i1, double d1)
{
this.s1 = s1;
this.i1 = i1;
this.d1 = d1;
}
}
public class A<T>
{
List<T> _list;
public A(List<T> obj)
{
_list = obj;
}
public void ListX()
{
foreach (var l in (IEnumerable)_list)
{
var x0 = l;
Console.WriteLine("!!: " + x0);
}
}
}
class Program
{
static readonly List<List0> _lists0 = new List<List0>();
static readonly List<List1> _lists1 = new List<List1>();
static void Main(string[] args)
{
_lists0.Add(new List0("s0", 0));
_lists0.Add(new List0("s1", 1));
_lists1.Add(new List1("s20", 2, 5.5));
_lists1.Add(new List1("s30", 3, 6.6));
A<List0> _b0 = new A<List0>(_lists0);
_b0.ListX();
A<List1> _b1 = new A<List1>(_lists1);
_b1.ListX();
}
}
}Решение задачи: «Перебрать переменные в классе»
textual
Листинг программы
using System;
using System.Collections;
using System.Collections.Generic;
namespace Test
{
public interface IMyInterface
{
string GetInmortantValues();
}
public class List0 : IMyInterface
{
public string s0 { get; set; }
public int i0 { get; set; }
public List0(string s0, int i0)
{
this.s0 = s0;
this.i0 = i0;
}
public string GetInmortantValues()
{
return String.Format("s0: {0}, io: {1}", s0, i0);
}
}
public class List1 : IMyInterface
{
public string s1 { get; set; }
public int i1 { get; set; }
public double d1 { get; set; }
public List1(string s1, int i1, double d1)
{
this.s1 = s1;
this.i1 = i1;
this.d1 = d1;
}
public string GetInmortantValues()
{
return String.Format("s1: {0}, i1: {1}, d1: {2}", s1, i1, d1);
}
}
public class A<T> where T: IMyInterface
{
List<T> _list;
public A(List<T> obj)
{
_list = obj;
}
public void ListX()
{
int n = 0;
Console.WriteLine("Current list:");
foreach (T l in _list)
{
Console.WriteLine("List[{0}] = {{{1}}}", n++, l.GetInmortantValues());
}
}
}
class Program
{
static readonly List<List0> _lists0 = new List<List0>();
static readonly List<List1> _lists1 = new List<List1>();
static void Main(string[] args)
{
_lists0.Add(new List0("s0", 0));
_lists0.Add(new List0("s1", 1));
_lists1.Add(new List1("s20", 2, 5.5));
_lists1.Add(new List1("s30", 3, 6.6));
A<List0> _b0 = new A<List0>(_lists0);
_b0.ListX();
A<List1> _b1 = new A<List1>(_lists1);
_b1.ListX();
Console.ReadLine();
}
}
}