Tag: recursion

递归搜索嵌套列表

我已经阅读并搜索了,我还没有想出这个相对简单的问题的答案。 我上课了: public class AccessibleTreeItem { public string name; public List children; public AccessibleTreeItem() { children = new List(); } } 这是使用一系列在这种情况下并不重要的函数填充的,但我正在寻找的是一种搜索列表中所有子项,搜索特定“名称”值的方法,以及如果找到,返回该列表。 如何以最简单的方式实现这一目标,性能最低? 谢谢 – 我已经被困在这几天了……

如何为给定字符串键入集合

可能重复: 如何获得子集的所有可能组合? 我正在尝试为给定字符串键入集合,例如“123”会给{1} {2} {3} {13} {23} {12} {123} {}但我的代码会给我1 1请任何人告诉我为什么,请帮助我修复它谢谢大家 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestAAD { class Program { static List sets = new List(); static int len = 0; private static void Generte_Sets(string str, int i) { sets.Add(str[i].ToString()); if (i < len) Generte_Sets(str, i + 1); else { […]

为什么这个函数什么都没有返回,虽然有匹配?

我称之为(Compact Framework – 此代码在Windows CE手持设备上运行)方法: public static List GetXMLFiles(string fileType, string startingDir) { const string EXTENSION = “.XML”; string dirName = startingDir; var fileNames = new List(); try { foreach (string f in Directory.GetFiles(dirName)) { string ext = Path.GetExtension(f).ToUpper(); string fileNameOnly = Path.GetFileNameWithoutExtension(f); if ((ext.Equals(EXTENSION, StringComparison.OrdinalIgnoreCase)) && (fileNameOnly.Contains(fileType))) { fileNames.Add(f); } } foreach (string d […]

异步递归。 我的记忆在哪里?

这更多地是出于好奇而不是任何现实世界的问题。 请考虑以下代码: void Main() { FAsync().Wait(); } async Task FAsync() { await Task.Yield(); await FAsync(); } 在同步世界中,这最终会导致堆栈溢出。 在异步世界中,这只会消耗大量内存(我认为这与我可能松散地称之为“异步堆栈”的内容有关?) 这些数据到底是什么,以及如何保存?

C#带递归的reflection

我正在研究Reflection,但是我在执行递归时遇到困难。 代码: public class User { public string Name; public int Number; public Address Address; } public class Address { public string Street; public string State; public string Country; } 现在我打印的是价值观。 Type t = user.GetType(); PropertyInfo[] props = t.GetProperties(); foreach (PropertyInfo prp in props) { if(!prp.GetType().IsPrimitive && prp.GetType().IsClass) { // Get the values of the […]

C#使用FTP上传整个目录

我想要做的是在C#(C Sharp)中使用FTP上传网站。 所以我需要上传文件夹中的所有文件和文件夹,保持其结构。 我正在使用这个FTP类: http : //www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class用于实际上传。 我得出结论,我需要编写一个递归方法,遍历主目录的每个子目录并上传其中的所有文件和文件夹。 这应该将我的文件夹的精确副本复制到FTP。 问题是……我不知道怎么写这样的方法。 我以前写过递归方法,但我是FTP部分的新手。 这是我到目前为止: private void recursiveDirectory(string directoryPath) { string[] filePaths = null; string[] subDirectories = null; filePaths = Directory.GetFiles(directoryPath, “*.*”); subDirectories = Directory.GetDirectories(directoryPath); if (filePaths != null && subDirectories != null) { foreach (string directory in subDirectories) { ftpClient.createDirectory(directory); } foreach (string file in filePaths) { […]

递归和等待/异步关键字

我对await关键字的工作方式有一个脆弱的把握,我想稍微扩展一下我对它的理解。 让我头晕目眩的问题是使用递归。 这是一个例子: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestingAwaitOverflow { class Program { static void Main(string[] args) { var task = TestAsync(0); System.Threading.Thread.Sleep(100000); } static async Task TestAsync(int count) { Console.WriteLine(count); await TestAsync(count + 1); } } } 这个显然抛出了StackOverflowException 。 我的理解是因为代码实际上是同步运行的,直到第一个异步操作,之后它返回一个包含异步操作信息的Task对象。 在这种情况下,没有异步操作,因此它只是在错误的承诺下继续递归,它最终会返回一个Task 。 现在改变它只是一点点: using System; using System.Collections.Generic; using […]

protobuf-net:检测到可能的递归

我尝试序列化对象图(不是很深)时遇到exception。 有意义的部分是这样的: [错误]致命的未处理exception:ProtoBuf.ProtoException:可能的递归检测(偏移量:5级):ProtoBuf.ProtoWriter.CheckRecursionStackAndPush(object)处的ProtoBuf.ProtoWriter.StartSubItem(对象,ProtoBuf.ProtoWriter) ,bool) 该图表示文件/目录结构,我的模型(简化)如下所示: [ProtoContract] [ProtoInclude(100, typeof(PackageDirectory))] [ProtoInclude(200, typeof(PackageFile))] public abstract class PackageMember { [ProtoMember(1)] public virtual string Name { get; protected set; } [ProtoMember(2, AsReference=true)] public PackageDirectory ParentDirectory { get; protected set; } } [ProtoContract] public class PackageDirectory : PackageMember { [ProtoMember(3)] private Dictionary _children; public PackageDirectory() { _children = new Dictionary(); } […]

在C#中模拟CTE递归

假设有以下CTE返回我所拥有的某些树数据(邻接模型)的级别(取自Linq中的分层数据 – 选项和性能 ): WITH hierarchy_cte(id, parent_id, data, lvl) AS ( SELECT id, parent_id, data, 0 AS lvl FROM dbo.hierarchical_table WHERE (parent_id IS NULL) UNION ALL SELECT t1.id, t1.parent_id, t1.data, h.lvl + 1 AS lvl FROM dbo.hierarchical_table AS t1 INNER JOIN hierarchy_cte AS h ON t1.parent_id = h.id ) SELECT id, parent_id, data, lvl FROM […]

如何使用json.net进行json的递归下降?

我试图使用json.net解析一个json文件。 该文件看起来像这样 {X: { Title:”foo”, xxxx:xxxx } } {Y: {ZZ: {Title: “bar”,…} } } 我试图通过Title属性递归处理所有对象的结构。 但我对JToken , JProperty , JContainer , JValue , JObject感到困惑。 阅读源代码并没有让我更加明智,也没有任何样本有帮助。 我想要一些东西 WalkNode(node, Action action) { foreach(var child in node.Children) { Action(child); WalkNode(child); } } Parse() { WalkNode(root, n=> { if(n[“Title”] != null) { … } }); }