Given the following types:
public interface IFruit { String name { get; set; } Int32 id { get; set; } }
public class Fruit : IFruit { public String name { get; set; } public Int32 id { get; set; } }
I think that you could do something like this:
static IEnumerable<T> GetSomeFruit<T>(String xml)
where T : IFruit, new()
{
return XElement.Parse(xml)
.Elements("fruit")
.Select(f => new T {
name = f.Element("name").Value,
id = Int32.Parse(f.Element("id").Value)
});
}
Which you would call like this:
IEnumerable<Fruit> fruit = GetSomeFruit<Fruit>(yourXml);
[http://stackoverflow.com/questions/1460567/xml-to-ienumerablet]