I have an overloaded radio button extension method that is throwing the runtime execution off. The arguments of the first method can, in certain cases, be confused for the arguments of the second, and vice-versa.
public static MvcHtmlString MyRadioButton<TModel>( this HtmlHelper<TModel> helper, string property_name, string value, bool is_checked, string label = "", object attributes = null, bool separate_label = false, bool within_div = true, bool label_after = true )
{
// method implementation
}
and the overload:
public static MvcHtmlString MyRadioButton<TModel>( this HtmlHelper<TModel> helper, string property_name, string value, bool is_checked, object attributes = null, bool separate_label = false, bool within_div = true, bool label_after = true )
{
return MyRadioButton(helper, property_name, value, is_checked, "", attributes, separate_label, within_div, label_after);
}
In this case:
Html.MyRadioButton("name", "value", true, "");
the empty string is valid as both the object-type "attributes" argument and as the string-type "label" argument, causing a conflict between the methods.
Is there a way to generically type an argument and then exclude types from that definition in the method? I'm thinking maybe an inheritance class of the object type that accepts a list of types to exclude, like object<string>
. The alternative solution is to jumble the arguments of the overload so it is apparent which needs to be called, but that's far less elegant than properly typing the arguments.