我列出了30个模型类,并且必须为每个类执行以下操作:
private static IEdmModel GetEdmModel(IServiceProvider serviceProvider)
{
var odataBuilder = new ODataConventionModelBuilder(serviceProvider);
odataBuilder
.EntitySet<AnswerCondition>("AnswerConditions")
.EntityType
.Select()
.Expand()
.OrderBy()
.Filter()
.Count();
现在,我想要一个键值列表,并在for循环中执行以下操作:
var list = new List<KeyValuePair<string, Type>>() {
new KeyValuePair<string, Type>("ActionTypes", typeof(ActionType)),
new KeyValuePair<string, Type>("AnswerConditions", typeof(AnswerCondition)),
.
.
.
};
foreach (KeyValuePair<string, Type> item in list)
{
string address = item.Key;
Type t = item.Value;
odataBuilder
.EntitySet<t>(address)
.EntityType
.Select()
.Expand()
.OrderBy()
.Filter()
.Count();
}
但是它显示了一个错误 t
't'是变量,但像类型一样使用
如何将类列表存储在变量中,并在以后用作泛型类型?
使用反射:
Type oDataType = odataBuilder.GetType();
MethodInfo genericMethod = oDataType.GetMethod("EntitySet", 1, new[] {typeof(string)});
foreach (KeyValuePair<string, Type> item in list)
{
string address = item.Key;
Type t = item.Value;
MethodInfo method = genericMethod.MakeGenericMethod(t);
dynamic set = method.Invoke(odataBuilder, new object[]{address});
set
.EntityType
.Select()
.Expand()
.OrderBy()
.Filter()
.Count();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句