I'm wondering if it's possible to cast an object to a Type... I've just started using Reflection, so maybe I'm doing it all wrong but here's what I would like to do:
...
Type type = ...;
Type interfaceType = someOtherType.GetInterface("IConverter`2");
return (Cast to interfaceType)Activator.CreateInstance(type);
Is the cast to the interface possible?
Update:
Compiler says that T and K can not be found. The myInterface Type instance knows the T and K class...
public IConverter<T, K> GetConverter(Type type)
{
if (dtoModelDictionary.ContainsKey(type))
{
Type foundType = dtoModelDictionary[type];
Type myInterface = foundType.GetInterface("IConverter`2");
return (IConverter<T, K>)Activator.CreateInstance(foundType);
}
else if (dalModelDictionary.ContainsKey(type))
{
Type foundType = dalModelDictionary[type];
return (IConverter<T, K>)Activator.CreateInstance(foundType);
}
else
{
throw new System.Exception();
}
}
Second update:
public SomeClass GetConverter(Type type)
{
if (dtoModelDictionary.ContainsKey(type))
{
Type foundType = dtoModelDictionary[type];
Type myInterface = foundType.GetInterface("IConverter`2");
IConverter<T, K> converter = (IConverter<T, K>)Activator.CreateInstance(foundType);
return converter.someMethod();
}
}