【发布时间】:2019-10-06 07:13:05
【问题描述】:
我正在清理如此讨厌的代码生成 他们在 UI (ASPX) 中有所有下拉菜单的类, 我首先能够更改为 linq 而不是我想要一个静态方法来传递列表
因此逻辑已从 UI 中卸载 这就是我所拥有的
我将传递的所有列表都具有以下这些属性, 刚刚使用
编写通用方法public static string Get_GenericDisplayData_Data(IList<T> ilist)
{
var list = (from prop in ilist
select new
Display = prop.Display ?? "",
Description = prop.Description ?? "",
Id = prop.Id
}).ToList();
string json = new JavaScriptSerializer().Serialize(list);
return json;
}
编译器抛出错误,显示、ID 和描述不存在或找不到, 如何解决?
【问题讨论】:
-
你的问题是什么?
-
编辑了 cmets
-
你可以通过输入
where T:{YouClassWithTheseFields}来修复它 -
我不知道,这个项目中有将近 100 个具有这些属性的类
-
为什么不编译?好吧,让我们举一个简单的例子。假设您使用
List<int>调用了此方法。int是否有Display属性? (当然,不,它没有)。 这就是它无法编译的原因。 您是否知道对它的每次调用都将具有这些属性并不重要。 编译器不知道这一点。实际上,我建议您阅读 stackoverflow.com/questions/48748604/… 和 docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… 。