c# - 如何转换为给定类型?

标签 c# reflection casting

我正在尝试弄清楚如何按照以下方式做某事

Type t = Type.GetType("fully qualified type name");
dynamic obj = foo as t

我该怎么做?我正在查看 Convert.ChangeType(),但它只返回一个对象,这不是我想要的。

最佳答案

看来你是混科,我不明白为什么。要么使用反射,要么使用动态类型,但两者都用对我来说意义不大。

如果你正在用你控制的类型做某事,让它们实现一个接口(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); 
}

如果您正在使用您无法控制的类型,请使用 dynamic - 无需强制转换:

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); 
}

我可以看到强制转换是必要的唯一情况是 COM 互操作作为 QueryInterface 的替代品或者如果该类型实现了一个返回新对象的转换。


关于您的评论:

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

静态类型指定您可以在代码中访问的最受限制类型。例如,您有一个变量 t输入为 System.Type在声明中 Type t = Type.GetType("foo"); ,但在运行时,GetType将返回 System.RuntimeType 类型的值.自 System.RuntimeType继承自 System.Type , 这不是问题。

同样,一切*都继承自System.Object并且可以存储在类型为 object 的变量中.一个例子可能会有帮助:

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 是 Reflection 的语法糖,因此不作为单独的类型存在。所以:

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"

等同于:

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);

C# 仅在编译时生成所有这些代码**。

*:嗯……most everything
<子>**:嗯……not exactly this code

关于c# - 如何转换为给定类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27067943/

相关文章:

c++ - 当我们进行向下转型时内部会发生什么?

c# - 如何更新具有 2 个不同 ID 的特定行?

c# - 如何计算上传文件的百分比?

c# - 更新了 web api 的验证示例

java - ReflectionTestUtils 设置字段, Spring 值注释返回 NPE

C# 浮点表达式 : strange behavior when casting the result float to int

c - 类型转换能持续多久? (C)

c# - 奇怪的控制台 MoveBufferArea IOException

c# - 在所有程序集中查找类型

java - 将 Java 代码转换为 Eclipse 和 Javac AST 注入(inject)语句