I am creating a Stack<T>
where I have the type of the Stack
in a variable. After I have created the Stack<T>
, I am wanting to push items to the Stack
. How can I access the Push
method?
Here is my code:
var itemType = GetIEnumerableItemType(type);
var stackType = typeof(Stack<>);
var constructedStackType = stackType.MakeGenericType(itemType);
var instance = Activator.CreateInstance(constructedStackType);
EDIT
Here is some code that I have tried that works with a List<T>
:
var itemType = GetIEnumerableItemType(type);
var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(itemType);
var instance = (IList)Activator.CreateInstance(constructedListType);
//instance.Add("example item");
Thank you