NewSlot and ReuseSlot
2011-09-20 21:55
309 查看
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,继承以后所有父类,接口的槽的位置不变
以下是这两个的定义
ReuseSlot | Indicates that the method will reuse an existing slot in the vtable. This is the default behavior. | |
NewSlot | Indicates that the method always gets a new slot in the vtable. |
(我的理解就是 有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
多继承接口的情况下,还需要进一步分析
相关文章推荐
- Effective C++ Item 16 Use the same form in corresponding uses of new and delete
- [EffectiveC++]item16:Use the same form in corresponding uses of new and delete
- How to use re-write module of Apache web server 2.2.3 on FC6 with new or refurbi
- Visual C++ Application: How to use manifests and re-distributable assemblies?
- Starting a Gradle Daemon, 5 busy and 1 incompatible and 1 stopped Daemons could not be reused, use --status for details FAILURE: Build failed with an exception. * What went wrong: Could not dispatch
- Knowing When to Use Override and New Keywords (C# Programming Guide)
- Qt How to use connect between incompatible signal and slot
- Item21 Perfer std::make_unique and std::make_shared to direct use of new
- [maven] Use plugin and develop new plugin
- Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model
- Plugin is too old and ANDROID_DAILY_OVERRIDE value is also outdated, please use new value :"eac1b1d3
- Item 16: Use the same form in corresponding uses of new and delete.(Effective C++)
- New-style Signal and Slot Support¶
- It will cause a new session when you use lazy loading collection, and the collection will assicated with the new session and the
- Fedor and New Game
- Li的前期工作Level_Set_Evolution_Without_Re-initialization_A_New_Variational_Formulation
- How do I use locales and resource bundles to internationalize my application?
- Use advanced interactive viewer to move, zoom and record photos, producing a unique sightline flash
- Difference between GWT.create(SomeClass.class) and new SomeClass()
- Good Bye 2016 C. New Year and Rating(模拟)