I dont' now how can I get a instance of a parameter for Invoke (please see the sample variable "listInstance"). I don't want to create a new list (with Activator.CreateInstance), I want to add an object to the existing list instance. How can I get the object Sample.Samples?
using System.Collections.Generic;
public class Class<T>
{
public readonly IList<T> InternalList = new List<T>();
public virtual void Add(T obj)
{
InternalList.Add(obj);
}
}
public class Sample
{
public Class<Sample> Samples { get; set; } = new Class<Sample>();
}
class Program
{
static void Main(string[] args)
{
var cla = new Sample();
var propertyInfo = cla.GetType().GetProperty("Samples");
var newSample = new Sample();
var addMethod = propertyInfo.PropertyType.GetMethod("Add");
var listInstance = ???; // Instance of the Property Sample.Samples
addMethod.Invoke(listInstance, new[] { newSample });
}
}