使用泛型创建的HtmlHelper扩展方法 [英] Using Generics to Create HtmlHelper Extension Methods

查看:175
本文介绍了使用泛型创建的HtmlHelper扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是很熟悉创建通用的方法,所以我想我应该把这个问题给社会和放大器;看什么回来。它甚至可能不是仿制药的有效使用!



我想创建一个扩展的HtmlHelper方法,在那里我可以指定该方法是一种特定类型。我传递到方法该类型的一个实例,以及一个TagBuilder对象的一个​​实例。那么我指定标记的类属性是对象的类型在我进去时,所有的对象的属性序列化到标签的属性。



修改。 ..
这样做的好处是,我可以很容易地然后我序列化的HTML元素在成JavaScript对象为jQuerying到服务器和放大器;模型绑定,以及为一类
...最终修改

指定样式的能力

此代码示例可能会澄清



我有一种类型是这样的:

 公共类的MyType 
{
公众诠释为prop1 {搞定;组; }
公众诠释Prop2 {搞定;组; }

公众的MyType(INT VAL1,诠释val2)将
{
this.Prop1 = VAL1;
this.Prop2 = val2的;
}
}



我在想什么是产生一个辅助方法,可能与类似这样的签名:

 公共静态字符串GetTag< T>(此的HtmlHelper小时,对象的myType,TagBuilder标签)
{
//投的myType于T //(即的MyType)
//使用反射来得到的MyType
属性//获得对应的属性$ b值$ b //建立与性能标签的属性/值对。
}



在理想情况下,我可以再拨打:

 <%VAR =的myType新的MyType(123,234); %GT; 
<%var标记=新TagBuilder(分区); %GT;
<%= Html.GetTag<&MyType的GT;(的myType,标签)%GT;

和生产将是



<$ P的HTML $ p> < D​​IV CLASS =MyType的为prop1 =123prop2 =234/>

和以后,我可以叫

 <%= Html.GetTag< MyOtherType>(myOtherType,标签)%GT; 



获得

 < D​​IV CLASS =MyOtherType为prop1 =123prop2 =234/> 



它甚至有可能?还是我看着这个完全错误的方式?任何人都关心,以填补我上一个更好的方法?



感谢



戴夫


解决方案

有关你想要做什么,我想使用泛型将是主要受益采取类型推断的优势。如果你声明你的方法如下:

 公共静态字符串GetTag< T>(此的HtmlHelper H,T MyObj中,TagBuilder标签) 

您将不必调用它时指定类型参数,因为它会从推断使用(即编译器会看到第二个参数的类型是的MyType ,所以会猜测那件T ==的MyType)。



但是不管怎样,你并不真的需要指定类型:该方法可以称之为的GetType 的对象,并使用所产生的键入它会以同样的方式已经使用的typeof(T),所以仿制药不那么有用这里。



不过,我看到一个原因无论如何使用它们:如果你键入 MySubType ,从的MyType 继承的对象,你可能想渲染只有在的MyType 定义的属性:你只需要指定情况下的MyType 作为类型参数,覆盖类型推断



下面是一个可能的实现:

 公共静态字符串GetTag< T>(此的HtmlHelper H,T MyObj中,TagBuilder标签)
{
类型T = typeof运算(T);
tag.Attributes.Add(类,t.Name);
的foreach(的PropertyInfo道具在t.GetProperties())
{
对象propValue = prop.GetValue(MyObj中,NULL);
串stringValue的= propValue!= NULL? propValue.ToString()的String.Empty;
tag.Attributes.Add(prop.Name,stringValue的);
}
返回tag.ToString();
}


I'm not really familiar with creating generic methods, so I thought I'd put this question to the community & see what comes back. It might not even be a valid use of generics!

I'd like to create a HtmlHelper extension method where I can specify that the method is of a certain type. I pass into the method an instance of that type, and an instance of a TagBuilder object. I then specify the class attribute of the tag to be the type of object I passed in, with all the object's properties serialized into the tag's attributes.

edit... the benefit of this would be that I could then easily serialize my Html elements in into javascript objects for jQuerying to the server & Model Binding, as well as the ability to specify style for a type ...end edit

This code sample might clarify.

I have a type like this:

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;
    }
}

What I'm thinking is to produce a helper method, maybe with a signature similar to this:

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.
}

Ideally I could then call:

<% var myType = new MyType(123, 234); %>
<% var tag = new TagBuilder("div"); %>
<%= Html.GetTag<MyType>(myType, tag) %>

and the html produced would be

<div class="MyType" prop1="123" prop2="234" />

And later on, I can call

<%= Html.GetTag<MyOtherType>(myOtherType, tag) %>

to get

<div class="MyOtherType" prop1="123" prop2="234" />

Is it even possible? Or am I looking at this totally the wrong way? Anyone care to fill me on on a better approach?

Thanks

Dave

解决方案

For what you're trying to do, I think the main benefit of using generics would be to take advantage of type inference. If you declare your method as follows :

public static string GetTag<T>(this HtmlHelper h, T myObj, TagBuilder tag)

You won't have to specify the type parameter when calling it, because it will be inferred from the usage (i.e. the compiler will see that the second parameter's type is MyType, so it will guess that T == MyType).

But anyway, you don't really need to specify the type : the method could call GetType on the object, and use the resulting type the same way it would have used typeof(T), so generics aren't so useful here.

However, I see one reason to use them anyway : if you have an object of type MySubType, which inherits from MyType, you might want to render only properties defined in MyType : in that case you just have to specify MyType as the type parameter, overriding the type inference.

Here's a possible implementation :

public static string GetTag<T>(this HtmlHelper h, T myObj, TagBuilder tag)
{
    Type t = typeof(T);
    tag.Attributes.Add("class", t.Name);
    foreach (PropertyInfo prop in t.GetProperties())
    {
        object propValue = prop.GetValue(myObj, null);
        string stringValue = propValue != null ? propValue.ToString() : String.Empty;
        tag.Attributes.Add(prop.Name, stringValue);
    }
    return tag.ToString();
}

这篇关于使用泛型创建的HtmlHelper扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆