zoukankan      html  css  js  c++  java
  • 接口和类 反射的差异性

    .NET接口和类 反射的差异性发现

    1 背景

    在项目中使用反射,反射出某类型的所有属性(Property)和对应的属性值。起初为了性能考虑在模块首次加载就反射类型的所有属性并将其存入字典。根据一般的编程规范——基于接口编程,所以首次传入的反射类型是一个接口。所以程序必然出现了异常否者就没有此文了。

    2 重现实验代码

    代码其实很简单,主要是比较接口反射和类反射的不同

    复制代码
    interface ILevel0
    {
      string LevelZero { get; set; }
    }
    
    interface ILevel1 : ILevel0
    {
      string LevelOne { get; set; }
    }
    
    class Base0
    {
      public string BaseZero { get; set; } } class Base1 : Base0 {   public string BaseOne { get; set; } } class Base : Base1, ILevel1 { #region private string level0; private string level1; #endregion #region [property] public string LevelOne { get { return level1; } set { level1 = value; } } public string LevelZero {   get { return level0; }   set { level0 = value; } } public string BaseClass { get; set; } #endregion [property] }
    复制代码
    复制代码
     1 static void Main(string[] args)
     2 
     3 {
     4 
     5 Base bas = new Base
     6 
     7 {
     8 
     9 BaseClass = "BaseClass",
    10 
    11 LevelOne = "level1",
    12 
    13 LevelZero = "level0",
    14 
    15 BaseOne = "BaseOne",
    16 
    17 BaseZero = "BaseZero"
    18 
    19 };
    20 
    21  
    22 
    23 ILevel0 level0 = bas;
    24 
    25 ILevel1 level1 = bas;
    26 
    27 Base0 base0 = bas;
    28 
    29 Base1 base1 = bas;
    30 
    31  
    32 
    33 RetriveProperty(typeof(ILevel0));
    34 
    35 RetriveProperty(typeof(ILevel1));
    36 
    37 RetriveProperty(typeof(Base0));
    38 
    39 RetriveProperty(typeof(Base1));
    40 
    41 RetriveProperty(typeof(Base));
    42 
    43 RetriveProperty(level0.GetType(), bas);
    44 
    45 //RetriveProperty(level1.GetType(), bas);
    46 
    47 //RetriveProperty(base0.GetType(), bas);
    48 
    49 //RetriveProperty(base1.GetType(), bas);
    50 
    51 //RetriveProperty(bas.GetType(), bas);
    52 
    53 Console.ReadKey();
    54 
    55 }
    56 
    57 public static void RetriveProperty(Type type, Object obj)
    58 
    59 {
    60 
    61 Console.WriteLine("-------{0}--------", type.Name);
    62 
    63 var property = type.GetProperties();
    64 
    65 foreach (var pro in property)
    66 
    67 {
    68 
    69 Console.WriteLine("{0}-{1}", pro.Name, pro.GetValue(obj, null));
    70 
    71 }
    72 
    73 Console.WriteLine();
    74 
    75 }
    76 
    77 public static void RetriveProperty(Type type)
    78 
    79 {
    80 
    81 Console.WriteLine("-------{0}--------", type.Name);
    82 
    83 var property = type.GetProperties();
    84 
    85 foreach (var pro in property)
    86 
    87 {
    88 
    89 Console.WriteLine("Property:{0}", pro.Name);
    90 
    91 }
    92 
    93 Console.WriteLine();
    94 
    95 }
    复制代码

    3 实验结果

    接口Level1反射并没有出现接口Level0的属性

    基类Base1反射出现了基类Base0中定义的属性

    所有指向类型Base的引用反射属性的结果都是相同的

    4 总结

    接口只是作为一种约定,没有实现类型的继承机制
     
     
     
    标签: .NET CSharp
  • 相关阅读:
    简单修改 MySQL 的 root 账号密码
    树莓派 Raspberry-Pi 折腾系列:系统安装及一些必要的配置
    [原创] JavaScript 图片放大镜插件 enlarge.js 以及移动版 enlarge.touch.js
    丢掉 WinPE,使用 DISKPART 来分区吧
    【原创】只用 HTML / CSS 画出一把 UKULELE(夏威夷四弦吉他)
    用网页模拟手机(安卓)通知列表中的「清除」效果
    关于 Google Chrome 中的全屏模式和 APP 模式
    简单好用的 AJAX 上传插件,还可以抛弃难看的 file 按钮哦~
    C# 操作 Excel 常见问题收集和整理(定期更新,欢迎交流)
    C++:几种callable实现方式的性能对比
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3244804.html
Copyright © 2011-2022 走看看