Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 679499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:15:59+00:00 2026-05-14T01:15:59+00:00

I’m not really familiar with creating generic methods, so I thought I’d put this

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:15:59+00:00Added an answer on May 14, 2026 at 1:15 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.