C# Activator.GetInstance instances don't retain state when created as Interfaces

176 views Asked by At

I am using reflection to find all instances of ICollector and instantiate each using this code block

var instances = from t in Assembly.GetExecutingAssembly().GetTypes()
                where t.GetInterfaces().Contains(typeof(ICollector))
                      && t.GetConstructor(Type.EmptyTypes) != null
                select Activator.CreateInstance(t) as ICollector;

The instances are created just fine, but when I call the method Start() on each instance (which is part of the interface), then properties set inside the method lose their value once the method is exited.

For example an instance of ICollector is NetworkCollector and it has this code in the Start() method

instanceNames = networkCategory.GetInstanceNames().ToList();
currentActivity = new List<CounterValues>();
counters = new List<PerformanceCounter>();

And those variables are also populated with actual values. When I subsequently call those variables in the Collect() method (also part of the interface ICollector) they are all null.

Am I missing something, or are these instances losing state?

0

There are 0 answers