加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#System.Type Type如何具有name属性

发布时间:2020-12-16 03:16:38 所属栏目:百科 来源:网络整理
导读:我很确定这与实现接口和继承有关. 在c#中,System.Type类如何具有属性Name?当我从元数据或使用对象浏览器示例Type类的代码时,我看到Type类没有: string Name 任何地方定义 我还看到Type继承自MemberInfo并实现了_Type和IReflect(就像这样): public abstrac
我很确定这与实现接口和继承有关.

在c#中,System.Type类如何具有属性Name?当我从元数据或使用对象浏览器示例Type类的代码时,我看到Type类没有:

string Name

任何地方定义

我还看到Type继承自MemberInfo并实现了_Type和IReflect(就像这样):

public abstract class Type : MemberInfo,_Type,IReflect

基类MemberInfo具有以下属性:

public abstract string Name { get; }

据我所知,由于抽象修饰符,必须在派生类中重写它.我没有在Type中看到它……

然后在_Type接口中有一个属性:

string Name { get; }

因为它在一个接口中,它也没有自己的实现.

Name的定义在哪里以及它在System.Type类中的值如何?或者我不理解继承和实现接口如何适用于此类

解决方法

请注意,System.Type本身就是一个抽象类.这意味着它可以在子类中重写.实际上,如果您执行以下操作,您可以看到运行时类型实际上不是System.Type:
typeof(Type).GetType().FullName; // System.RuntimeType

System.RuntimeType是您在文档中看不到的内部类型,但它会覆盖Name属性.看起来有点像这样:

namespace System
{
    internal class RuntimeType : Type,ISerializable,ICloneable
    {
        ...
        public override string Name
        {
            get { ... }
        }
    }
}

请参阅official documentation中的此注释:

Type is an abstract base class that allows multiple implementations. The system will always provide the derived class RuntimeType. In reflection,all classes beginning with the word Runtime are created only once per object in the system and support comparison operations.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读