首页 文章

使用“dynamic”类型会产生编译错误

提问于
浏览
-2

我想创建一个匿名对象列表 . 所以我这样做了:

var problematicAddresses = new HashSet<dynamic>();
foreach (DataRow row in dtMeters.Rows)
{
    var billingAddress = new
    {
        address = row["BillingAddress"].ToString(),
        city = row["BillingCity"].ToString(),
        state = row["BillingState"].ToString(),
        zip = row["BillingZip"].ToString()
    };    

    // ... fill problematicAddresses without any problem

    foreach (var completeAddress in problematicAddresses)
    {
        string addr = completeAddress.address;

        // ...
    }

    // ...
}

这里的最后一行没有编译,并给我以下错误:

无法找到编译动态表达式所需的一种或多种类型 . 您是否缺少对Microsoft.CSharp.dll和System.Core.dll的引用?

你能帮帮我吗?

谢谢 .

1 回答

  • 0

    确保包含这些库...另外,在最后一行使用var而不是string

相关问题