我有以下Generic类,它接收类型T,并且必须实现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>
,我想返回一个IEnumerable<object[]>
,其中包含与此OtherType中的属性一样多的项。
我想使用ExpressionTrees来实现这一点,但我不知道如何编写它。
如果没有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; }
}