【发布时间】:2011-01-25 15:49:10
【问题描述】:
我对创建泛型方法并不是很熟悉,所以我想我应该将这个问题提交给社区并看看会发生什么。它甚至可能不是对泛型的有效使用!
我想创建一个 HtmlHelper 扩展方法,我可以在其中指定该方法属于某种类型。我将一个该类型的实例和一个 TagBuilder 对象的实例传递给该方法。然后我将标签的类属性指定为我传入的对象类型,并将所有对象的属性序列化到标签的属性中。
编辑... 这样做的好处是我可以轻松地将我的 Html 元素序列化为 javascript 对象,以便 jQuerying 到服务器和模型绑定,以及为类型指定样式的能力 ...结束编辑
此代码示例可能会澄清。
我有一个这样的类型:
public class MyType
{
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public MyType(int val1, int val2)
{
this.Prop1 = val1;
this.Prop2 = val2;
}
}
我在想的是生成一个辅助方法,可能带有类似于这样的签名:
public static string GetTag<T>(this HtmlHelper h, object myType, TagBuilder tag)
{
// cast myType to T //(i.e. MyType)
// use reflection to get the properties in MyType
// get values corresponding to the properties
// build up tag's attribute/value pairs with properties.
}
理想情况下,我可以打电话:
<% var myType = new MyType(123, 234); %>
<% var tag = new TagBuilder("div"); %>
<%= Html.GetTag<MyType>(myType, tag) %>
生成的html是
<div class="MyType" prop1="123" prop2="234" />
稍后,我可以打电话
<%= Html.GetTag<MyOtherType>(myOtherType, tag) %>
得到
<div class="MyOtherType" prop1="123" prop2="234" />
有可能吗?还是我看这个完全错误的方式?有人愿意让我了解更好的方法吗?
谢谢
戴夫
【问题讨论】:
-
这个是可以的,但是除非你传入的类型有有效的html属性,否则会生成无效的html。你确定你要这么做吗?你能详细说明这样做的目的吗?
-
@Baddie,我编辑说“这样做的好处是我可以轻松地将我的 Html 元素序列化为 javascript 对象,以便 jQuerying 到服务器和模型绑定,以及能够指定类型的样式”。我知道它是无效的 Html,但是在 XHTML 文档的上下文中(即它是 XML 的子集,因此可以扩展),可以使用自定义属性吗?
标签: c# asp.net-mvc extension-methods html-helper