It seems like you are mixing disciplines, and I don't understand why. Either use reflection or dynamic typing, but using both makes little sense to me.
If you are doing something with types you control, have them implement an interface:
interface IModule
{
string Name { get; }
}
class Module1 : IModule
{
public string Name { get { return "Module 1"; } }
}
public void PrintModuleName(string moduleType)
{
Type tModule = Type.GetType("MyApp.Module1");
IModule module = (IModule)Activator.CreateInstance(tModule);
Console.WriteLine(module.Name);
}
If you are working with types you don't control, use dynamic
- no cast needed:
class Module1
{
public string Name { get { return "Module 1"; } }
}
public void PrintModuleName(string moduleType)
{
Type tModule = Type.GetType("MyApp.Module1");
dynamic module = Activator.CreateInstance(tModule);
Console.WriteLine(module.Name);
}
The only case where I could see the cast being necessary is for COM interop as a substitute for QueryInterface
or if the type implements a cast which returns a new object.
Regarding your comment:
Since Convert.ChangeType()
returns a type of object, wouldn't obj
still
resolve to a type of object during runtime? How does this differ from
object obj = Convert.ChangeType(foo,t);
? To my understanding, the
dynamic
keyword still uses the same type
Static typing specifies the most restricted type you are allowed to access in code. For example, you have a variable t
typed as System.Type
in the statement Type t = Type.GetType("foo");
, but at runtime, GetType
will return a value of type System.RuntimeType
. Since System.RuntimeType
inherits from System.Type
, it is not a problem.
Similarly, everything* inherits from System.Object
and can be stored in a variable typed as object
. An example may be helpful:
System.Type t = Type.GetType("System.String");
Console.WriteLine("Runtime type of `t`:\t{0}", t.GetType().FullName);
// Prints "Runtime type of `t` is: System.RuntimeType"
System.String s = "foo";
Console.WriteLine("Runtime type of `s`:\t{0}", s.GetType().FullName);
// Prints "Runtime type of `s` is: System.String"
object o = s;
Console.WriteLine("Runtime type of `o`:\t{0}", o.GetType().FullName);
// Prints "Runtime type of `o` is: System.String"
Dynamic is syntactic sugar for Reflection, and therefore does not exist as a separate type. So:
string s = "foo";
Console.WriteLine("Runtime type of `s`:\t{0}", s.GetType().FullName);
// Prints "Runtime type of `s` is: System.String"
dynamic d = s;
Console.WriteLine("Runtime type of `d:\t{0}", d.GetType().FullName);
// Also prints "Runtime type of `d` is: System.String"
Is the same as:
string s = "foo";
Console.WriteLine("Runtime type of `s`:\t{0}", s.GetType().FullName);
Type typeOfS = s.GetType();
object resultOfGetType = typeOfS.GetMethod("GetType").Invoke(s, null);
Type typeOfResultOfGetType = resultOfGetType.GetType();
object resultOfFullName = typeOfResultOfGetType.GetProperty("FullName").GetValue(resultOfGetType);
Console.WriteLine("Runtime type of `d:\t{0}", resultOfFullName);
The C# just generates all of this code** at compile-time.
*: well... most everything
**: well... not exactly this code