作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这样的类型:
public class TypeValue
{
public Type Type { get; private set; }
public object Value { get; private set; }
}
所以我可以做类似的事情:
TypeValue tv = ...
int count = (tv.Type) tv.Value;
但是编译器给我这个错误:
The type or namespace name 'tv' could not be found (are you missing a using directive or an assembly reference?)
我如何实现这一目标?
最佳答案
没有。编译器如何在编译时确定“Type”指的是什么类型? “类型”对象与执行强制转换时使用的类型名称不同。例如,这也不起作用:
// "typeof" returns a "Type" object.
string foo = (typeof(string))SomeObj;
您将无法使用静态转换,但您可以偷偷摸摸地执行此操作 using reflection at runtime.
关于c# - 如何将对象值转换为类型值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975090/
我是一名优秀的程序员,十分优秀!