简体   繁体   中英

Get Type of Collection using Roslyn

Using Roslyn for VB.Net I can get the Type of an Expression using the code below.

Dim ExpressionType As TypeInfo = SemanticModel.GetTypeInfo(ForEachStatement.Expression)

If the expression is a collection (List, Dictionary, Array, Collection...) how can I find out what is in the collection? For the example below I want to find DocumentIdAndRoot

Dim docs As List(Of DocumentIdAndRoot) = Await RemoveParameterAsync(document, parameter, root, cancellationToken)

如果您的问题特定于foreach ,那么您应该使用SemanticModel.GetForEachStatementInfo() ,它返回包含所有必要信息的ForEachStatementInfo

What you could do is take the TypeInfo you get, and look at the ImplementedInterfaces property. One of those would be IEnumerable or ICollection, and from there you could look at what the generic parameter is.

If you do have a ForEach involved somewhere, you're still better off using Tamas' approach, since that will correctly implement the language rules there.

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