zoukankan      html  css  js  c++  java
  • NewSlot and ReuseSlot

    namespace CSharpTester
    {
    class Program
    {
    static void Main(string[] args)
    {
    var methodA
    = typeof(A).GetMethod("Test");
    PrintAttributes(
    typeof(System.Reflection.MethodAttributes), (int)methodA.Attributes);

    var methodB
    = typeof(B).GetMethod("Test");
    PrintAttributes(
    typeof(System.Reflection.MethodAttributes), (int)methodB.Attributes);
    }
    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
    if (!attribType.IsEnum) { Console.WriteLine("This type is not an enum."); return; }

    FieldInfo[] fields
    = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
    for (int i = 0; i < fields.Length; i++)
    {
    int fieldvalue = (Int32)fields[i].GetValue(null);
    if ((fieldvalue & iAttribValue) == fieldvalue)
    {
    Console.WriteLine(fields[i].Name);
    }
    }
    }
    }
    public interface IA
    {
    void Test();

    void Test1();
    }
    public class A : IA
    {
    public void Test() { Console.WriteLine(1); }
    void IA.Test1() { Console.WriteLine(1); }
    public void Test2() { Console.WriteLine(1); }
    public void Test3() { Console.WriteLine(1); }
    public void Test4() { Console.WriteLine(1); }
    public void Test5() { Console.WriteLine(1); }
    }
    public class B : IA
    {
    public void A1() { Console.WriteLine(1); }
    public void A2() { Console.WriteLine(1); }
    public void A3() { Console.WriteLine(1); }
    public void Test() { Console.WriteLine(1); }
    public void Test1() { Console.WriteLine(1); }
    public void Test2() { Console.WriteLine(1); }
    }


    }

    两个类A,B 继承接口IA,都实现方法Test

    以下是运行结果

    两个都出现了newslot 和reuseslot,结论是都重用了同样的slot,继承以后所有父类,接口的槽的位置不变

    以下是这两个的定义

         
    Supported by the XNA Framework ReuseSlot Indicates that the method will reuse an existing slot in the vtable. This is the default behavior.
    Supported by the XNA Framework NewSlot Indicates that the method always gets a new slot in the vtable.

    url:http://msdn.microsoft.com/en-us/library/system.reflection.methodattributes.aspx

    (我的理解就是 有newslot的 说明在当前类型有实现了,否则就在父类或者外部;有reuseslot的就说明该方法必然是重写或者继承自父类,当然方法偏移量应该是要一致的)

    如果某个类并没有实现某个方法,而是继承自其父类的实现

    那么newslot为false  而reuseslot为true

    以下是代码

    namespace CSharpTester
    {
    class Program
    {
    static void Main(string[] args)
    {
    var methodA
    = typeof(A).GetMethod("A1");

    PrintAttributes(
    typeof(System.Reflection.MethodAttributes), (int)methodA.Attributes);
    }
    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
    if (!attribType.IsEnum) { Console.WriteLine("This type is not an enum."); return; }

    FieldInfo[] fields
    = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
    for (int i = 0; i < fields.Length; i++)
    {
    int fieldvalue = (Int32)fields[i].GetValue(null);
    if ((fieldvalue & iAttribValue) == fieldvalue)
    {
    Console.WriteLine(fields[i].Name);
    }
    }
    }
    }
    public class A : B
    {
    }
    public class B
    {
    public void A1() { Console.WriteLine(1); }
    }
    }

    以下是运行结果,有ReuseSlot 没有NewSlot

    多继承接口的情况下,还需要进一步分析

  • 相关阅读:
    docker参数--restart=always的作用
    docker参数expose使用
    Linux主机添加路由和端口转发
    docker自动开启端口转发功能
    【Tips】【UE】总结自己常用的UltraEdit使用技巧
    浅谈I2C总线
    I2C总线简介(很经典)
    ECN
    视频编码未来简史
    爬虫与反爬虫
  • 原文地址:https://www.cnblogs.com/PurpleTide/p/2182877.html
Copyright © 2011-2022 走看看