I have a factory defined like so :
public IPopulator CreatePopulator<T>(ReportItem Item) where T : IReportElement
{
if (typeof(T) == typeof(BarChartElement))
{
return BarChartPopulator.Create(Item);
}
else
{
throw new NotSupportedException(string.Format("Type: {0} is not suppported by {1}", typeof(T).Name, this.GetType().Name));
}
}
In the class that calls this method I have a variable like this:
IReportElement MyElement { get; set; }
Assuming MyElement is instantiated to a type that implements IReportElement, How can I call my factory using this variable ?
I have tried
Type VariableType = MyChartElement.GetType();
PopulatorFactory.CreatePopulator<VariableType>(new Chart());
And
PopulatorFactory.CreatePopulator<MyVariable.GetType()>(new Chart());
I could write a switch statement but I feel like there should be some way for me to pass my type. Is this possible ?