【问题标题】:CS1061 'IEnumerable<<anonymous type: x>>' does not contain a definition for dumpCS1061 'IEnumerable<<anonymous type: x>>' 不包含转储的定义
【发布时间】:2021-08-17 10:09:38
【问题描述】:

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

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

S1061 'IEnumerable&lt;&lt;anonymous type: string Group, string Title, string Username, string Password&gt;&gt;' does not contain a definition for 'Dump' and no accessible extension method 'Dump' accepting a first argument of type 'IEnumerable&lt;&lt;anonymous type: string Group, string Title, string Username, string Password&gt;&gt;' 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 处有一条红色下划线。它显示了我在上面分享的相同错误消息。

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

如果有人可以帮助我,将不胜感激。 Id 也开放其他解决方案如何从数据库中读出密码。

谢谢!

【问题讨论】:

  • 要清楚 - 你真的想在 Linqpad 中运行这段代码吗?正如注释所示,该行正在使用 Linqpad 功能。
  • 这里的明确提示是this is how Linqpad outputs stuff
  • 谢谢我看看。

标签: c# linq keepass


【解决方案1】:

由于kpdata 是匿名类型的集合,它已覆盖ToString(如果entry.Strings.ReadSafe 返回string 或某些类型“正确”覆盖ToString 方法)你可以只使用Console.WriteLine on它:

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

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

【讨论】:

  • 谢谢。我已经尝试使用 WriteLine 简单地输出条目。但我的输出看起来不正确System.Linq.Enumerable+SelectEnumerableIterator2[KeePassLib.PwEntry,f__AnonymousType04[System.String,System.String,System.String,System.String]]
  • @Beardy,对不起,错过了收藏。比你可以使用类似Console.WriteLine(string.Join(Environment.NewLine, kpdata));
  • 哇哦。它确实有效!我从没想过有人能这么快给出解决方案!你能给我举个例子吗,如何只获取密码并将其存储在变量中?
  • @Beardy - 你有一个集合,所以理论上可以有多个密码 - 在这种情况下,你需要哪一个?
  • 集合/数据库有 3 行,分别称为标题、用户名和密码。假设标题名称是 Azkaban。然后我想从 Title Azkaban 获取密码并将其存储在变量中
【解决方案2】:

将其转换为对象!

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

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

【讨论】:

    猜你喜欢
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2023-03-20
    相关资源
    最近更新 更多