为什么某些属性/字段在调试器中可见,但无法从代码中访问? [英] Why some properties/fields are being visible in debugger, but not accessible from the code?

查看:67
本文介绍了为什么某些属性/字段在调试器中可见,但无法从代码中访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,为什么我不能从代码中访问 Attributes ,但是在调试器中完全可见?

似乎没有一个名为属性"的属性/字段

错误:

'ModelMetadata'不包含'Attributes'的定义,并且找不到可以接受的扩展方法'Attributes',它接受类型为'ModelMetadata'的第一个参数(您是否缺少using指令或程序集引用?)

代码:

 使用Microsoft.AspNetCore.Html;使用Microsoft.AspNetCore.Mvc.Rendering;使用Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;使用系统;使用System.Linq.Expressions;命名空间Project.Views{公共静态类HtmlExtensions{Public static IHtmlContent DescriptionFor< TModel,TValue>(此IHtmlHelper< TModel> html,Expression< Func< TModel,TValue>表达式){如果(html == null)抛出新的ArgumentNullException(nameof(html));如果(表达式== null)抛出新的ArgumentNullException(nameof(expression));var modelExplorer = ExpressionMetadataProvider.FromLambdaExpression(expression,html.ViewData,html.MetadataProvider);如果(modelExplorer == null)抛出新的InvalidOperationException($无法获得{ExpressionHelper.GetExpressionText(expression)}"的模型资源管理器);var resolveDisplayName = modelExplorer.Metadata.Attributes ??modelExplorer.Metadata.PropertyName;返回新的HtmlString(resolvedDisplayName ?? string.Empty);}}} 

解决方案

ModelMetadata .如果您查看该类型,将会看到它没有可以访问的 Attributes 成员.

但是,位于 modelExplorer.Metadata 的对象的运行时类型是类型 属性成员.

由于调试器仅关心运行时类型,因此您可以访问该属性.但是,当您尝试在代码中执行此操作时,会受到编译时间类型的限制.您必须先强制​​转换类型才能访问 Attributes 属性:

  ModelMetadata元数据= modexlExplorer.Metadata;//元数据.属性不存在DefaultModelMetadata defaultMetadata =(DefaultModelMetadata)元数据;//defaultMetadata.Attributes存在 

I'm curious, why I cannot access Attributes from the code, but it's perfectly visible in debugger?

Also seems like there's no property/field called "Attributes"

ModelMetadata Class

Error:

'ModelMetadata' does not contain a definition for 'Attributes' and no accessible extension method 'Attributes' accepting a first argument of type 'ModelMetadata' could be found (are you missing a using directive or an assembly reference?)

Code:

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);
        }
    }
}

解决方案

The ModelExplorer.Metadata property that you are accessing has the type ModelMetadata. If you look at that type, you will see that it does not have an Attributes member that you could access.

However, the runtime type of the object that sits at modelExplorer.Metadata is the type DefaultModelMetadata which does have an Attributes member.

Since the debugger only cares about runtime types, you are able to access that property. But when you attempt to do it in code, you are limited by the compile time types. You would have to cast the type first in order to access the Attributes property:

ModelMetadata metadata = modexlExplorer.Metadata;
// metadata.Attributes does not exist

DefaultModelMetadata defaultMetadata = (DefaultModelMetadata) metadata;
// defaultMetadata.Attributes exists

这篇关于为什么某些属性/字段在调试器中可见,但无法从代码中访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆