Microsoft.VisualC 命名空间包含支持用 c + + 语言的代码生成和编译的类。 混合编程中使用COM接口指针

Microsoft.VisualC 命名空间包含支持用 c + + 语言的代码生成和编译的类。

Microsoft.VisualC.StlClr 

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
 名称说明
System_CAPS_protmethod Attribute()

初始化 Attribute 类的新实例。

属性
 
 
 名称说明
System_CAPS_pubproperty TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

方法
 
 
 名称说明
System_CAPS_pubmethod Equals(Object)

此 API 支持 产品 基础结构,不应从代码直接使用。返回一个值,该值指示此实例是否与指定的对象相等。(覆盖 Object.Equals(Object)。)

System_CAPS_protmethod Finalize()

在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(继承自 Object。)

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttribute(Assembly, Type)

检索应用于指定的程序集的自定义特性。 参数指定要搜索的程序集和自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttribute(Assembly, Type, Boolean)

检索应用于程序集的自定义属性。 参数指定的程序集、 自定义特性来进行搜索,并忽略的搜索选项的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttribute(MemberInfo, Type)

检索应用于类型的成员的自定义特性。 参数指定该成员和要搜索的自定义属性的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttribute(MemberInfo, Type, Boolean)

检索应用于类型的成员的自定义特性。 参数指定该成员、 自定义属性来进行搜索,以及是否要搜索的成员的祖先的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttribute(Module, Type)

检索应用于模块的自定义属性。 参数指定该模块,以及要搜索的自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttribute(Module, Type, Boolean)

检索应用于某个模块的自定义特性。 参数指定模块、 类型的自定义属性来进行搜索,并忽略的搜索选项。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttribute(ParameterInfo, Type)

检索应用于方法参数的自定义特性。 参数指定的方法参数,以及要搜索的自定义属性的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttribute(ParameterInfo, Type, Boolean)

检索应用于方法参数的自定义特性。 参数指定的方法参数、 自定义属性来进行搜索,以及是否要搜索祖先构成的方法参数的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(Assembly)

检索一个数组,它由应用于程序集的自定义属性组成。 参数指定程序集。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(Assembly, Boolean)

检索一个数组,它由应用于程序集的自定义属性组成。 参数指定该程序集,并忽略的搜索选项。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(Assembly, Type)

检索一个数组,它由应用于程序集的自定义属性组成。 参数指定要搜索的程序集和自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(Assembly, Type, Boolean)

检索一个数组,它由应用于程序集的自定义属性组成。 参数指定的程序集、 类型的自定义属性来进行搜索,并忽略的搜索选项。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(MemberInfo)

检索应用于类型的成员的自定义特性的数组。 参数指定的成员。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(MemberInfo, Boolean)

检索应用于类型的成员的自定义特性的数组。 参数指定该成员、 自定义属性来进行搜索,以及是否要搜索的成员的祖先的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(MemberInfo, Type)

检索应用于类型的成员的自定义特性的数组。 参数指定该成员和要搜索的自定义属性的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(MemberInfo, Type, Boolean)

检索应用于类型的成员的自定义特性的数组。 参数指定该成员、 自定义属性来进行搜索,以及是否要搜索的成员的祖先的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(Module)

检索应用于模块的自定义特性的数组。 参数指定的模块。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(Module, Boolean)

检索应用于模块的自定义特性的数组。 参数指定该模块,并忽略的搜索选项。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(Module, Type)

检索应用于模块的自定义特性的数组。 参数指定该模块,以及要搜索的自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(Module, Type, Boolean)

检索应用于模块的自定义特性的数组。 参数指定该模块、 自定义特性来进行搜索,并忽略的搜索选项的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(ParameterInfo)

检索应用于方法参数的自定义特性的数组。 一个参数指定的方法参数。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(ParameterInfo, Boolean)

检索应用于方法参数的自定义特性的数组。 参数指定的方法参数,以及是否搜索方法参数的祖先。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(ParameterInfo, Type)

检索应用于方法参数的自定义特性的数组。 参数指定的方法参数,以及要搜索的自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static GetCustomAttributes(ParameterInfo, Type, Boolean)

检索应用于方法参数的自定义特性的数组。 参数指定的方法参数、 自定义特性来进行搜索,以及是否要搜索的方法参数的祖先的类型。

System_CAPS_pubmethod GetHashCode()

返回此实例的哈希代码。(覆盖 Object.GetHashCode()。)

System_CAPS_pubmethod GetType()

获取当前实例的 Type。(继承自 Object。)

System_CAPS_pubmethod IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

System_CAPS_pubmethodSystem_CAPS_static IsDefined(Assembly, Type)

确定是否将任何自定义特性应用于程序集。 参数指定要搜索的程序集和自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static IsDefined(Assembly, Type, Boolean)

确定是否将任何自定义特性应用于程序集。 参数指定的程序集、 自定义特性来进行搜索,并忽略的搜索选项的类型。

System_CAPS_pubmethodSystem_CAPS_static IsDefined(MemberInfo, Type)

确定是否将任何自定义特性应用于类型的成员。 参数指定要搜索的成员和自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static IsDefined(MemberInfo, Type, Boolean)

确定是否将任何自定义特性应用于类型的成员。 参数指定该成员、 自定义属性来进行搜索,以及是否要搜索的成员的祖先的类型。

System_CAPS_pubmethodSystem_CAPS_static IsDefined(Module, Type)

确定是否将具有指定任何的类型自定义特性应用于模块。 参数指定该模块,以及要搜索的自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static IsDefined(Module, Type, Boolean)

确定是否将任何自定义特性应用于模块。 参数指定该模块、 自定义特性来进行搜索,并忽略的搜索选项的类型。

System_CAPS_pubmethodSystem_CAPS_static IsDefined(ParameterInfo, Type)

确定是否将任何自定义特性应用于方法参数。 参数指定的方法参数,以及要搜索的自定义特性的类型。

System_CAPS_pubmethodSystem_CAPS_static IsDefined(ParameterInfo, Type, Boolean)

确定是否将任何自定义特性应用于方法参数。 参数指定的方法参数、 自定义特性来进行搜索,以及是否要搜索的方法参数的祖先的类型。

System_CAPS_pubmethod Match(Object)

当在派生类中重写,则返回一个值,该值指示此实例是否等于指定的对象。

System_CAPS_protmethod MemberwiseClone()

创建当前 Object 的浅表副本。(继承自 Object。)

System_CAPS_pubmethod ToString()

返回表示当前对象的字符串。(继承自 Object。)

 名称说明
System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

System_CAPS_pubinterfaceSystem_CAPS_privmethod _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  */
原文地址:https://www.cnblogs.com/endv/p/6810847.html