jeudi 10 mars 2016

Get all instances of a class

is there a way to get all instances of an object in c# with reflections? For example, lets say we have an object Point.

Point p1 = new Point(5,5);
Point p2 = new point(2,2);

Is there a way to get all instances of this with reflection? I have seen something like this but it doesn't work for me.

return (from t in Assembly.GetExecutingAssembly().GetTypes()
                    where t.BaseType == (typeof(T)) && t.GetConstructor(Type.EmptyTypes) != null
                    select (T)Activator.CreateInstance(t)).ToList();

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire