You need to specify how you are going to convert XElementto an instance T. Currently, your code just doesn't work if it ServiceAccountalready has an explicit conversion from XElement.
, FromXElement - :
private static readonly Dictionary<Type, Delegate> Converters =
new Dictionary<Type, Delegate> {
{ typeof(ServiceAccount),
(Func<XElement, ServiceAccount>) ServiceAccount.FromXElement },
{ typeof(OtherEntity),
(Func<XElement, OtherEntity>) OtherEntity.FromXElement },
};
- :
public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
Delegate converter;
if (!Converters.TryGetValue(typeof(TEntity), out converter))
{
throw new InvalidOperationException("...");
}
Func<XElement, TEntity> realConverter = (Func<XElement, TEntity>) converter;
var filename = GetEntityFileName<TEntity>();
var doc = XDocument.Load(filename);
return doc.Descendants(entityName)
.Select(realConverter)
.AsQueryable();
}
, IQueryable<T> , ... - IEnumerable<T>?