As the comments say, using a StackFrame is a bit more complicated and error prone. Also, I'm not sure if you can get information about the private type of the generic type.
But you could follow a different approach when you maintain List<Type> which are already processed. Below are two versions of the CreateRepository method that I assume is a method that you can use to create element repositories.
private static List<Type> addedItemList; has the info of all the created types so far.
Version - 1
public static Repository<T> CreateRepository(T item) { if (addedItemList.Contains<Type>(item.GetType())) { return new Repository<T> { Item = item }; } addedItemList.Add(item.GetType()); return CreateRepository(item); }
Version 2
public static Repository<T> CreateRepository() { if (addedItemList.Contains<Type>(typeof(T))) { return new Repository<T> { Item = default(T) }; } addedItemList.Add(typeof(T)); return CreateRepository(); }
source share