简体   繁体   English

VB.Net为什么需要显式声明我的字典才能使用select?

[英]VB.Net Why does my dictionary need to be explicitly declared in order to use select?

Using VB.Net with MVC5. 在MVC5中使用VB.Net。

I have a dictionary: 我有一本字典:

Dim Filter as New Dictionary(Of String, Object)

' the key's value is another dictionary
Filter.Add("customParams", New Dictionary(Of String, String) From {{"k1","v1"}, {"k2","v2"}, {"k3","v3"}})

and I am trying to convert the Filter("customParams") value, which is a Dictionary(Of String, String), to a delimited string, "k1=v1, k2=v2, k3=v3" 并且我正在尝试将Filter(“ customParams”)值(它是Dictionary(字符串,字符串))转换为定界字符串,“ k1 = v1,k2 = v2,k3 = v3”

This doesn't work: 这不起作用:

Dim kvString As String = String.Join(", ", Filter("customParams").Select(Function(x) x.Key & "=" + x.Value).ToArray())

I get this exception: 我得到这个例外:

Public member 'Select' on type 'Dictionary(Of String,String)' not found. 找不到类型为'Dictionary(Of String,String)'的公共成员'Select'。

But if I explicitly declare a new variable for the Dictionary(Of String,String) and use that instead of Filter("customParams") then this works: 但是,如果我为Dictionary(Of String,String)显式声明一个新变量,并使用它代替Filter(“ customParams”),则可以使用:

Dim customParams As Dictionary(Of String, String) = Filter("customParams")
Dim kvString As String = String.Join(", ", customParams.Select(Function(x) x.Key & "=" + x.Value).ToArray())

Why doesn't it work the other way? 为什么它不起作用呢? The exception itself says that it's working with a Dictionary(Of String, String) 异常本身说它正在使用Dictionary(Of String,String)

By default, VB.Net allows late binding ( OPTION STRICT OFF ). 默认情况下,VB.Net允许后期绑定OPTION STRICT OFF )。 Late binding is the equivalent of dynamic in C# and means the runtime looks up the method to call by name and the compiler does not type checking or validation. 后期绑定等效于C#中的dynamic ,这意味着运行时按名称查找要调用的方法,并且编译器不进行类型检查或验证。 Only public members can be accessed by late binding, so extension (friend) methods such as Select are not available. 后期绑定只能访问公共成员 ,因此扩展(朋友)方法(例如“ Select不可用。 This is automatic for object variables in VB.Net. 这对于VB.Net中的object变量是自动的。 customParams has a (non- object ) assigned type and thus uses early binding. customParams具有(非object )分配的类型,因此使用早期绑定。

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

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