在运行时转换为特定类型 [英] Cast to a Specific Type at Runtime

查看:32
本文介绍了在运行时转换为特定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时强制转换为特定类型.

I need to cast to a specific Type at Runtime.

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

How it works, and looks like - now:

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

作为字符串的客户,我需要 GetProperty(),不是这样写的,直接,它来自一个总是变化的变量.我只是写了客户",以供理解.

The Customer as String, where I need for GetProperty(), isn't written like that directly, it comes from a Variable, that changes always. I just wrote "Customer", for comprehension purposes.

我现在想要的是一样的,但没有说明,它是一个客户或任何类型.

What I want now is the same, but without telling, that its a Customer or any Type.

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

因为它也可能是这样的:

Cause it could also be something like that:

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

我尝试了诸如 businnesObject = Activator.CreateInstance(type);

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

Hope someone has a small solution for this

推荐答案

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

Customer and Order both inherit from System.Object, so you can just use

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

这篇关于在运行时转换为特定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆