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

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

  • 相关阅读:
    pytest文档70-Hook钩子函数完整API总结
    pytest文档69-Hook函数之参数化生成测试用例pytest_generate_tests
    pytest文档68-pytest-lazy-fixture 插件解决 pytest.mark.parametrize 中使用 fixture 问题
    pytest文档67-pytest.mark.parametrize 中使用 fixture
    docker学习15-Docker 使用修改后容器来创建镜像
    pytest文档66-工厂化的 fixtures
    pytest文档65-内置 request 读取项目的根目录 rootdir
    jenkins学习16-无法连接仓库:Error performing git command: git ls-remote -h
    多屏切换神器
    邀请推广:Office 365 开发入门指南教程
  • 原文地址:https://www.cnblogs.com/PurpleTide/p/2182877.html
Copyright © 2011-2022 走看看