简体   繁体   中英

C# Cannot implicitly convert type 'T' to 'object[*,*]'

I have a method that I'm attempting to update to accept multiple types of data so I used a generic. I got it working with a couple different types, but one type that is being passed in is object[,] . When I try to set the data to that type, I get the following error on conversion:

Cannot implicitly convert type 'T' to 'object[*,*]'

The code is pretty simple with:

object[,] data = (T)(object[,])element;

Any ideas?

The issue you're seeing is that since T neither derives from object[,] nor implements an explicit conversion , the code has literally no idea how to perform the conversion. This is because under the hood, conversions either box or unbox derived types, or call the conversion method in order to return the desired output type.

Since you're using a Generic, you can constrain the type using a where statement. As long as the constrained type (or a more derived type) defines and explicit conversion to (or derives from) object[,] , you'll be able to perform the conversion.

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