使用类型对象或字符串类型铸造

发布于 2025-02-13 05:49:20 字数 209 浏览 1 评论 0原文

我正在寻找一种使用字符串或类型对象将我的类类型转换为特定对象的方法。例如,请参阅我的下面简单代码段:

        int i = 10;
        Type t = i.GetType();
        string str = typeof(t) i;

它说t是一个无法用作类型的杂质。我的问题是,无论如何都可以做这件事吗?

I am looking for a way to convert my class type into a specific object using a string or Type object. For instance see my below simple code snippet:

        int i = 10;
        Type t = i.GetType();
        string str = typeof(t) i;

It saying that t is a varialbe which cannot used as a type. My question is that is there anyway to do this thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2025-02-20 05:49:21

这是演员和转换的示例:

using System;

public T CastObject<T>(object input)
{   
    return (T) input;   
}

public T ConvertObject<T>(object input) 
{
    return (T) Convert.ChangeType(input, typeof(T));
}

Here is an example of a cast and a convert:

using System;

public T CastObject<T>(object input)
{   
    return (T) input;   
}

public T ConvertObject<T>(object input) 
{
    return (T) Convert.ChangeType(input, typeof(T));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文