简体   繁体   中英

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.

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().

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 :

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

See this site

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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