简体   繁体   English

C# 将类型存储在数组中

[英]C# Store type in an array

I want to store an error in an array, but when I try this it tells me我想在数组中存储一个错误,但是当我尝试这个时它告诉我

System.IO.DirectoryNotFoundException is a type, which is not valid in the given context. System.IO.DirectoryNotFoundException 是一种类型,在给定的上下文中无效。

Type[] errorArray = new Type[] { System.IO.DirectoryNotFoundException };

Am I doing something wrong, or is it not possible this way?我做错了什么,还是不可能这样?

What you want to do is use typeof().你想要做的是使用 typeof()。

Type[] errorArray = new Type[] { typeof(System.IO.DirectoryNotFoundException) };

This way you store the type of your exception.这样您就可以存储异常的类型。

It seems you want to store the types of the exceptions in an array, not the exception itself, so you need to use typeof :似乎您想将异常的类型存储在数组中,而不是异常本身,因此您需要使用typeof

Type[] errorArray = new Type[] { typeof (System.IO.DirectoryNotFoundException) }

See this site看到这个网站

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM