Как же всё-таки сделать копию Liss, где Т - ссылочный тип (в моем случае -- класс) - C#
Формулировка задачи:
class T
{
int d;
DateTime f;
public T(int d, DateTIme date = new DateTime()) { d = d; f = date;}
}
//где-то в Main или в другом классе
List<T> list = new List<T>();
//терь тут нужно сделать полную копию Решение задачи: «Как же всё-таки сделать копию Liss, где Т - ссылочный тип (в моем случае -- класс)»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
internal class Program
{
private static void Main(string[] args)
{
var list = new List<T> { new T(3) };
List<T> list2 = list.ToList<T>();
var type = typeof(T);
var d = type.GetField("d", BindingFlags.NonPublic | BindingFlags.Instance);
d.SetValue(list2.First(),5);
Console.WriteLine(d.GetValue(list.First()));
Console.WriteLine(d.GetValue(list2.First()));
}
}
class T
{
int d;
DateTime f;
public T(int d, DateTime date = new DateTime())
{
this.d = d;
f = date;
}
}