D'accord, je pense que je vois ce que vous cherchez maintenant. Essayez ceci:
// Need to declare in advance to call within the lambda.
Func<ErrorType, XElement> recursiveGenerator = null;
recursiveGenerator = error => new XElement
(error.Name,
new XAttribute("Ignore", error.Ignore),
error.ChildErrors.Select(recursiveGenerator));
var errorList = new XElement
("ErrorList", errors.ChildErrors.Select(recursiveGenerator));
Voici un exemple complet:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
public class ErrorType
{
public String Name { get; set; }
public bool Ignore { get; set; }
public List<ErrorType> ChildErrors { get; protected set; }
public ErrorType()
{
ChildErrors = new List<ErrorType>();
}
}
public class ErrorList
{
public List<ErrorType> ChildErrors { get; protected set; }
public ErrorList()
{
ChildErrors = new List<ErrorType>();
}
}
public class Test
{
public static void Main()
{
var childError2 = new ErrorType {
Name = "ChildErrorName2", Ignore=false };
var childError1 = new ErrorType {
Name = "ChildErrorName1", Ignore=true,
ChildErrors = { childError2 }
};
var mainError = new ErrorType {
Name = "ErrorName1", Ignore=true,
ChildErrors = { childError1 }
};
var errorList = new ErrorList { ChildErrors = { mainError } };
// Need to declare in advance to call within the lambda.
Func<ErrorType, XElement> recursiveGenerator = null;
recursiveGenerator = error => new XElement
(error.Name,
new XAttribute("Ignore", error.Ignore),
error.ChildErrors.Select(recursiveGenerator));
var element = new XElement
("ErrorList",
errorList.ChildErrors.Select(recursiveGenerator);
Console.WriteLine(element);
}
}
Vive Jon qui semble à peu près juste. Je l'ai testé cependant et il a une erreur de compilation sur la ligne: errorList.ChildErrors.Select (recursiveGenerator (x)); J'ai remplacé X par errorList.ChildErrors [0] pour correspondre au type correct mais pas de joie. Mes compétences lambda sont faibles, donc je ne pouvais pas comprendre ce qui n'allait pas. – Chris
Oups, fixé cela - il devrait être errorList.ChildErrors.Select (recursiveGenerator); –
(J'avais l'habitude d'avoir errorList.ChildErrors.Select (x => recursiveGenerator (x)) qui est un peu redondant.) –