I have next LINQ query to read App.config to instantiate objects using fully qualified types stated inside:
var strategies = from strategy in section.Strategies
let indicators = (
from indicator in strategy.Indicators
select (IIndicatorReader)Activator.CreateInstance(Type.GetType(indicator.Type), bot.Mt, section.Symbol))
let orders = (
from order in strategy.Orders
select new OrderInfo(order.Id, order.Operation.Value, order.Amount))
select (IStrategy)Activator.CreateInstance(Type.GetType(strategy.Type), section.Symbol, strategy.Amount, strategy.Limit, indicators, orders);
So each time inside the strategy I call
indicatorList.Select(i => i.Operation)
this instantiation occurs:
(IIndicatorReader)Activator.CreateInstance(Type.GetType(indicator.Type), bot.Mt, section.Symbol))
and appropriate class's constructor is called.
But an indicator stated in App.config first is instantiated twice, all other - once. How can it be?? I will be happy to provide any additional information required.
My indicator collection:
public class IndicatorElementCollection : ConfigurationElementCollection, IEnumerable<IndicatorElement>
{
...
public new IEnumerator<IndicatorElement> GetEnumerator()
{
return this.OfType<IndicatorElement>().GetEnumerator();
}
}
Implementation of GetEnumerator()
conversion from non-generic to generic is taken from this question on SO.
Another implementation:
foreach (OrderElement element in (System.Collections.IEnumerable)this)
{
yield return element;
}
works in the same manner.
The LINQ expression indicators
will be re-evaluated every time you call GetEnumerator
. You need to force a single evaluation by invoking ToList
or ToArray
. This could of course result in memory space issues if you are expecting many indicators.