c# - 在运行时转换为特定类型

标签 c# types casting

我需要在运行时转换为特定类型。

它是如何工作的,现在看起来像:

Customer test = (Customer)entityBase.GetType().GetProperty("Customer").GetValue(entityBase, null);

我需要 GetProperty() 的 Customer as String 不是这样写的直接,它来自一个总是变化的变量。为了便于理解,我只写了“客户”。

我现在想要的是相同的,但没有说明,它是客户还是任何类型。

Placeholder test = (Placeholder)entityBase.GetType().GetProperty("That Changes, thats fine").GetValue(entityBase, null);

因为它也可能是这样的:

Order test = (Order)entityBase.GetType().GetProperty("That Changes, thats fine").GetValue(entityBase, null);

我试过类似businnesObject = Activator.CreateInstance(type);

希望有人对此有一个小的解决方案

最佳答案

CustomerOrder 都继承自 System.Object,所以你可以直接使用

Object test = entityBase.GetType().GetProperty("That Changes, thats fine").GetValue(entityBase, null);

关于c# - 在运行时转换为特定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9884896/

相关文章:

c# - 如何找到所有实现 IDisposable 的类?

C# 使用存储在数据库中的方法名称调用方法

python - int ('123' ) in python - 它是 'int' 类的函数调用还是构造函数调用?

C++ 编译错误枚举 "does not name a type"

C# 导出到 excel 用单元格中的换行符替换 <br/>

c# - LINQ 中的 If Else

typescript - 如何从 typescript 中的类型中排除 getter only 属性

c# - json.NET 反序列化 2D 数组时抛出 InvalidCastException

c# - 在 C# 中将委托(delegate)转换为通用委托(delegate)

C++ : Seeding mt的低32位包含什么