如何从字符串“ string []”中获取.NET数组类型?

编程入门 行业动态 更新时间:2024-10-21 19:09:53
本文介绍了如何从字符串“ string []”中获取.NET数组类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给出字符串 string [] ,并要求获取该类的基础类型,可能以以下内容开头:

Given the string "string[]" and asked to get the underlying Type for this class, one might start with:

private Type getTypeByName(string typeName) { if (typeName.EndsWith("[]")) { return something; // But what? } return Type.GetType(typeName); }

string []是什么类型,如何反映出该类型

What type is "string[]" and how does one reflect the type out of it?

显然有一个 System.String 类型和一个 System.Array 类型,但我看不到它们如何像在 Nullable< T> 及其T与 MakeGenericType 方法。

Obviously there's a System.String type and a System.Array type, but I can't see how they can be reflected "together" as you would normally do for Nullable<T> and its T with the MakeGenericType method.

任何帮助打破我陷入困境的思路的人,将不胜感激!

Any help to break the mind-loop I've gotten myself into will be greatly appreciated!

推荐答案

究竟是什么问题? Type.GetType 可以正常工作:

What exactly is your problem? Type.GetType works fine:

Console.WriteLine(typeof(string[])); var type = Type.GetType("System.String[]"); Console.WriteLine(type);

打印:

System.String[] System.String[]

这按预期工作。

更多推荐

如何从字符串“ string []”中获取.NET数组类型?

本文发布于:2023-11-08 13:50:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   类型   NET   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!