2015-03-05 4 views
-1

У меня есть следующий Generic класс, который принимает тип Т и должен реализовывать IEnumerable:ExpressionTree и IEnumerable реализация

public class ConfigurationHelper<T>: IEnumerable<object[]> where T: BaseTestConfiguration 
{ 
    public T _configuration; 

    public ConfigurationHelper(configuration) 
    { 
     _configuration = configuration; 
    } 

    public IEnumerator<object[]> GetEnumerator() 
    { 
     ParameterExpression element = Expression.Parameter(typeof(T), "element"); 
     //use reflection to check the property that's a generic list 
     foreach (PropertyInfo property in _configuration.GetType().GetGenericArguments()[0].GetProperties()) 
     { 
     } 

     /* HERE IS MY ISSUE */ 
     return _configuration.Select(x=>GET LIST<OTHERTYPE> PROPERTY) 
          .SelectMany(i => new object[] { AS MANY PROPERTIES AS OTHERTYPE }) 
          .GetEnumerator(); 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return GetEnumerator(); 
    } 
} 

Единственное, что я знаю от моего типа Т его, что есть только один тип недвижимости List<OtherType>, и Я хотел бы вернуть IEnumerable<object[]> с таким количеством элементов, как свойства в этом OtherType.

Я хотел бы использовать ExpressionTrees для этого, но я не знаю, как его создать.

+1

Непонятно, чего вы пытаетесь достичь. Для его выражения используйте псевдокод. – abatishchev

+0

В текущем коде вы будете запускать весь запрос LINQ каждый раз при запуске перечисления. – abatishchev

+0

Привет, Можете ли вы дать мне некоторое определение BaseTestConfiguration? –

ответ

0

Не может действительно что-то сделать без определения BestTestConfiguration. Если вы можете его предоставить, я изменю код. Пожалуйста, проверьте приведенный ниже код, что я пытался сделать из своего понимания:

class Program 
    { 
     public static void Main(string[] args) 
     { 
      var helper = new ConfigurationHelper<BestTestConfiguration>(new BestTestConfiguration() 
      { 
       Objects = new List<string>() 
       { 
        "testing 1", 
        "testing 2" 
       } 
      }); 
      var item = helper.GetEnumerator(); 
      Console.ReadLine(); 
     } 


    } 

    public class ConfigurationHelper<T> : IEnumerable<object[]> where T : BestTestConfiguration 
    { 
     public T Configuration; 

     public ConfigurationHelper(T configuration) 
     { 
      Configuration = configuration; 
     } 

     public IEnumerator<object[]> GetEnumerator() 
     { 
      ParameterExpression element = Expression.Parameter(typeof(T), "element"); 
      //use reflection to check the property that's a generic list 
      var items = new List<object[]>(); 

      foreach (PropertyInfo property in Configuration.GetType().GetProperties().Where(x => x.PropertyType.IsGenericType)) 
      { 
       var valueOfProperty = Configuration.GetType().GetProperty(property.Name).GetValue(Configuration, null); 
       items.Add(new object[] {valueOfProperty}); 
      } 

      return items.GetEnumerator(); 
     } 

     IEnumerator IEnumerable.GetEnumerator() 
     { 
      return GetEnumerator(); 
     } 
    } 

    public class BestTestConfiguration 
    { 
     public List<string> Objects { get; set; } 
    } 
Смежные вопросы