CS1061 'IEnumerable<<anonymous type: x>>' 不包含转储的定义

CS1061 'IEnumerable<<anonymous type: x>>' does not contain a definition for dump

我只想从我的软件 KeePass 中获取密码。

在此处使用旧问题的代码后 Link to the question,我收到此错误消息:

S1061 'IEnumerable<<anonymous type: string Group, string Title, string Username, string Password>>' does not contain a definition for 'Dump' and no accessible extension method 'Dump' accepting a first argument of type 'IEnumerable<<anonymous type: string Group, string Title, string Username, string Password>>' could be found (are you missing a using directive or an assembly reference?) KeePasso C:\Users\prusinma\source\repos\KeePasso\KeePasso\Program.cs 36 Active

这是我使用的代码:

using System.Linq;
using KeePassLib;
using KeePassLib.Keys;
using KeePassLib.Serialization;

namespace KeePasso
{
    class Program
    {
        static void Main()
        {

            var dbpath = @"\xxx\Home_VIE\xxx\Desktop\KeePassDatabase\Database.kdbx";
            var keypath = @"\xxx\Home_VIE\xxx\Desktop\KeePassDatabase\Database.key";
            var masterpw = "1234abcd";

            var ioConnInfo = new IOConnectionInfo { Path = dbpath };
            var compKey = new CompositeKey();
            compKey.AddUserKey(new KcpPassword(masterpw));
            compKey.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromPath(keypath))); // Keyfile

            var db = new PwDatabase();
            db.Open(ioConnInfo, compKey, null);

            var kpdata = from entry in db.RootGroup.GetEntries(true)
                         select new
                         {
                             Group = entry.ParentGroup.Name,
                             Title = entry.Strings.ReadSafe("Title"),
                             Username = entry.Strings.ReadSafe("UserName"),
                             Password = entry.Strings.ReadSafe("Password"),
                         };

            kpdata.Dump(); // this is how Linqpad outputs stuff
            db.Close();
        }
    }
}

在代码的最后一行,Dump 处有红色下划线。它显示了我在上面分享的相同错误消息。

我已经在尝试寻找类似的问题,其中大部分都与类型有关。但正如我所见,标题、用户名和密码中的所有 datas/entries 都是字符串。

如果有人能帮助我,我将不胜感激。我也对如何从数据库中读出密码的其他解决方案持开放态度。

谢谢!

因为 kpdata 是匿名类型的集合,它覆盖了 ToString(并且如果 entry.Strings.ReadSafe returns string 或某些类型带有“正确”重写 ToString 方法)你可以在上面使用 Console.WriteLine:

Console.WriteLine(string.Join(Environment.NewLine, kpdata));; // instead of kpdata.Dump();

否则,您将需要to find a way将 LINQPad 的 Dump 方法导入您的项目,或者只使用一些 json 序列化库将对象转换为字符串。

将其转换为对象!

//But This works!
((object)d).Dump();

// as does this)
(d as Object).Dump()