我有以下接收类型T的Generic类,并且必须实现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();
}
}
我从类型T中知道的唯一一件事就是只有一个属性类型List<OtherType>
,我希望在此OtherType中返回一个IEnumerable<object[]>
项和属性一样多的项目。
我想使用ExpressionTrees,但我不知道如何撰写它。
答案 0 :(得分: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; }
}