我很好奇,为什么我不能访问Attributes
从代码,但它在调试器的完全可见?
也似乎像有没有所谓的“属性”属性/字段
ModelMetadata类
错误:
“ModelMetadata”不包含关于“属性”和没有可访问的扩展方法“属性”接受类型“ModelMetadata”的第一个参数的定义可以找到(是否缺少using指令或程序集引用?)
码:
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using System;
using System.Linq.Expressions;
namespace Project.Views
{
public static class HtmlExtensions
{
public static IHtmlContent DescriptionFor<TModel, TValue>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
if (html == null) throw new ArgumentNullException(nameof(html));
if (expression == null) throw new ArgumentNullException(nameof(expression));
var modelExplorer = ExpressionMetadataProvider.FromLambdaExpression(expression, html.ViewData, html.MetadataProvider);
if (modelExplorer == null) throw new InvalidOperationException($"Failed to get model explorer for {ExpressionHelper.GetExpressionText(expression)}");
var resolvedDisplayName = modelExplorer.Metadata.Attributes ?? modelExplorer.Metadata.PropertyName;
return new HtmlString(resolvedDisplayName ?? string.Empty);
}
}
}