I try use System.Reflection to create new Instances of some clases. First of all ,will be find the classes using some filters like "Where and EndWith", when all the classe are load, then create a list. For each type into the list make the instance of the class using
"viewModelType.Assembly.CreateInstance(viewModelType.FullName)".
GetType().Assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => type.Name.EndsWith("ViewModel"))
.ToList()
.ForEach(viewModelType =>
ViewContainer.AddComponent(viewModelType.Assembly.CreateInstance(viewModelType.FullName)));
so at the last line the plan is to set the instances into a Dictinary using the Static Method ViewContainer.AddComponent(T).
here the code snipe for these Class and his Method:
private Dictionary<Type, object> Component { get; } = new Dictionary<Type, object>();
/// <summary>
/// Add Component
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="componentName"></param>
public void AddComponent<T>(T componentName)
{
Component[typeof(T)] = componentName;
}
So the Reflexion logic works well i meant the classes that i search are found, exactly the are 3, my problem it's that wen i want to add the instances using "AddComponent" will be just add one of then , may be cuz Createinstance(..) returns object? and the dictionary dont accept more than once?, in this case can some say me how i can add correctly these 3 elements into the dictionary?.
I know that the Method "AddComponent" works well , for example when we use the folling call
ViewContainer.AddComponent(New SomeClassName)
Thanks for advance
(Project use .net Framework 4.5)
Best Regards Javanto
Aucun commentaire:
Enregistrer un commentaire