C#反射,兰姆达?
我需要使用反射来实现这一点:C#反射,兰姆达?,c#,reflection,lambda,C#,Reflection,Lambda,我需要使用反射来实现这一点: @Html.Grid((IEnumerable<MyType>)list).Columns(columns => { columns.Add(foo => foo.Title) .Titled("Custom column title") .SetWidth(110);
@Html.Grid((IEnumerable<MyType>)list).Columns(columns =>
{
columns.Add(foo => foo.Title)
.Titled("Custom column title")
.SetWidth(110);
columns.Add(foo => foo.Description)
.Sortable(true);
}).WithPaging(20)
所以我需要做一些类似的事情:
g.Columns(columns =>
{
columns.Add(foo => foo.Title)
.Titled("Custom column title")
.SetWidth(110);
columns.Add(foo => foo.Description)
.Sortable(true);
}).WithPaging(20)
使用反射
var g
是HtmlGrid的类型:
有人可以提供示例代码来执行此操作吗?
为了安全起见,我添加了Grid.Mvc github链接: 因为我不知道怎么解决它(第三次是魅力:) 这次我测试了一下 你用它就像
@Html.MakeGrid(items, someType)
其中someType
是对象的类型
甚至
this.Write(Helper.MakeGrid(Html, items, someType);
或者以任何方式使用@Html.Grid方法
我作弊两次:一次是通过非泛型MakeGrid
的反射调用泛型MakeGridInternal
,第二次是在CreateColumns
中使用反射获取各个列的属性信息
请注意,在调用MakeGrid
时,您必须在type
中传递元素的真实类型,并且项必须是任何引用类型的集合(但如果将签名更改为对象项
,则90%有效)
公共静态类帮助器{
公共静态IHtmlString MakeGrid(此HtmlHelper html,IEnumerable items,类型){
//您甚至可以给出以下签名:
//公共静态IHtmlString MakeGrid(HtmlHelper html,对象项,类型)
//但很明显,物品必须是某种类型的集合!
return(IHtmlString)typeof(Helper).GetMethod(“MakeGridInternal”).MakeGenericMethod(type).Invoke(null,新对象[]{html,items});
}
公共静态IHtmlString MakeGridInternal(HtmlHelper html,IEnumerable items),其中T:class{
返回GridMvc.Html.GridExtensions.Grid(Html,items)
.Columns(CreateColumns)
.带传呼(10);
}
公共静态void CreateColumns(IGridColumnCollection列){
类型t=类型(t);
PropertyInfo title=t.GetProperty(“title”);
PropertyInfo description=t.GetProperty(“description”);
列。添加(标题)
.标题为(“我的标题”)
.设置宽度(100);
列。添加(说明)
.Sortable(true);
}
}
您能否提供更多信息,说明为什么要通过反射调用此函数?假设这是GridMvc,我只能看到名为Columns的属性,而不是方法。。。你能给你正在使用的类型一个引用吗?你不能把g
转换成正确的类型并正常调用这个方法吗?你怎么知道类型t
有一个标题和一个描述
属性?因为完全通过反射进行操作会很难看。必须通过表达式树来构建foo=>foo.Title
。@xanatos您能告诉我您的意思吗?举个例子?好主意,谢谢。我将在周末后尝试:)嗯……我得到错误:没有为Int32 id system.object类型定义属性。可以用简单的方法解决吗?:@mrfazolka抱歉,不,我想好了,不,没关系:)我有备份解决方案:)我有备份解决方案:)@mrfazolka现在试试:-)我考虑过这个问题,安装了MVC并解决了它
this.Write(Helper.MakeGrid(Html, items, someType);
public static class Helper {
public static IHtmlString MakeGrid(this HtmlHelper html, IEnumerable<object> items, Type type) {
// You can give even this signature:
// public static IHtmlString MakeGrid(HtmlHelper html, object items, Type type)
// But clearly items MUST be a collection of some type!
return (IHtmlString)typeof(Helper).GetMethod("MakeGridInternal").MakeGenericMethod(type).Invoke(null, new object[] { html, items });
}
public static IHtmlString MakeGridInternal<T>(HtmlHelper html, IEnumerable<T> items) where T : class {
return GridMvc.Html.GridExtensions.Grid<T>(html, items)
.Columns(CreateColumns)
.WithPaging(10);
}
public static void CreateColumns<T>(IGridColumnCollection<T> columns) {
Type t = typeof(T);
PropertyInfo title = t.GetProperty("Title");
PropertyInfo description = t.GetProperty("Description");
columns.Add(title)
.Titled("MyTitle")
.SetWidth(100);
columns.Add(description)
.Sortable(true);
}
}