Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert static method with generic paramerter to a generic class

I have the following html helper method:

public static MvcHtmlString CustomEditorFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
        Expression<Func<TModel, TProperty>> expression, 
        EditorOptions options,
        object htmlAttributes)
{
}

I have a need to put move it to into a helper library that is structured in this way:

public class HtmlHelperExenion
{
    public static Custom(this HtmlHelper helper)
    {
        return new HelperFactory(helper);
    }
}

public class HelperFactory
{
    internal HtmlHelper HtmlHelper { get; set; }
    public HelperFactory(HtmlHelper htmlHelper)
    {
        this.HtmlHelper = htmlHelper;
    }

    public virtual EditorBuilder CustomEditorFor( parameters from static MvcHtmlString static method above go here)
    {
        return new EditorBuilder( parameters go here );
    }
}

I am unable to figure out how to structure my HelperFactory class and the EditorBuilder class constructor to handle the generics correctly.

This is what I tried and it didn't work:

public static HelperFactory<TModel, TProperty> Custom<TModel, TProperty>(this HtmlHelper<TModel> helper)
{
    return new HelperFactory<TModel, TProperty>(helper);
}

public class HelperFactory<TModel, TProperty> : HelperFactory
{
    public HtmlHelper<TModel> HtmlHelper { get; set; }

    public HelperFactory(HtmlHelper<TModel> htmlHelper)
        : base((HtmlHelper)htmlHelper)
    {
        this.HtmlHelper = htmlHelper;
    }

    public virtual EditorBuilder<TModel, TProperty> CustomEditorFor(Expression<Func<TModel, TProperty>> expression,
EditorOptions options,
object htmlAttributes)
    {
        return new EditorBuilder<TModel, TProperty>(this.HtmlHelper,
                   expression,
                   options,
                   htmlAttributes);
    }
}

public class EditorBuilder<TModel, TProperty>
{
    public EditorBuilder(HtmlHelper<TModel> helper,
        Expression<Func<TModel, TProperty>> expression,
        EditorOptions options,
        object htmlAttributes)
    {

    }

}

The end goal is to be able to use it in this manner: @Html.Custom().CustomEditorFor(model=>model.Property)

Any assistance would be greatly appreciated.

like image 984
user2948570 Avatar asked Jan 24 '14 13:01

user2948570


People also ask

Can generics be used with static methods?

Static and non-static generic methods are allowed, as well as generic class constructors. The syntax for a generic method includes a list of type parameters, inside angle brackets, which appears before the method's return type.

Can a method that uses a generic class parameter be static?

1 Answer. You can't use a class's generic type parameters in static methods or static fields. The class's type parameters are only in scope for instance methods and instance fields.

How do you declare a generic method How do you invoke a generic method?

Generic MethodsAll generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example). Each type parameter section contains one or more type parameters separated by commas.

Can you have a generic method in a non-generic class?

Generic methods in non-generic classYes, you can define a generic method in a non-generic class in Java.


1 Answers

Your not far off, just change your HelperFactory to take in TModel so you can tie it to a generic HtmlHelper and make CustomEditorFor generic so it can accept any TProperty i.e.

public class HelperFactory<TModel>
{
    public HelperFactory(HtmlHelper<TModel> htmlHelper)
    {
        this.HtmlHelper = htmlHelper;
    }

    public HtmlHelper<TModel> HtmlHelper { get; private set; }

    public virtual EditorBuilder<TModel, TProperty> CustomEditorFor<TProperty>(Expression<Func<TModel, TProperty>> expression, EditorOptions options, object htmlAttributes)
    {
        return new EditorBuilder<TModel, TProperty>(this.HtmlHelper,
                   expression,
                   options,
                   htmlAttributes);
    }
}

Then all you need to do is update the extension method

public static class HtmlHelperExt
{
    public static HelperFactory<TModel> Custom<TModel>(this HtmlHelper<TModel> helper)
    {
        return new HelperFactory<TModel>(helper);
    }
}

So for a model like

public class MyModel
{
    public int Id { get; set; }
    ...
}

The usage would look like

@model MyModel

@Html.Custom().CustomEditorFor(x => x.Id, EditorOptions.Option1, null)
like image 146
James Avatar answered Sep 22 '22 03:09

James