Как получить доступ к полям объекта, хранящегося в списке<T>
Я храню объекты, в списке<t>. В моих функциях RemovePoolObject и GetPoolObject для работы. Но я не могу получить доступ к полям TestClass.
#region ObjectPooler public class PooledObject<T> { public Func<T> ObjectInitializer { get; } public PooledObject(Func<T> objectInitializer) { ObjectInitializer = objectInitializer ?? throw new ArgumentNullException(nameof(objectInitializer)); } } public enum PoolAddMethod { None, SingleObject //Initialize Single Object } public class ObjectPooler<T> { public List<T> _deepEndObjects { get; set; } private PooledObject<T> _deepEndObject; public PoolAddMethod AddObjectMethod { get; set; } public ObjectPooler(PooledObject<T> pooledObject, PoolAddMethod addObjectMethod) { _deepEndObject = pooledObject ?? throw new ArgumentNullException(nameof(pooledObject)); AddObjectMethod = addObjectMethod; _deepEndObjects = new List<T>(); } public void AddPoolObject() { _deepEndObjects.Add(_deepEndObject.ObjectInitializer.Invoke()); } public void EmptyPool() { _deepEndObjects.Clear(); } public T GetPoolObject(string gGet = "") { //Return a object at given idx or in this case a given GUID //return _deepEndObjects.Find(x => x.g.ToString() == gGet); return default(T); } public void RemovePoolObject(string gRemove = "") { //Remove a object at given idx or in this case a given GUID //_deepEndObjects.Remove(_deepEndObjects.Find(x => x.g.ToString() == gRemove)); } } #endregion public class TestClass { public Guid g = Guid.NewGuid(); //Just for testing. Real class would like an unique ID } PooledObject<TestClass> _pooledObject; ObjectPooler<TestClass> objectPooler; public Form1() { InitializeComponent(); _pooledObject = new PooledObject<TestClass>(() => ObjectAlloc<TestClass>.New()); objectPooler = new ObjectPooler<TestClass>(_pooledObject, PoolAddMethod.SingleObject); } private void genbutton_Click(object sender, EventArgs e) { int iTag = Convert.ToInt32((sender as Control).Tag); switch (iTag) { case 0: objectPooler.AddPoolObject(); break; case 1: if (listView1.SelectedItems.Count > 0) { objectPooler.RemovePoolObject(); objectPooler._deepEndObjects.Remove(objectPooler._deepEndObjects.Find(x => x.g.ToString() == listView1.SelectedItems[0].Text)); } break; case 2: objectPooler.EmptyPool(); break; default: break; } listView1.Clear(); objectPooler._deepEndObjects.ForEach(x => listView1.Items.Add($"{x.g.ToString()}")); }
Что я уже пробовал:
_deepEndObjects.Remove(_deepEndObjects.Find(x => x.g.ToString() == gRemove));