Microsoft.VisualC 命名空间包含支持用 c + + 语言的代码生成和编译的类。 混合编程中使用COM接口指针 Microsoft.VisualC.StlClr
Microsoft.VisualC 命名空间包含支持用 c + + 语言的代码生成和编译的类。
Unmanaged Code 和 Managed Code 混合编程中使用COM接口指针的一种方法 ,大家先看一段代码。
1 private unsafe void Initialize(IDispatch* pObject) 2 { 3 this.m_pdispObject = pObject; 4 **(((int*) pObject))[4](pObject); 5 this.m_eventIID = Guid.Empty; 6 this.m_dwEventCookie = 0; 7 8 9 }
这是使用C#语言写的某Class的一个成员函数内的部分代码。
其中 **(((int*) pObject))[4](pObject); 这一行是关键的一笔,初看起来,很像是早期使用C语言写OLE程序时的情景,至少有异曲同工的感觉。
要理解这一行代码的意思,需要看一下在上下文中IDispatch的定义:
1 [StructLayout(LayoutKind.Sequential, Size=4), MiscellaneousBits(0x41), DebugInfoInPDB, NativeCppClass, CLSCompliant(false)] 2 public static class IDispatch : ValueType 3 { 4 }
这又是在C#语言中定义对应于C++ Class的Object的VPtr的一种非常妙的方法。
至此,就可以完全清楚前面那段代码的意思了:
1 **(((int*) pObject))[4](pObject);
这一行相当于C++中的
1 pObject->AddRef();
至于"[]"操作符中,为什么是4代表了AddRef调用,留给读者自己去理解,结合COM规范中接口的本质、C++ Class的V-Table的布局,不难得到答案。
Unmanaged Code和Managed Code混合编程中,这种使用COM接口指针的方法,虽稍显晦涩,但在不方便使用C++/CLI而必须使用C#的地方,还是很方便的,而且整体来看,这种方式也还是相当优雅的。
Attribute 类 表示自定义特性的基类
1 [SerializableAttribute] 2 [AttributeUsageAttribute(AttributeTargets.All, Inherited = true, 3 AllowMultiple = false)] 4 [ClassInterfaceAttribute(ClassInterfaceType.None)] 5 [ComVisibleAttribute(true)] 6 public abstract class Attribute : _Attribute
名称 | 说明 | |
---|---|---|
Attribute() |
Attribute 类的新实例。 |
名称 | 说明 | |
---|---|---|
|
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。 |
|
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 |
|
_Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。 |
|
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。 |
T:System.Attribute类将预定义的系统信息或用户定义的自定义信息与目标元素。一个目标元素可以是一个组件、类、构造函数、委托、枚举、事件、字段、接口、方法、可执行文件模块、参数、属性,返回值,结构,或另一个属性。
属性提供的信息也称为元数据。元数据可以在运行时检查由应用程序来控制程序如何处理数据,或在运行时由外部工具控制如何处理或维护应用程序本身。例如,在.NET框架预定义类型和使用属性来控制运行时的行为,和一些编程语言中使用的属性类型来表示不直接支持的语言功能。NET框架的通用类型系统。
所有的属性类型派生的直接或间接地从System.Attribute类。属性可应用于任何目标元素;多个属性可以应用于同一目标元素;属性可以由目标元素派生的元素继承。使用T:system.attributetargets类指定目标元素应用该属性。
T:System.Attribute类提供了方便的方法来检索和测试自定义属性。有关使用属性的更多信息,请参见应用属性并使用属性扩展元数据。
下面的代码示例演示如何使用System.Attribute T。
1 using System; 2 using System.Reflection; 3 4 // An enumeration of animals. Start at 1 (0 = uninitialized). 5 public enum Animal { 6 // Pets. 7 Dog = 1, 8 Cat, 9 Bird, 10 } 11 12 // A custom attribute to allow a target to have a pet. 13 public class AnimalTypeAttribute : Attribute { 14 // The constructor is called when the attribute is set. 15 public AnimalTypeAttribute(Animal pet) { 16 thePet = pet; 17 } 18 19 // Keep a variable internally ... 20 protected Animal thePet; 21 22 // .. and show a copy to the outside world. 23 public Animal Pet { 24 get { return thePet; } 25 set { thePet = value; } 26 } 27 } 28 29 // 一个测试类,每种方法都有自己的宠物pet. 30 class AnimalTypeTestClass { 31 [AnimalType(Animal.Dog)] 32 public void DogMethod() {} 33 34 [AnimalType(Animal.Cat)] 35 public void CatMethod() {} 36 37 [AnimalType(Animal.Bird)] 38 public void BirdMethod() {} 39 } 40 41 class DemoClass { 42 static void Main(string[] args) { 43 AnimalTypeTestClass testClass = new AnimalTypeTestClass(); 44 Type type = testClass.GetType(); 45 // Iterate through all the methods of the class. 46 foreach(MethodInfo mInfo in type.GetMethods()) { 47 // Iterate through all the Attributes for each method. 48 foreach (Attribute attr in 49 Attribute.GetCustomAttributes(mInfo)) { 50 // Check for the AnimalType attribute. 51 if (attr.GetType() == typeof(AnimalTypeAttribute)) 52 Console.WriteLine( 53 "Method {0} has a pet {1} attribute.", 54 mInfo.Name, ((AnimalTypeAttribute)attr).Pet); 55 } 56 57 } 58 } 59 } 60 /* 61 * Output: 62 * Method DogMethod has a pet Dog attribute. 63 * Method CatMethod has a pet Cat attribute. 64 * Method BirdMethod has a pet Bird attribute. 65 */