gpt4 book ai didi

c# - 如何将对象值转换为类型值?

转载 作者:行者123 更新时间:2023-11-30 14:02:54 24 4
gpt4 key购买 nike

我有这样的类型:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com