如何创建类型[]的数组?

时间:2015-11-19 12:20:57

标签: c# types

如何在System.Type数据类型

中初始化数据类型数组

我正在尝试此示例代码

var previewFrame = document.getElementById(id);
var preview = previewFrame.contentDocument || previewFrame.contentWindow.document;
preview.open();
preview.write('<!doctype html>\n<audio autoplay><source src="/audio/test.mp3" type="audio/mp3"></audio>');
preview.close();

在我找到此链接中的解决方案后,我能够理解问题

Gettype

解决方案:  输入[] coltypes = {typeof(string),typeof(int),typeof(double),typeof(string)};

3 个答案:

答案 0 :(得分:5)

您需要使用typeof()获取类型的type object

Type[] coltypes = new Type[] { typeof(string), typeof(int), typeof(double) };

请注意,编译器会解释使用typeof表达式,并在编译时获取类型对象。

答案 1 :(得分:4)

因为string本身不是System.Type。您可以使用typeof operator生成System.Type

你可以使用;

Type[] coltypes = { typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string) };

答案 2 :(得分:0)

你错过了一件逗号,

Type[] coltypes = { typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string) };
相关问题