C#html-helper扩展现有方法而不覆盖?

我已经搜索过这个问题并找到了可能的答案 ,但我还是需要一些帮助。

我正在尝试编写一个html-helper来扩展现有LabelFor方法的function

public static MvcHtmlString LabelFor(this HtmlHelper html, Expression<Func> expression, IDictionary htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); //string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); var labelText = html.LabelFor(expression); if (String.IsNullOrEmpty(labelText.ToString())) { return MvcHtmlString.Empty; } if (metadata.IsRequired) { labelText = new MvcHtmlString(labelText.ToString().Substring(0, labelText.ToString().Length - 8).Trim() + "*"); } TagBuilder tag = new TagBuilder("label"); tag.MergeAttributes(htmlAttributes); tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); tag.SetInnerText(labelText.ToString()); return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); } 

我正在尝试添加一个function,其中Method将检查变量是否具有“required”标志,然后执行某些操作(在这种情况下,在标签末尾添加红色*)

 [Required] [Display(Name = "Year")] public string ProjectYr { get; set; } 

但是,我觉得我正在覆盖整个LabelForfunction。 有没有办法简单地向现有的LabelFor方法添加新function,同时保留原始的所有function而不覆盖它? 无论如何, 覆盖都不起作用,因为我的方法是静态的。

非常感谢你提前!

这是一个完整的例子,可以满足您的要求

访问辅助扩展类中的模型属性

 public static class LabelExtensions { public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, IDictionary htmlAttributes, String requiredMarker = "*") { return LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), null, htmlAttributes, requiredMarker); } public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Object htmlAttributes, String requiredMarker) { return LabelFor(html, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), requiredMarker); } internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String labelText = null, IDictionary htmlAttributes = null, String requiredMarker = null) { var resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); var tag = new TagBuilder("label"); tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName))); tag.SetInnerText(resolvedLabelText); tag.MergeAttributes(htmlAttributes, true); if (metadata.IsRequired && !String.IsNullOrWhiteSpace(requiredMarker)) { var requiredSpan = new TagBuilder("span") {InnerHtml = requiredMarker}; requiredSpan.AddCssClass("required"); tag.InnerHtml += requiredSpan; } var result = tag.ToString(TagRenderMode.Normal); return new MvcHtmlString(result); } } 

这是unit testing

 public static class LabelExtensionFixtures { [TestFixture] public class should_return_label_with_required_info : MvcExtensionFixtureBase { private class TestClass { [Required] public Guid Id { get; set; } } private MvcHtmlString _expectedResult; private HtmlHelper _sut; private MvcHtmlString _result; [SetUp] public void Given() { //arrange _expectedResult = MvcHtmlString.Create( ""); _sut = CreateHtmlHelper(new TestClass {Id = Guid.NewGuid()}); //act _result = _sut.LabelFor(model => model.Id, new { @class = "control-label col-md-2" }, "*"); } [Test] public void Test() { //asert Assert.That(_result.ToHtmlString(), Is.EqualTo(_expectedResult.ToHtmlString())); } } } public abstract class MvcExtensionFixtureBase { protected HtmlHelper CreateHtmlHelper(T instance) { var viewDataDictionary = new ViewDataDictionary(instance); var viewContext = A.Fake(); A.CallTo(() => viewContext.ViewData).Returns(viewDataDictionary); var viewDataContainer = A.Fake(); A.CallTo(() => viewDataContainer.ViewData).Returns(viewDataDictionary); return new HtmlHelper(viewContext, viewDataContainer); } }