TODO: solution using Stack
stackoverflow questions:
How to “unroll” a “recursive” structure
IEnumerable and Recursion using yield return
Same function in two versions. The function goes through all the elements recursively and return they as IEnumerable.
There is no solution yet, using Stack
public class ResourceFileInfo
{
...
// By yield
public IEnumerable<ResourceInfo> GetAllResources1()
{
if (_localResources != null)
{
foreach (ResourceInfo ri in _localResources)
{
yield return ri;
}
}
if (_mergedFiles != null)
{
foreach (ResourceFileInfo file in _mergedFiles)
{
foreach (ResourceInfo fri in file.Resources)
{
yield return fri;
}
}
}
}
// By Concat & SelectMany
public IEnumerable<ResourceInfo> GetAllResources2
{
if (_localResources != null)
{
if (_mergedFiles != null)
return _localResources.Concat(_mergedFiles.SelectMany(x => x.Resources));
else
return _localResources;
}
else
{
if (_mergedFiles != null)
return _mergedFiles.SelectMany(x => x.Resources);
else
return Enumerable.Empty<ResourceInfo>();
}
}
}
Object structure:
- 📁 ResourceFileInfo
- 📁 ResourceFileInfo
- 📦 ResourceInfo
- 📦 ResourceInfo
- 📁 ResourceFileInfo
- 📁 ResourceFileInfo
- 📦 ResourceInfo
- 📦 ResourceInfo
- 📁 ResourceFileInfo
- 📦 ResourceInfo
- 📁 ResourceFileInfo
public class ResourceInfo
{
public string ClassName { get; }
public string XKey { get; }
public ResourceInfo(string className, string xKey)
{
ClassName = className;
XKey = xKey;
}
}
public class ResourceFileInfo
{
internal ResourceFileInfo[] _mergedFiles;
internal ResourceInfo[] _localResources;
public string Path { get; }
public IEnumerable<ResourceFileInfo> MergedFiles
=> _mergedFiles ?? Enumerable.Empty<ResourceFileInfo>();
public IEnumerable<ResourceInfo> LocalResources
=> _localResources ?? Enumerable.Empty<ResourceInfo>();
public ResourceFileInfo(AssemblyInfo parentAssembly, string path)
{
ParentAssembly = parentAssembly;
Path = path;
}
...
}