Otherwise, a compile-time error occurs.
20.1.15 The NoIDispatch attribute
The presence of the NoIDispatch attribute indicates that the class or interface should derive from IUnknown rather than IDispatch when exported to COM.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class NoIDispatchAttribute: System.Attribute
{
public NoIDispatchAttribute() {…}
}
20.1.16 The NonSerialized attribute
The presence of the NonSerialized attribute on a field or property indicates that that field or property should not be serialized.
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
259
C# LANGUAGE REFERENCE
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class NonSerializedAttribute: System.Attribute
{
public NonSerializedAttribute() {…}
}
20.1.17 The Predeclared attribute
The presence of the Predeclared attribute denotes a predeclared object imported from COM.
[AttributeUsage(Attribute(AttributeTargets.Class)]
public class PredeclaredAttribute: System.Attribute
{
public PredeclaredAttribute() {…}
}
20.1.18 The ReturnsHResult attribute
The ReturnsHResult attribute is used to mark a method as returning an HRESULT result in COM .
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
public class ReturnsHResultAttribute: System.Attribute
{
public ReturnsHResultAttribute(bool value) {…}
public bool Value { get {…} }
}
A method that is decorated with the ReturnsHResult attribute must not have a body. Thus, the ReturnsHResult attribute may be placed on an interface method or on an extern class methods that have the extern modifier. A compile-time error occurs if any other method declaration includes the ReturnsHResult attribute.
The example
class interface Interface1
{
[ReturnsHResult]
int M(int x, int y);
}
declares that the M method of Interface1 returns an HRESULT. The corresponding COM signature for M is a method that takes three arguments (the two int arguments x and y plus a third argument of type int* that is used for the return value) and returns an HRESULT.
20.1.19 The Serializable attribute
The presence of the Serializable attribute on a class indicates that the class can be serialized..
[AttributeUsage(AttributeTargets.Class
| AttributeTargets.Delegate
| AttributeTargets.Enum
| AttributeTargets.Struct)]
public class SerializableAttribute: System.Attribute
{
public SerializableAttribute() {…}
}
20.1.20 The StructLayout attribute
The StructLayout attribute is used to specify the layout of fields for the struct.
260
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 20 Interoperability
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class StructLayoutAttribute: System.Attribute
{
public StructLayoutAttribute(LayoutKind kind) {…}
public CharSet CharSet;
public int Pack;
public LayoutKind StructLayoutKind { get {…} }
}
The StructLayout attribute has the following behaviors:
• It can only be placed struct declarations.
• It has a positional parameter of type Layout.
• It has three named parameters:
• The CharSet named parameter indicates the default character set for containing char and string types. The default is CharSet.Auto.
• The Pack named parameter indicates the packing size, in bytes. The packing size must be a power of two. The default packing size is 4.
• It is a single-use attribute class.
If LayoutKind.Explicit is specified, then every field in the struct must have the StructOffset attribute.
If LayoutKind.Explicit is not specified, then use of the StructOffset attribute is prohibited.
20.1.21 The StructOffset attribute
The StructOffset attribute is used to specify the layout of fields for the struct.
[AttributeUsage(AttributeTargets.Field)]
public class StructOffsetAttribute: System.Attribute
{
public StructOffsetAttribute(int offset) {…}
}
The StructOffset attribute may not be placed on a field declarations that is a member of a class.
20.1.22 The TypeLibFunc attribute
The TypeLibFunc attribute is used to specify typelib flags, for interoperability with COM.
[AttributeUsage(AttributeTargets.Method)]
public class TypeLibFuncAttribute: System.Attribute
{
public TypeLibFuncAttribute(short value) {…}
public short Value { get {…} }
}
20.1.23 The TypeLibType attribute
The TypeLibType attribute is used to specify typelib flags, for interoperability with COM.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class TypeLibTypeAttribute: System.Attribute
{
public TypeLibTypeAttribute(short value) {…}
public short Value { get {…} }
}
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
261
C# LANGUAGE REFERENCE
20.1.24 The TypeLibVar attribute
The TypeLibVar attribute is used to specify typelib flags, for interoperability with COM.
[AttributeUsage(AttributeTargets.Field)]
public class TypeLibVarAttribute: System.Attribute
{
public TypeLibVarAttribute(short value) {…}
public short Value { get {…} }
}
20.2 Supporting enums
namespace System.Interop {
public enum CallingConvention
{
WinAPI = 1,
Cdecl = 2,
Stdcall = 3,
Thiscall = 4,
Fastcall = 5
}
public enum CharSet
{
None
Auto,
Ansi,
Unicode
}
public enum ComInterfaceType
{
Dual = 0,
IUnknown = 1,
IDispatch = 2,
}
|