c# - 提供给 .NET 通用构造函数的类型参数,但仍然会导致错误,就好像没有

标签 c# asp.net-mvc razor

我的 ASP.NET MVC 4 Razor 应用程序中有这段看似没有问题的代码:

@{
    IDictionary htmlAttributes = new Dictionary<string, string>();
}

这会产生错误,

"Using the generic type 'System.Collections.Generic.IDictionary' requires 2 type arguments".

有人知道发生了什么事吗?

最佳答案

你还需要把它放在左边:

IDictionary<string, string> htmlAttributes = new Dictionary<string, string>();

请注意,错误消息是在谈论 IDictionary,而不是 Dictionary

或者,如果您确实想要非通用的 IDictionary接口(interface),确保其命名空间 (System.Collections) 在范围内。

关于c# - 提供给 .NET 通用构造函数的类型参数,但仍然会导致错误,就好像没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16359456/

相关文章:

c# - 从字符串反序列化时使用 XmlSerializer.CanDeserialize()

c# - Windows Azure HPC 调度程序

c# - 程序不包含适合入口点的静态 ‘Main’ 方法

layout - 布局页面在~/Views/Shared之外时,为什么Razor看不到ViewBag、Url.Content等?

c# - 如何在 C# 中向 mySql 数据库添加日期 (DateTime.now)

c# - 具有多个存储库的 ASP.NET MVC Controller ?

c# - ASP.Net MVC3 上的 HttpPost - "No parameterless constructor defined for this object"

asp.net-mvc - 如何使用 Jquery 更新强类型 Html.DropDownList

c# - ASP.NET MVC 中的一个 Web.config 而不是每个 Views 文件夹的多个

c# - 是否可以在执行代码之前加载服务器端 Blazor 页面 HTML?