[bcl] More netstandard api matching
[mono-project.git] / mcs / class / referencesource / mscorlib / system / runtime / interopservices / attributes.cs
blob17068f63bc9e4ad9cabf831cfc9067ba21bac739
1 #if MONO_COM
2 #define FEATURE_COMINTEROP
3 #endif
4 // ==++==
5 //
6 // Copyright (c) Microsoft Corporation. All rights reserved.
7 //
8 // ==--==
9 ////////////////////////////////////////////////////////////////////////////////
10 ////////////////////////////////////////////////////////////////////////////////
11 namespace System.Runtime.InteropServices{
13 using System;
14 using System.Reflection;
15 using System.Diagnostics.Contracts;
17 [AttributeUsage(AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
18 [System.Runtime.InteropServices.ComVisible(true)]
19 public sealed class UnmanagedFunctionPointerAttribute : Attribute
21 CallingConvention m_callingConvention;
23 public UnmanagedFunctionPointerAttribute(CallingConvention callingConvention) { m_callingConvention = callingConvention; }
25 public CallingConvention CallingConvention { get { return m_callingConvention; } }
27 public CharSet CharSet;
28 public bool BestFitMapping;
29 public bool ThrowOnUnmappableChar;
31 // This field is ignored and marshaling behaves as if it was true (for historical reasons).
32 public bool SetLastError;
34 // P/Invoke via delegate always preserves signature, HRESULT swapping is not supported.
35 //public bool PreserveSig;
38 [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
39 [System.Runtime.InteropServices.ComVisible(false)]
40 public sealed class TypeIdentifierAttribute : Attribute
42 public TypeIdentifierAttribute() { }
43 public TypeIdentifierAttribute(string scope, string identifier) { Scope_ = scope; Identifier_ = identifier; }
45 public String Scope { get { return Scope_; } }
46 public String Identifier { get { return Identifier_; } }
48 internal String Scope_;
49 internal String Identifier_;
52 // To be used on methods that sink reverse P/Invoke calls.
53 // This attribute is a CoreCLR-only security measure, currently ignored by the desktop CLR.
54 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
55 public sealed class AllowReversePInvokeCallsAttribute : Attribute
57 public AllowReversePInvokeCallsAttribute()
62 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event, Inherited = false)]
63 [System.Runtime.InteropServices.ComVisible(true)]
64 public sealed class DispIdAttribute : Attribute
66 internal int _val;
67 public DispIdAttribute(int dispId)
69 _val = dispId;
71 public int Value { get { return _val; } }
74 [Serializable]
75 [System.Runtime.InteropServices.ComVisible(true)]
76 public enum ComInterfaceType
78 InterfaceIsDual = 0,
79 InterfaceIsIUnknown = 1,
80 InterfaceIsIDispatch = 2,
82 [System.Runtime.InteropServices.ComVisible(false)]
83 InterfaceIsIInspectable = 3,
86 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
87 [System.Runtime.InteropServices.ComVisible(true)]
88 public sealed class InterfaceTypeAttribute : Attribute
90 internal ComInterfaceType _val;
91 public InterfaceTypeAttribute(ComInterfaceType interfaceType)
93 _val = interfaceType;
95 public InterfaceTypeAttribute(short interfaceType)
97 _val = (ComInterfaceType)interfaceType;
99 public ComInterfaceType Value { get { return _val; } }
102 [AttributeUsage(AttributeTargets.Class, Inherited = false)]
103 [System.Runtime.InteropServices.ComVisible(true)]
104 public sealed class ComDefaultInterfaceAttribute : Attribute
106 internal Type _val;
108 public ComDefaultInterfaceAttribute(Type defaultInterface)
110 _val = defaultInterface;
113 public Type Value { get { return _val; } }
116 [Serializable]
117 [System.Runtime.InteropServices.ComVisible(true)]
118 public enum ClassInterfaceType
120 None = 0,
121 AutoDispatch = 1,
122 AutoDual = 2
125 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, Inherited = false)]
126 [System.Runtime.InteropServices.ComVisible(true)]
127 public sealed class ClassInterfaceAttribute : Attribute
129 internal ClassInterfaceType _val;
130 public ClassInterfaceAttribute(ClassInterfaceType classInterfaceType)
132 _val = classInterfaceType;
135 public ClassInterfaceAttribute(short classInterfaceType)
137 _val = (ClassInterfaceType)classInterfaceType;
139 public ClassInterfaceType Value { get { return _val; } }
142 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Delegate | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
143 [System.Runtime.InteropServices.ComVisible(true)]
144 public sealed class ComVisibleAttribute : Attribute
146 internal bool _val;
147 public ComVisibleAttribute(bool visibility)
149 _val = visibility;
151 public bool Value { get { return _val; } }
154 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
155 [System.Runtime.InteropServices.ComVisible(true)]
156 public sealed class TypeLibImportClassAttribute : Attribute
158 internal String _importClassName;
159 public TypeLibImportClassAttribute(Type importClass)
161 _importClassName = importClass.ToString();
163 public String Value { get { return _importClassName; } }
166 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
167 [System.Runtime.InteropServices.ComVisible(true)]
168 public sealed class LCIDConversionAttribute : Attribute
170 internal int _val;
171 public LCIDConversionAttribute(int lcid)
173 _val = lcid;
175 public int Value { get {return _val;} }
178 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
179 [System.Runtime.InteropServices.ComVisible(true)]
180 public sealed class ComRegisterFunctionAttribute : Attribute
182 public ComRegisterFunctionAttribute()
187 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
188 [System.Runtime.InteropServices.ComVisible(true)]
189 public sealed class ComUnregisterFunctionAttribute : Attribute
191 public ComUnregisterFunctionAttribute()
196 [AttributeUsage(AttributeTargets.Class, Inherited = false)]
197 [System.Runtime.InteropServices.ComVisible(true)]
198 public sealed class ProgIdAttribute : Attribute
200 internal String _val;
201 public ProgIdAttribute(String progId)
203 _val = progId;
205 public String Value { get {return _val;} }
208 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
209 [System.Runtime.InteropServices.ComVisible(true)]
210 public sealed class ImportedFromTypeLibAttribute : Attribute
212 internal String _val;
213 public ImportedFromTypeLibAttribute(String tlbFile)
215 _val = tlbFile;
217 public String Value { get {return _val;} }
220 [Obsolete("The IDispatchImplAttribute is deprecated.", false)]
221 [Serializable]
222 [System.Runtime.InteropServices.ComVisible(true)]
223 public enum IDispatchImplType
225 SystemDefinedImpl = 0,
226 InternalImpl = 1,
227 CompatibleImpl = 2,
230 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, Inherited = false)]
231 [Obsolete("This attribute is deprecated and will be removed in a future version.", false)]
232 [System.Runtime.InteropServices.ComVisible(true)]
233 public sealed class IDispatchImplAttribute : Attribute
235 internal IDispatchImplType _val;
236 public IDispatchImplAttribute(IDispatchImplType implType)
238 _val = implType;
240 public IDispatchImplAttribute(short implType)
242 _val = (IDispatchImplType)implType;
244 public IDispatchImplType Value { get {return _val;} }
247 [AttributeUsage(AttributeTargets.Class, Inherited = true)]
248 [System.Runtime.InteropServices.ComVisible(true)]
249 public sealed class ComSourceInterfacesAttribute : Attribute
251 internal String _val;
252 public ComSourceInterfacesAttribute(String sourceInterfaces)
254 _val = sourceInterfaces;
256 public ComSourceInterfacesAttribute(Type sourceInterface)
258 _val = sourceInterface.FullName;
260 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2)
262 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName;
264 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3)
266 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName;
268 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3, Type sourceInterface4)
270 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName + "\0" + sourceInterface4.FullName;
272 public String Value { get {return _val;} }
275 #if FEATURE_COMINTEROP || MOBILE_LEGACY
276 [AttributeUsage(AttributeTargets.All, Inherited = false)]
277 [System.Runtime.InteropServices.ComVisible(true)]
278 public sealed class ComConversionLossAttribute : Attribute
280 public ComConversionLossAttribute()
284 #endif
285 #if FEATURE_COMINTEROP || MOBILE_LEGACY
286 [Serializable]
287 [Flags()]
288 [System.Runtime.InteropServices.ComVisible(true)]
289 public enum TypeLibTypeFlags
291 FAppObject = 0x0001,
292 FCanCreate = 0x0002,
293 FLicensed = 0x0004,
294 FPreDeclId = 0x0008,
295 FHidden = 0x0010,
296 FControl = 0x0020,
297 FDual = 0x0040,
298 FNonExtensible = 0x0080,
299 FOleAutomation = 0x0100,
300 FRestricted = 0x0200,
301 FAggregatable = 0x0400,
302 FReplaceable = 0x0800,
303 FDispatchable = 0x1000,
304 FReverseBind = 0x2000,
307 [Serializable]
308 [Flags()]
309 [System.Runtime.InteropServices.ComVisible(true)]
310 public enum TypeLibFuncFlags
312 FRestricted = 0x0001,
313 FSource = 0x0002,
314 FBindable = 0x0004,
315 FRequestEdit = 0x0008,
316 FDisplayBind = 0x0010,
317 FDefaultBind = 0x0020,
318 FHidden = 0x0040,
319 FUsesGetLastError = 0x0080,
320 FDefaultCollelem = 0x0100,
321 FUiDefault = 0x0200,
322 FNonBrowsable = 0x0400,
323 FReplaceable = 0x0800,
324 FImmediateBind = 0x1000,
327 [Serializable]
328 [Flags()]
329 [System.Runtime.InteropServices.ComVisible(true)]
330 public enum TypeLibVarFlags
332 FReadOnly = 0x0001,
333 FSource = 0x0002,
334 FBindable = 0x0004,
335 FRequestEdit = 0x0008,
336 FDisplayBind = 0x0010,
337 FDefaultBind = 0x0020,
338 FHidden = 0x0040,
339 FRestricted = 0x0080,
340 FDefaultCollelem = 0x0100,
341 FUiDefault = 0x0200,
342 FNonBrowsable = 0x0400,
343 FReplaceable = 0x0800,
344 FImmediateBind = 0x1000,
347 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Struct, Inherited = false)]
348 [System.Runtime.InteropServices.ComVisible(true)]
349 public sealed class TypeLibTypeAttribute : Attribute
351 internal TypeLibTypeFlags _val;
352 public TypeLibTypeAttribute(TypeLibTypeFlags flags)
354 _val = flags;
356 public TypeLibTypeAttribute(short flags)
358 _val = (TypeLibTypeFlags)flags;
360 public TypeLibTypeFlags Value { get {return _val;} }
363 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
364 [System.Runtime.InteropServices.ComVisible(true)]
365 public sealed class TypeLibFuncAttribute : Attribute
367 internal TypeLibFuncFlags _val;
368 public TypeLibFuncAttribute(TypeLibFuncFlags flags)
370 _val = flags;
372 public TypeLibFuncAttribute(short flags)
374 _val = (TypeLibFuncFlags)flags;
376 public TypeLibFuncFlags Value { get {return _val;} }
379 [AttributeUsage(AttributeTargets.Field, Inherited = false)]
380 [System.Runtime.InteropServices.ComVisible(true)]
381 public sealed class TypeLibVarAttribute : Attribute
383 internal TypeLibVarFlags _val;
384 public TypeLibVarAttribute(TypeLibVarFlags flags)
386 _val = flags;
388 public TypeLibVarAttribute(short flags)
390 _val = (TypeLibVarFlags)flags;
392 public TypeLibVarFlags Value { get {return _val;} }
395 [Serializable]
396 [System.Runtime.InteropServices.ComVisible(true)]
397 public enum VarEnum
399 VT_EMPTY = 0,
400 VT_NULL = 1,
401 VT_I2 = 2,
402 VT_I4 = 3,
403 VT_R4 = 4,
404 VT_R8 = 5,
405 VT_CY = 6,
406 VT_DATE = 7,
407 VT_BSTR = 8,
408 VT_DISPATCH = 9,
409 VT_ERROR = 10,
410 VT_BOOL = 11,
411 VT_VARIANT = 12,
412 VT_UNKNOWN = 13,
413 VT_DECIMAL = 14,
414 VT_I1 = 16,
415 VT_UI1 = 17,
416 VT_UI2 = 18,
417 VT_UI4 = 19,
418 VT_I8 = 20,
419 VT_UI8 = 21,
420 VT_INT = 22,
421 VT_UINT = 23,
422 VT_VOID = 24,
423 VT_HRESULT = 25,
424 VT_PTR = 26,
425 VT_SAFEARRAY = 27,
426 VT_CARRAY = 28,
427 VT_USERDEFINED = 29,
428 VT_LPSTR = 30,
429 VT_LPWSTR = 31,
430 VT_RECORD = 36,
431 VT_FILETIME = 64,
432 VT_BLOB = 65,
433 VT_STREAM = 66,
434 VT_STORAGE = 67,
435 VT_STREAMED_OBJECT = 68,
436 VT_STORED_OBJECT = 69,
437 VT_BLOB_OBJECT = 70,
438 VT_CF = 71,
439 VT_CLSID = 72,
440 VT_VECTOR = 0x1000,
441 VT_ARRAY = 0x2000,
442 VT_BYREF = 0x4000
445 [Serializable]
446 [System.Runtime.InteropServices.ComVisible(true)]
447 // Note that this enum should remain in-[....] with the CorNativeType enum in corhdr.h
448 public enum UnmanagedType
450 Bool = 0x2, // 4 byte boolean value (true != 0, false == 0)
452 I1 = 0x3, // 1 byte signed value
454 U1 = 0x4, // 1 byte unsigned value
456 I2 = 0x5, // 2 byte signed value
458 U2 = 0x6, // 2 byte unsigned value
460 I4 = 0x7, // 4 byte signed value
462 U4 = 0x8, // 4 byte unsigned value
464 I8 = 0x9, // 8 byte signed value
466 U8 = 0xa, // 8 byte unsigned value
468 R4 = 0xb, // 4 byte floating point
470 R8 = 0xc, // 8 byte floating point
472 Currency = 0xf, // A currency
474 BStr = 0x13, // OLE Unicode BSTR
476 LPStr = 0x14, // Ptr to SBCS string
478 LPWStr = 0x15, // Ptr to Unicode string
480 LPTStr = 0x16, // Ptr to OS preferred (SBCS/Unicode) string
482 ByValTStr = 0x17, // OS preferred (SBCS/Unicode) inline string (only valid in structs)
484 IUnknown = 0x19, // COM IUnknown pointer.
486 IDispatch = 0x1a, // COM IDispatch pointer
488 Struct = 0x1b, // Structure
490 Interface = 0x1c, // COM interface
492 SafeArray = 0x1d, // OLE SafeArray
494 ByValArray = 0x1e, // Array of fixed size (only valid in structs)
496 SysInt = 0x1f, // Hardware natural sized signed integer
498 SysUInt = 0x20,
500 VBByRefStr = 0x22,
502 AnsiBStr = 0x23, // OLE BSTR containing SBCS characters
504 TBStr = 0x24, // Ptr to OS preferred (SBCS/Unicode) BSTR
506 VariantBool = 0x25, // OLE defined BOOLEAN (2 bytes, true == -1, false == 0)
508 FunctionPtr = 0x26, // Function pointer
510 AsAny = 0x28, // Paired with Object type and does runtime marshalling determination
512 LPArray = 0x2a, // C style array
514 LPStruct = 0x2b, // Pointer to a structure
516 CustomMarshaler = 0x2c,
518 Error = 0x2d,
520 [System.Runtime.InteropServices.ComVisible(false)]
521 IInspectable = 0x2e,
523 [System.Runtime.InteropServices.ComVisible(false)]
524 HString = 0x2f, // Windows Runtime HSTRING
527 #if !MONO
528 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.ReturnValue, Inherited = false)]
529 [System.Runtime.InteropServices.ComVisible(true)]
530 public unsafe sealed class MarshalAsAttribute : Attribute
532 #if MONO
533 internal MarshalAsAttribute Copy ()
535 return (MarshalAsAttribute)this.MemberwiseClone ();
537 #else
538 [System.Security.SecurityCritical] // auto-generated
539 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
541 return GetCustomAttribute(parameter.MetadataToken, parameter.GetRuntimeModule());
544 [System.Security.SecurityCritical] // auto-generated
545 internal static bool IsDefined(RuntimeParameterInfo parameter)
547 return GetCustomAttribute(parameter) != null;
550 [System.Security.SecurityCritical] // auto-generated
551 internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
553 return GetCustomAttribute(field.MetadataToken, field.GetRuntimeModule()); ;
556 [System.Security.SecurityCritical] // auto-generated
557 internal static bool IsDefined(RuntimeFieldInfo field)
559 return GetCustomAttribute(field) != null;
562 [System.Security.SecurityCritical] // auto-generated
563 internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
565 UnmanagedType unmanagedType, arraySubType;
566 VarEnum safeArraySubType;
567 int sizeParamIndex = 0, sizeConst = 0;
568 string marshalTypeName = null, marshalCookie = null, safeArrayUserDefinedTypeName = null;
569 int iidParamIndex = 0;
570 ConstArray nativeType = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);
572 if (nativeType.Length == 0)
573 return null;
575 MetadataImport.GetMarshalAs(nativeType,
576 out unmanagedType, out safeArraySubType, out safeArrayUserDefinedTypeName, out arraySubType, out sizeParamIndex,
577 out sizeConst, out marshalTypeName, out marshalCookie, out iidParamIndex);
579 RuntimeType safeArrayUserDefinedType = safeArrayUserDefinedTypeName == null || safeArrayUserDefinedTypeName.Length == 0 ? null :
580 RuntimeTypeHandle.GetTypeByNameUsingCARules(safeArrayUserDefinedTypeName, scope);
581 RuntimeType marshalTypeRef = null;
585 marshalTypeRef = marshalTypeName == null ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(marshalTypeName, scope);
587 catch (System.TypeLoadException)
589 // The user may have supplied a bad type name string causing this TypeLoadException
590 // Regardless, we return the bad type name
591 Contract.Assert(marshalTypeName != null);
594 return new MarshalAsAttribute(
595 unmanagedType, safeArraySubType, safeArrayUserDefinedType, arraySubType,
596 (short)sizeParamIndex, sizeConst, marshalTypeName, marshalTypeRef, marshalCookie, iidParamIndex);
599 internal MarshalAsAttribute(UnmanagedType val, VarEnum safeArraySubType, RuntimeType safeArrayUserDefinedSubType, UnmanagedType arraySubType,
600 short sizeParamIndex, int sizeConst, string marshalType, RuntimeType marshalTypeRef, string marshalCookie, int iidParamIndex)
602 _val = val;
603 SafeArraySubType = safeArraySubType;
604 SafeArrayUserDefinedSubType = safeArrayUserDefinedSubType;
605 IidParameterIndex = iidParamIndex;
606 ArraySubType = arraySubType;
607 SizeParamIndex = sizeParamIndex;
608 SizeConst = sizeConst;
609 MarshalType = marshalType;
610 MarshalTypeRef = marshalTypeRef;
611 MarshalCookie = marshalCookie;
613 #endif
614 internal UnmanagedType _val;
615 public MarshalAsAttribute(UnmanagedType unmanagedType)
617 _val = unmanagedType;
619 public MarshalAsAttribute(short unmanagedType)
621 _val = (UnmanagedType)unmanagedType;
623 public UnmanagedType Value { get { return _val; } }
625 // Fields used with SubType = SafeArray.
626 public VarEnum SafeArraySubType;
627 public Type SafeArrayUserDefinedSubType;
629 // Field used with iid_is attribute (interface pointers).
630 public int IidParameterIndex;
632 // Fields used with SubType = ByValArray and LPArray.
633 // Array size = parameter(PI) * PM + C
634 public UnmanagedType ArraySubType;
635 public short SizeParamIndex; // param index PI
636 public int SizeConst; // constant C
638 // Fields used with SubType = CustomMarshaler
639 [System.Runtime.InteropServices.ComVisible(true)]
640 public String MarshalType; // Name of marshaler class
641 [System.Runtime.InteropServices.ComVisible(true)]
642 public Type MarshalTypeRef; // Type of marshaler class
643 public String MarshalCookie; // cookie to pass to marshaler
645 #endif
646 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)]
647 [System.Runtime.InteropServices.ComVisible(true)]
648 public sealed class ComImportAttribute : Attribute
650 internal static Attribute GetCustomAttribute(RuntimeType type)
652 if ((type.Attributes & TypeAttributes.Import) == 0)
653 return null;
655 return new ComImportAttribute();
658 internal static bool IsDefined(RuntimeType type)
660 return (type.Attributes & TypeAttributes.Import) != 0;
663 public ComImportAttribute()
668 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Delegate, Inherited = false)]
669 [System.Runtime.InteropServices.ComVisible(true)]
670 public sealed class GuidAttribute : Attribute
672 internal String _val;
673 public GuidAttribute(String guid)
675 _val = guid;
677 public String Value { get { return _val; } }
680 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
681 [System.Runtime.InteropServices.ComVisible(true)]
682 public sealed class PreserveSigAttribute : Attribute
684 internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
686 if ((method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) == 0)
687 return null;
689 return new PreserveSigAttribute();
692 internal static bool IsDefined(RuntimeMethodInfo method)
694 return (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;
697 public PreserveSigAttribute()
702 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
703 [System.Runtime.InteropServices.ComVisible(true)]
704 public sealed class InAttribute : Attribute
706 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
708 return parameter.IsIn ? new InAttribute() : null;
710 internal static bool IsDefined(RuntimeParameterInfo parameter)
712 return parameter.IsIn;
715 public InAttribute()
720 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
721 [System.Runtime.InteropServices.ComVisible(true)]
722 public sealed class OutAttribute : Attribute
724 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
726 return parameter.IsOut ? new OutAttribute() : null;
728 internal static bool IsDefined(RuntimeParameterInfo parameter)
730 return parameter.IsOut;
733 public OutAttribute()
738 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
739 [System.Runtime.InteropServices.ComVisible(true)]
740 public sealed class OptionalAttribute : Attribute
742 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
744 return parameter.IsOptional ? new OptionalAttribute() : null;
746 internal static bool IsDefined(RuntimeParameterInfo parameter)
748 return parameter.IsOptional;
751 public OptionalAttribute()
756 [Flags]
757 public enum DllImportSearchPath
759 UseDllDirectoryForDependencies = 0x100,
760 ApplicationDirectory = 0x200,
761 UserDirectories = 0x400,
762 System32 = 0x800,
763 SafeDirectories = 0x1000,
764 AssemblyDirectory = 0x2,
765 LegacyBehavior = 0x0
768 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Method, AllowMultiple = false)]
769 [System.Runtime.InteropServices.ComVisible(false)]
770 public sealed class DefaultDllImportSearchPathsAttribute : Attribute
772 internal DllImportSearchPath _paths;
773 public DefaultDllImportSearchPathsAttribute(DllImportSearchPath paths)
775 _paths = paths;
778 public DllImportSearchPath Paths { get { return _paths; } }
781 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
782 [System.Runtime.InteropServices.ComVisible(true)]
783 public unsafe sealed class DllImportAttribute : Attribute
785 [System.Security.SecurityCritical] // auto-generated
786 internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
788 if ((method.Attributes & MethodAttributes.PinvokeImpl) == 0)
789 return null;
791 #if !MONO
792 MetadataImport scope = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
793 #endif
794 string entryPoint, dllName = null;
795 int token = method.MetadataToken;
796 PInvokeAttributes flags = 0;
798 #if MONO
799 ((MonoMethod)method).GetPInvoke(out flags, out entryPoint, out dllName);
800 #else
801 scope.GetPInvokeMap(token, out flags, out entryPoint, out dllName);
802 #endif
804 CharSet charSet = CharSet.None;
806 switch (flags & PInvokeAttributes.CharSetMask)
808 case PInvokeAttributes.CharSetNotSpec: charSet = CharSet.None; break;
809 case PInvokeAttributes.CharSetAnsi: charSet = CharSet.Ansi; break;
810 case PInvokeAttributes.CharSetUnicode: charSet = CharSet.Unicode; break;
811 case PInvokeAttributes.CharSetAuto: charSet = CharSet.Auto; break;
813 // Invalid: default to CharSet.None
814 default: break;
817 CallingConvention callingConvention = CallingConvention.Cdecl;
819 switch (flags & PInvokeAttributes.CallConvMask)
821 case PInvokeAttributes.CallConvWinapi: callingConvention = CallingConvention.Winapi; break;
822 case PInvokeAttributes.CallConvCdecl: callingConvention = CallingConvention.Cdecl; break;
823 case PInvokeAttributes.CallConvStdcall: callingConvention = CallingConvention.StdCall; break;
824 case PInvokeAttributes.CallConvThiscall: callingConvention = CallingConvention.ThisCall; break;
825 case PInvokeAttributes.CallConvFastcall: callingConvention = CallingConvention.FastCall; break;
827 // Invalid: default to CallingConvention.Cdecl
828 default: break;
831 bool exactSpelling = (flags & PInvokeAttributes.NoMangle) != 0;
832 bool setLastError = (flags & PInvokeAttributes.SupportsLastError) != 0;
833 bool bestFitMapping = (flags & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
834 bool throwOnUnmappableChar = (flags & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
835 bool preserveSig = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;
837 return new DllImportAttribute(
838 dllName, entryPoint, charSet, exactSpelling, setLastError, preserveSig,
839 callingConvention, bestFitMapping, throwOnUnmappableChar);
842 internal static bool IsDefined(RuntimeMethodInfo method)
844 return (method.Attributes & MethodAttributes.PinvokeImpl) != 0;
848 internal DllImportAttribute(
849 string dllName, string entryPoint, CharSet charSet, bool exactSpelling, bool setLastError, bool preserveSig,
850 CallingConvention callingConvention, bool bestFitMapping, bool throwOnUnmappableChar)
852 _val = dllName;
853 EntryPoint = entryPoint;
854 CharSet = charSet;
855 ExactSpelling = exactSpelling;
856 SetLastError = setLastError;
857 PreserveSig = preserveSig;
858 CallingConvention = callingConvention;
859 BestFitMapping = bestFitMapping;
860 ThrowOnUnmappableChar = throwOnUnmappableChar;
863 internal String _val;
865 public DllImportAttribute(String dllName)
867 _val = dllName;
869 public String Value { get { return _val; } }
871 public String EntryPoint;
872 public CharSet CharSet;
873 public bool SetLastError;
874 public bool ExactSpelling;
875 public bool PreserveSig;
876 public CallingConvention CallingConvention;
877 public bool BestFitMapping;
878 public bool ThrowOnUnmappableChar;
882 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
883 [System.Runtime.InteropServices.ComVisible(true)]
884 public unsafe sealed class StructLayoutAttribute : Attribute
886 private const int DEFAULT_PACKING_SIZE = 8;
888 [System.Security.SecurityCritical] // auto-generated
889 internal static StructLayoutAttribute GetCustomAttribute(RuntimeType type)
891 if (!IsDefined(type))
892 return null;
894 int pack = 0, size = 0;
895 LayoutKind layoutKind = LayoutKind.Auto;
896 switch (type.Attributes & TypeAttributes.LayoutMask)
898 case TypeAttributes.ExplicitLayout: layoutKind = LayoutKind.Explicit; break;
899 case TypeAttributes.AutoLayout: layoutKind = LayoutKind.Auto; break;
900 case TypeAttributes.SequentialLayout: layoutKind = LayoutKind.Sequential; break;
901 default: Contract.Assume(false); break;
904 CharSet charSet = CharSet.None;
905 switch (type.Attributes & TypeAttributes.StringFormatMask)
907 case TypeAttributes.AnsiClass: charSet = CharSet.Ansi; break;
908 case TypeAttributes.AutoClass: charSet = CharSet.Auto; break;
909 case TypeAttributes.UnicodeClass: charSet = CharSet.Unicode; break;
910 default: Contract.Assume(false); break;
913 #if MONO
914 type.GetPacking (out pack, out size);
915 #else
916 type.GetRuntimeModule().MetadataImport.GetClassLayout(type.MetadataToken, out pack, out size);
917 #endif
919 // Metadata parameter checking should not have allowed 0 for packing size.
920 // The runtime later converts a packing size of 0 to 8 so do the same here
921 // because it's more useful from a user perspective.
922 if (pack == 0)
923 pack = DEFAULT_PACKING_SIZE;
925 return new StructLayoutAttribute(layoutKind, pack, size, charSet);
928 internal static bool IsDefined(RuntimeType type)
930 if (type.IsInterface || type.HasElementType || type.IsGenericParameter)
931 return false;
933 return true;
936 internal LayoutKind _val;
938 internal StructLayoutAttribute(LayoutKind layoutKind, int pack, int size, CharSet charSet)
940 _val = layoutKind;
941 Pack = pack;
942 Size = size;
943 CharSet = charSet;
946 public StructLayoutAttribute(LayoutKind layoutKind)
948 _val = layoutKind;
950 public StructLayoutAttribute(short layoutKind)
952 _val = (LayoutKind)layoutKind;
954 public LayoutKind Value { get { return _val; } }
955 public int Pack;
956 public int Size;
957 public CharSet CharSet;
960 [AttributeUsage(AttributeTargets.Field, Inherited = false)]
961 [System.Runtime.InteropServices.ComVisible(true)]
962 public unsafe sealed class FieldOffsetAttribute : Attribute
964 [System.Security.SecurityCritical] // auto-generated
965 internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
967 int fieldOffset;
969 if (field.DeclaringType != null &&
970 #if MONO
971 (fieldOffset = field.GetFieldOffset ()) >= 0)
972 #else
973 field.GetRuntimeModule().MetadataImport.GetFieldOffset(field.DeclaringType.MetadataToken, field.MetadataToken, out fieldOffset))
974 #endif
975 return new FieldOffsetAttribute(fieldOffset);
977 return null;
980 [System.Security.SecurityCritical] // auto-generated
981 internal static bool IsDefined(RuntimeFieldInfo field)
983 return GetCustomAttribute(field) != null;
986 internal int _val;
987 public FieldOffsetAttribute(int offset)
989 _val = offset;
991 public int Value { get { return _val; } }
994 #if FEATURE_COMINTEROP || MOBILE_LEGACY
995 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
996 [System.Runtime.InteropServices.ComVisible(true)]
997 public sealed class ComAliasNameAttribute : Attribute
999 internal String _val;
1000 public ComAliasNameAttribute(String alias)
1002 _val = alias;
1004 public String Value { get {return _val;} }
1007 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)]
1008 [System.Runtime.InteropServices.ComVisible(true)]
1009 public sealed class AutomationProxyAttribute : Attribute
1011 internal bool _val;
1012 public AutomationProxyAttribute(bool val)
1014 _val = val;
1016 public bool Value { get {return _val;} }
1019 [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
1020 [System.Runtime.InteropServices.ComVisible(true)]
1021 public sealed class PrimaryInteropAssemblyAttribute : Attribute
1023 internal int _major;
1024 internal int _minor;
1026 public PrimaryInteropAssemblyAttribute(int major, int minor)
1028 _major = major;
1029 _minor = minor;
1032 public int MajorVersion { get {return _major;} }
1033 public int MinorVersion { get {return _minor;} }
1036 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
1037 [System.Runtime.InteropServices.ComVisible(true)]
1038 public sealed class CoClassAttribute : Attribute
1040 internal Type _CoClass;
1042 public CoClassAttribute(Type coClass)
1044 _CoClass = coClass;
1047 public Type CoClass { get { return _CoClass; } }
1050 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
1051 [System.Runtime.InteropServices.ComVisible(true)]
1052 public sealed class ComEventInterfaceAttribute : Attribute
1054 internal Type _SourceInterface;
1055 internal Type _EventProvider;
1057 public ComEventInterfaceAttribute(Type SourceInterface, Type EventProvider)
1059 _SourceInterface = SourceInterface;
1060 _EventProvider = EventProvider;
1063 public Type SourceInterface { get {return _SourceInterface;} }
1064 public Type EventProvider { get {return _EventProvider;} }
1066 #endif
1067 #if FEATURE_COMINTEROP || MOBILE_LEGACY
1068 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1069 [System.Runtime.InteropServices.ComVisible(true)]
1070 public sealed class TypeLibVersionAttribute : Attribute
1072 internal int _major;
1073 internal int _minor;
1075 public TypeLibVersionAttribute(int major, int minor)
1077 _major = major;
1078 _minor = minor;
1081 public int MajorVersion { get {return _major;} }
1082 public int MinorVersion { get {return _minor;} }
1085 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1086 [System.Runtime.InteropServices.ComVisible(true)]
1087 public sealed class ComCompatibleVersionAttribute : Attribute
1089 internal int _major;
1090 internal int _minor;
1091 internal int _build;
1092 internal int _revision;
1094 public ComCompatibleVersionAttribute(int major, int minor, int build, int revision)
1096 _major = major;
1097 _minor = minor;
1098 _build = build;
1099 _revision = revision;
1102 public int MajorVersion { get {return _major;} }
1103 public int MinorVersion { get {return _minor;} }
1104 public int BuildNumber { get {return _build;} }
1105 public int RevisionNumber { get {return _revision;} }
1108 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
1109 [System.Runtime.InteropServices.ComVisible(true)]
1110 public sealed class BestFitMappingAttribute : Attribute
1112 internal bool _bestFitMapping;
1114 public BestFitMappingAttribute(bool BestFitMapping)
1116 _bestFitMapping = BestFitMapping;
1119 public bool BestFitMapping { get { return _bestFitMapping; } }
1120 public bool ThrowOnUnmappableChar;
1123 [AttributeUsage(AttributeTargets.Module, Inherited = false)]
1124 [System.Runtime.InteropServices.ComVisible(true)]
1125 public sealed class DefaultCharSetAttribute : Attribute
1127 internal CharSet _CharSet;
1129 public DefaultCharSetAttribute(CharSet charSet)
1131 _CharSet = charSet;
1134 public CharSet CharSet { get { return _CharSet; } }
1137 [Obsolete("This attribute has been deprecated. Application Domains no longer respect Activation Context boundaries in IDispatch calls.", false)]
1138 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1139 [System.Runtime.InteropServices.ComVisible(true)]
1140 public sealed class SetWin32ContextInIDispatchAttribute : Attribute
1142 public SetWin32ContextInIDispatchAttribute()
1147 [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
1148 [System.Runtime.InteropServices.ComVisible(false)]
1149 public sealed class ManagedToNativeComInteropStubAttribute : Attribute
1151 internal Type _classType;
1152 internal String _methodName;
1154 public ManagedToNativeComInteropStubAttribute(Type classType, String methodName)
1156 _classType = classType;
1157 _methodName = methodName;
1160 public Type ClassType { get { return _classType; } }
1161 public String MethodName { get { return _methodName; } }
1163 #endif
1164 #endif