我需要在运行时转换为特定类型。
它是如何工作的,现在看起来像:
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);
希望有人对此有一个小的解决方案
最佳答案
Customer
和 Order
都继承自 System.Object
,所以你可以直接使用
Object test = entityBase.GetType().GetProperty("That Changes, thats fine").GetValue(entityBase, null);
关于c# - 在运行时转换为特定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9884896/