83a6c0b161dc06d981fb41e8c181b99d62e96dfb
[mono.git] / mcs / class / referencesource / mscorlib / system / runtime / interopservices / attributes.cs
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{
12
13     using System;
14     using System.Reflection;
15     using System.Diagnostics.Contracts;
16
17     [AttributeUsage(AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
18     [System.Runtime.InteropServices.ComVisible(true)]
19     public sealed class UnmanagedFunctionPointerAttribute : Attribute
20     {
21         CallingConvention m_callingConvention;
22
23         public UnmanagedFunctionPointerAttribute(CallingConvention callingConvention) { m_callingConvention = callingConvention; }
24
25         public CallingConvention CallingConvention { get { return m_callingConvention; } }
26
27         public CharSet CharSet;
28         public bool BestFitMapping;
29         public bool ThrowOnUnmappableChar;
30
31         // This field is ignored and marshaling behaves as if it was true (for historical reasons).
32         public bool SetLastError;
33
34         //  P/Invoke via delegate always preserves signature, HRESULT swapping is not supported.
35         //public bool PreserveSig;
36     }
37
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
41     {
42         public TypeIdentifierAttribute() { }
43         public TypeIdentifierAttribute(string scope, string identifier) { Scope_ = scope; Identifier_ = identifier; }
44
45         public String Scope { get { return Scope_; } }
46         public String Identifier { get { return Identifier_; } }
47
48         internal String Scope_;
49         internal String Identifier_;
50     }
51
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
56     {
57         public AllowReversePInvokeCallsAttribute()
58         {
59         }
60     }
61
62     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event, Inherited = false)]
63     [System.Runtime.InteropServices.ComVisible(true)]
64     public sealed class DispIdAttribute : Attribute
65     {
66         internal int _val;
67         public DispIdAttribute(int dispId)
68         {
69             _val = dispId;
70         }
71         public int Value { get { return _val; } }
72     }
73
74     [Serializable]
75     [System.Runtime.InteropServices.ComVisible(true)]
76     public enum ComInterfaceType
77     {
78         InterfaceIsDual = 0,
79         InterfaceIsIUnknown = 1,
80         InterfaceIsIDispatch = 2,
81
82         [System.Runtime.InteropServices.ComVisible(false)]
83         InterfaceIsIInspectable = 3,
84     }
85
86     [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
87     [System.Runtime.InteropServices.ComVisible(true)]
88     public sealed class InterfaceTypeAttribute : Attribute
89     {
90         internal ComInterfaceType _val;
91         public InterfaceTypeAttribute(ComInterfaceType interfaceType)
92         {
93             _val = interfaceType;
94         }
95         public InterfaceTypeAttribute(short interfaceType)
96         {
97             _val = (ComInterfaceType)interfaceType;
98         }
99         public ComInterfaceType Value { get { return _val; } }
100     }
101
102     [AttributeUsage(AttributeTargets.Class, Inherited = false)]
103     [System.Runtime.InteropServices.ComVisible(true)]
104     public sealed class ComDefaultInterfaceAttribute : Attribute
105     {
106         internal Type _val;
107
108         public ComDefaultInterfaceAttribute(Type defaultInterface)
109         {
110             _val = defaultInterface;
111         }
112
113         public Type Value { get { return _val; } }
114     }
115
116     [Serializable]
117     [System.Runtime.InteropServices.ComVisible(true)]
118     public enum ClassInterfaceType
119     {
120         None = 0,
121         AutoDispatch = 1,
122         AutoDual = 2
123     }
124
125     [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, Inherited = false)]
126     [System.Runtime.InteropServices.ComVisible(true)]
127     public sealed class ClassInterfaceAttribute : Attribute
128     {
129         internal ClassInterfaceType _val;
130         public ClassInterfaceAttribute(ClassInterfaceType classInterfaceType)
131         {
132             _val = classInterfaceType;
133
134         }
135         public ClassInterfaceAttribute(short classInterfaceType)
136         {
137             _val = (ClassInterfaceType)classInterfaceType;
138         }
139         public ClassInterfaceType Value { get { return _val; } }
140     }
141
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
145     {
146         internal bool _val;
147         public ComVisibleAttribute(bool visibility)
148         {
149             _val = visibility;
150         }
151         public bool Value { get { return _val; } }
152     }
153
154     [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
155     [System.Runtime.InteropServices.ComVisible(true)]
156     public sealed class TypeLibImportClassAttribute : Attribute
157     {
158         internal String _importClassName;
159         public TypeLibImportClassAttribute(Type importClass)
160         {
161             _importClassName = importClass.ToString();
162         }
163         public String Value { get { return _importClassName; } }
164     }
165
166     [AttributeUsage(AttributeTargets.Method, Inherited = false)] 
167     [System.Runtime.InteropServices.ComVisible(true)]
168     public sealed class LCIDConversionAttribute : Attribute
169     {
170         internal int _val;
171         public LCIDConversionAttribute(int lcid)
172         {
173             _val = lcid;
174         }
175         public int Value { get {return _val;} } 
176     }
177
178     [AttributeUsage(AttributeTargets.Method, Inherited = false)] 
179     [System.Runtime.InteropServices.ComVisible(true)]
180     public sealed class ComRegisterFunctionAttribute : Attribute
181     {
182         public ComRegisterFunctionAttribute()
183         {
184         }
185     }
186
187     [AttributeUsage(AttributeTargets.Method, Inherited = false)] 
188     [System.Runtime.InteropServices.ComVisible(true)]
189     public sealed class ComUnregisterFunctionAttribute : Attribute
190     {
191         public ComUnregisterFunctionAttribute()
192         {
193         }
194     }
195
196     [AttributeUsage(AttributeTargets.Class, Inherited = false)] 
197     [System.Runtime.InteropServices.ComVisible(true)]
198     public sealed class ProgIdAttribute : Attribute
199     {
200         internal String _val;
201         public ProgIdAttribute(String progId)
202         {
203             _val = progId;
204         }
205         public String Value { get {return _val;} }  
206     }
207     
208     [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] 
209     [System.Runtime.InteropServices.ComVisible(true)]
210     public sealed class ImportedFromTypeLibAttribute : Attribute
211     {
212         internal String _val;
213         public ImportedFromTypeLibAttribute(String tlbFile)
214         {
215             _val = tlbFile;
216         }
217         public String Value { get {return _val;} }
218     }
219
220     [Obsolete("The IDispatchImplAttribute is deprecated.", false)]
221     [Serializable]
222     [System.Runtime.InteropServices.ComVisible(true)]
223     public enum IDispatchImplType
224     {
225         SystemDefinedImpl   = 0,
226         InternalImpl        = 1,
227         CompatibleImpl      = 2,
228     }
229
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
234     {
235         internal IDispatchImplType _val;
236         public IDispatchImplAttribute(IDispatchImplType implType)
237         {
238             _val = implType;
239         }
240         public IDispatchImplAttribute(short implType)
241         {
242             _val = (IDispatchImplType)implType;
243         }
244         public IDispatchImplType Value { get {return _val;} }   
245     }
246
247     [AttributeUsage(AttributeTargets.Class, Inherited = true)] 
248     [System.Runtime.InteropServices.ComVisible(true)]
249     public sealed class ComSourceInterfacesAttribute : Attribute
250     {
251         internal String _val;
252         public ComSourceInterfacesAttribute(String sourceInterfaces)
253         {
254             _val = sourceInterfaces;
255         }
256         public ComSourceInterfacesAttribute(Type sourceInterface)
257         {
258             _val = sourceInterface.FullName;
259         }
260         public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2)
261         {
262             _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName;
263         }
264         public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3)
265         {
266             _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName;
267         }
268         public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3, Type sourceInterface4)
269         {
270             _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName + "\0" + sourceInterface4.FullName;
271         }
272         public String Value { get {return _val;} }  
273     }    
274
275 #if FEATURE_COMINTEROP || MOBILE_LEGACY
276     [AttributeUsage(AttributeTargets.All, Inherited = false)] 
277     [System.Runtime.InteropServices.ComVisible(true)]
278     public sealed class ComConversionLossAttribute : Attribute
279     {
280         public ComConversionLossAttribute()
281         {
282         }
283     }
284 #endif
285 #if FEATURE_COMINTEROP || MOBILE_LEGACY
286 [Serializable]
287 [Flags()]
288     [System.Runtime.InteropServices.ComVisible(true)]
289     public enum TypeLibTypeFlags
290     {
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,
305     }
306     
307 [Serializable]
308 [Flags()]
309     [System.Runtime.InteropServices.ComVisible(true)]
310     public enum TypeLibFuncFlags
311     {   
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,
325     }
326
327 [Serializable]
328 [Flags()]
329     [System.Runtime.InteropServices.ComVisible(true)]
330     public enum TypeLibVarFlags
331     {   
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,
345     }
346
347     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Struct, Inherited = false)]
348     [System.Runtime.InteropServices.ComVisible(true)]
349     public sealed class  TypeLibTypeAttribute : Attribute
350     {
351         internal TypeLibTypeFlags _val;
352         public TypeLibTypeAttribute(TypeLibTypeFlags flags)
353         {
354             _val = flags;
355         }
356         public TypeLibTypeAttribute(short flags)
357         {
358             _val = (TypeLibTypeFlags)flags;
359         }
360         public TypeLibTypeFlags Value { get {return _val;} }    
361     }
362
363     [AttributeUsage(AttributeTargets.Method, Inherited = false)] 
364     [System.Runtime.InteropServices.ComVisible(true)]
365     public sealed class TypeLibFuncAttribute : Attribute
366     {
367         internal TypeLibFuncFlags _val;
368         public TypeLibFuncAttribute(TypeLibFuncFlags flags)
369         {
370             _val = flags;
371         }
372         public TypeLibFuncAttribute(short flags)
373         {
374             _val = (TypeLibFuncFlags)flags;
375         }
376         public TypeLibFuncFlags Value { get {return _val;} }    
377     }
378
379     [AttributeUsage(AttributeTargets.Field, Inherited = false)] 
380     [System.Runtime.InteropServices.ComVisible(true)]
381     public sealed class TypeLibVarAttribute : Attribute
382     {
383         internal TypeLibVarFlags _val;
384         public TypeLibVarAttribute(TypeLibVarFlags flags)
385         {
386             _val = flags;
387         }
388         public TypeLibVarAttribute(short flags)
389         {
390             _val = (TypeLibVarFlags)flags;
391         }
392         public TypeLibVarFlags Value { get {return _val;} } 
393     }   
394
395     [Serializable]
396     [System.Runtime.InteropServices.ComVisible(true)]
397     public enum VarEnum
398     {
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
443     }
444
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
449     {
450         Bool = 0x2,         // 4 byte boolean value (true != 0, false == 0)
451
452         I1 = 0x3,         // 1 byte signed value
453
454         U1 = 0x4,         // 1 byte unsigned value
455
456         I2 = 0x5,         // 2 byte signed value
457
458         U2 = 0x6,         // 2 byte unsigned value
459
460         I4 = 0x7,         // 4 byte signed value
461
462         U4 = 0x8,         // 4 byte unsigned value
463
464         I8 = 0x9,         // 8 byte signed value
465
466         U8 = 0xa,         // 8 byte unsigned value
467
468         R4 = 0xb,         // 4 byte floating point
469
470         R8 = 0xc,         // 8 byte floating point
471
472         Currency = 0xf,         // A currency
473
474         BStr             = 0x13,        // OLE Unicode BSTR
475
476         LPStr = 0x14,        // Ptr to SBCS string
477
478         LPWStr = 0x15,        // Ptr to Unicode string
479
480         LPTStr = 0x16,        // Ptr to OS preferred (SBCS/Unicode) string
481
482         ByValTStr = 0x17,        // OS preferred (SBCS/Unicode) inline string (only valid in structs)
483
484         IUnknown = 0x19,        // COM IUnknown pointer. 
485
486         IDispatch        = 0x1a,        // COM IDispatch pointer
487
488         Struct = 0x1b,        // Structure
489
490         Interface        = 0x1c,        // COM interface
491
492         SafeArray        = 0x1d,        // OLE SafeArray
493
494         ByValArray = 0x1e,        // Array of fixed size (only valid in structs)
495
496         SysInt = 0x1f,        // Hardware natural sized signed integer
497
498         SysUInt = 0x20,
499
500         VBByRefStr       = 0x22,         
501
502         AnsiBStr         = 0x23,        // OLE BSTR containing SBCS characters
503
504         TBStr            = 0x24,        // Ptr to OS preferred (SBCS/Unicode) BSTR
505
506         VariantBool      = 0x25,        // OLE defined BOOLEAN (2 bytes, true == -1, false == 0)
507
508         FunctionPtr = 0x26,        // Function pointer
509
510         AsAny = 0x28,        // Paired with Object type and does runtime marshalling determination
511
512         LPArray = 0x2a,        // C style array
513
514         LPStruct = 0x2b,        // Pointer to a structure
515
516         CustomMarshaler  = 0x2c,        
517
518         Error = 0x2d,
519
520         [System.Runtime.InteropServices.ComVisible(false)]
521         IInspectable     = 0x2e,
522         
523         [System.Runtime.InteropServices.ComVisible(false)]
524         HString          = 0x2f,        // Windows Runtime HSTRING
525
526         [System.Runtime.InteropServices.ComVisible(false)]
527         LPUTF8Str        = 0x30,        // UTF8 string
528     }
529
530 #if !MONO
531     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.ReturnValue, Inherited = false)]
532     [System.Runtime.InteropServices.ComVisible(true)]
533     public unsafe sealed class MarshalAsAttribute : Attribute
534     {
535 #if MONO
536         internal MarshalAsAttribute Copy ()
537         {
538             return (MarshalAsAttribute)this.MemberwiseClone ();
539         }
540 #else
541         [System.Security.SecurityCritical]  // auto-generated
542         internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
543         {
544             return GetCustomAttribute(parameter.MetadataToken, parameter.GetRuntimeModule());
545         }
546
547         [System.Security.SecurityCritical]  // auto-generated
548         internal static bool IsDefined(RuntimeParameterInfo parameter)
549         {
550             return GetCustomAttribute(parameter) != null;
551         }
552
553         [System.Security.SecurityCritical]  // auto-generated
554         internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
555         {
556             return GetCustomAttribute(field.MetadataToken, field.GetRuntimeModule()); ;
557         }
558
559         [System.Security.SecurityCritical]  // auto-generated
560         internal static bool IsDefined(RuntimeFieldInfo field)
561         {
562             return GetCustomAttribute(field) != null;
563         }
564
565         [System.Security.SecurityCritical]  // auto-generated
566         internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
567         {
568             UnmanagedType unmanagedType, arraySubType;
569             VarEnum safeArraySubType;
570             int sizeParamIndex = 0, sizeConst = 0;
571             string marshalTypeName = null, marshalCookie = null, safeArrayUserDefinedTypeName = null;
572             int iidParamIndex = 0;
573             ConstArray nativeType = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);
574
575             if (nativeType.Length == 0)
576                 return null;
577
578             MetadataImport.GetMarshalAs(nativeType,
579                 out unmanagedType, out safeArraySubType, out safeArrayUserDefinedTypeName, out arraySubType, out sizeParamIndex,
580                 out sizeConst, out marshalTypeName, out marshalCookie, out iidParamIndex);
581
582             RuntimeType safeArrayUserDefinedType = safeArrayUserDefinedTypeName == null || safeArrayUserDefinedTypeName.Length == 0 ? null :
583                 RuntimeTypeHandle.GetTypeByNameUsingCARules(safeArrayUserDefinedTypeName, scope);
584             RuntimeType marshalTypeRef = null;
585
586             try
587             {
588                 marshalTypeRef = marshalTypeName == null ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(marshalTypeName, scope);
589             }
590             catch (System.TypeLoadException)
591             {
592                 // The user may have supplied a bad type name string causing this TypeLoadException
593                 // Regardless, we return the bad type name
594                 Contract.Assert(marshalTypeName != null);
595             }
596
597             return new MarshalAsAttribute(
598                 unmanagedType, safeArraySubType, safeArrayUserDefinedType, arraySubType,
599                 (short)sizeParamIndex, sizeConst, marshalTypeName, marshalTypeRef, marshalCookie, iidParamIndex);
600         }
601
602         internal MarshalAsAttribute(UnmanagedType val, VarEnum safeArraySubType, RuntimeType safeArrayUserDefinedSubType, UnmanagedType arraySubType,
603             short sizeParamIndex, int sizeConst, string marshalType, RuntimeType marshalTypeRef, string marshalCookie, int iidParamIndex)
604         {
605             _val = val;
606             SafeArraySubType = safeArraySubType;
607             SafeArrayUserDefinedSubType = safeArrayUserDefinedSubType;
608             IidParameterIndex = iidParamIndex;
609             ArraySubType = arraySubType;
610             SizeParamIndex = sizeParamIndex;
611             SizeConst = sizeConst;
612             MarshalType = marshalType;
613             MarshalTypeRef = marshalTypeRef;
614             MarshalCookie = marshalCookie;
615         }
616 #endif
617         internal UnmanagedType _val;
618         public MarshalAsAttribute(UnmanagedType unmanagedType)
619         {
620             _val = unmanagedType;
621         }
622         public MarshalAsAttribute(short unmanagedType)
623         {
624             _val = (UnmanagedType)unmanagedType;
625         }
626         public UnmanagedType Value { get { return _val; } }
627
628         // Fields used with SubType = SafeArray.
629         public VarEnum            SafeArraySubType;
630         public Type               SafeArrayUserDefinedSubType;
631
632         // Field used with iid_is attribute (interface pointers).
633         public int                IidParameterIndex;
634
635         // Fields used with SubType = ByValArray and LPArray.
636         // Array size =  parameter(PI) * PM + C
637         public UnmanagedType ArraySubType;
638         public short SizeParamIndex;           // param index PI
639         public int SizeConst;                // constant C
640
641         // Fields used with SubType = CustomMarshaler
642         [System.Runtime.InteropServices.ComVisible(true)]
643         public String MarshalType;              // Name of marshaler class
644         [System.Runtime.InteropServices.ComVisible(true)]
645         public Type MarshalTypeRef;           // Type of marshaler class
646         public String MarshalCookie;            // cookie to pass to marshaler
647     }
648 #endif
649     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)]
650     [System.Runtime.InteropServices.ComVisible(true)]
651     public sealed class ComImportAttribute : Attribute
652     {
653         internal static Attribute GetCustomAttribute(RuntimeType type)
654         {
655             if ((type.Attributes & TypeAttributes.Import) == 0)
656                 return null;
657
658             return new ComImportAttribute();
659         }
660
661         internal static bool IsDefined(RuntimeType type)
662         {
663             return (type.Attributes & TypeAttributes.Import) != 0;
664         }
665
666         public ComImportAttribute()
667         {
668         }
669     }
670
671     [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Delegate, Inherited = false)]
672     [System.Runtime.InteropServices.ComVisible(true)]
673     public sealed class GuidAttribute : Attribute
674     {
675         internal String _val;
676         public GuidAttribute(String guid)
677         {
678             _val = guid;
679         }
680         public String Value { get { return _val; } }
681     }
682
683     [AttributeUsage(AttributeTargets.Method, Inherited = false)]
684     [System.Runtime.InteropServices.ComVisible(true)]
685     public sealed class PreserveSigAttribute : Attribute
686     {
687         internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
688         {
689             if ((method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) == 0)
690                 return null;
691
692             return new PreserveSigAttribute();
693         }
694
695         internal static bool IsDefined(RuntimeMethodInfo method)
696         {
697             return (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;
698         }
699
700         public PreserveSigAttribute()
701         {
702         }
703     }
704
705     [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
706     [System.Runtime.InteropServices.ComVisible(true)]
707     public sealed class InAttribute : Attribute
708     {
709         internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
710         {
711             return parameter.IsIn ? new InAttribute() : null;
712         }
713         internal static bool IsDefined(RuntimeParameterInfo parameter)
714         {
715             return parameter.IsIn;
716         }
717
718         public InAttribute()
719         {
720         }
721     }
722
723     [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
724     [System.Runtime.InteropServices.ComVisible(true)]
725     public sealed class OutAttribute : Attribute
726     {
727         internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
728         {
729             return parameter.IsOut ? new OutAttribute() : null;
730         }
731         internal static bool IsDefined(RuntimeParameterInfo parameter)
732         {
733             return parameter.IsOut;
734         }
735
736         public OutAttribute()
737         {
738         }
739     }
740
741     [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
742     [System.Runtime.InteropServices.ComVisible(true)]
743     public sealed class OptionalAttribute : Attribute
744     {
745         internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
746         {
747             return parameter.IsOptional ? new OptionalAttribute() : null;
748         }
749         internal static bool IsDefined(RuntimeParameterInfo parameter)
750         {
751             return parameter.IsOptional;
752         }
753
754         public OptionalAttribute()
755         {
756         }
757     }
758
759     [Flags]
760     public enum DllImportSearchPath
761     {
762         UseDllDirectoryForDependencies = 0x100,
763         ApplicationDirectory = 0x200,
764         UserDirectories = 0x400,
765         System32 = 0x800,
766         SafeDirectories = 0x1000,
767         AssemblyDirectory = 0x2,
768         LegacyBehavior = 0x0
769     }
770
771     [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Method, AllowMultiple = false)]
772     [System.Runtime.InteropServices.ComVisible(false)]
773     public sealed class DefaultDllImportSearchPathsAttribute : Attribute
774     {
775         internal DllImportSearchPath _paths;
776         public DefaultDllImportSearchPathsAttribute(DllImportSearchPath paths)
777         {
778             _paths = paths;
779         }
780
781         public DllImportSearchPath Paths { get { return _paths; } }
782     }
783
784     [AttributeUsage(AttributeTargets.Method, Inherited = false)]
785     [System.Runtime.InteropServices.ComVisible(true)]
786     public unsafe sealed class DllImportAttribute : Attribute
787     {
788         [System.Security.SecurityCritical]  // auto-generated
789         internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
790         {
791             if ((method.Attributes & MethodAttributes.PinvokeImpl) == 0)
792                 return null;
793
794 #if !MONO
795             MetadataImport scope = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
796 #endif
797             string entryPoint, dllName = null;
798             int token = method.MetadataToken;
799             PInvokeAttributes flags = 0;
800
801 #if MONO
802             ((MonoMethod)method).GetPInvoke(out flags, out entryPoint, out dllName);
803 #else
804             scope.GetPInvokeMap(token, out flags, out entryPoint, out dllName);
805 #endif
806
807             CharSet charSet = CharSet.None;
808
809             switch (flags & PInvokeAttributes.CharSetMask)
810             {
811                 case PInvokeAttributes.CharSetNotSpec: charSet = CharSet.None; break;
812                 case PInvokeAttributes.CharSetAnsi: charSet = CharSet.Ansi; break;
813                 case PInvokeAttributes.CharSetUnicode: charSet = CharSet.Unicode; break;
814                 case PInvokeAttributes.CharSetAuto: charSet = CharSet.Auto; break;
815
816                 // Invalid: default to CharSet.None
817                 default: break;
818             }
819
820             CallingConvention callingConvention = CallingConvention.Cdecl;
821
822             switch (flags & PInvokeAttributes.CallConvMask)
823             {
824                 case PInvokeAttributes.CallConvWinapi: callingConvention = CallingConvention.Winapi; break;
825                 case PInvokeAttributes.CallConvCdecl: callingConvention = CallingConvention.Cdecl; break;
826                 case PInvokeAttributes.CallConvStdcall: callingConvention = CallingConvention.StdCall; break;
827                 case PInvokeAttributes.CallConvThiscall: callingConvention = CallingConvention.ThisCall; break;
828                 case PInvokeAttributes.CallConvFastcall: callingConvention = CallingConvention.FastCall; break;
829
830                 // Invalid: default to CallingConvention.Cdecl
831                 default: break;
832             }
833
834             bool exactSpelling = (flags & PInvokeAttributes.NoMangle) != 0;
835             bool setLastError = (flags & PInvokeAttributes.SupportsLastError) != 0;
836             bool bestFitMapping = (flags & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
837             bool throwOnUnmappableChar = (flags & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
838             bool preserveSig = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;
839
840             return new DllImportAttribute(
841                 dllName, entryPoint, charSet, exactSpelling, setLastError, preserveSig,
842                 callingConvention, bestFitMapping, throwOnUnmappableChar);
843         }
844
845         internal static bool IsDefined(RuntimeMethodInfo method)
846         {
847             return (method.Attributes & MethodAttributes.PinvokeImpl) != 0;
848         }
849
850
851         internal DllImportAttribute(
852             string dllName, string entryPoint, CharSet charSet, bool exactSpelling, bool setLastError, bool preserveSig,
853             CallingConvention callingConvention, bool bestFitMapping, bool throwOnUnmappableChar)
854         {
855             _val = dllName;
856             EntryPoint = entryPoint;
857             CharSet = charSet;
858             ExactSpelling = exactSpelling;
859             SetLastError = setLastError;
860             PreserveSig = preserveSig;
861             CallingConvention = callingConvention;
862             BestFitMapping = bestFitMapping;
863             ThrowOnUnmappableChar = throwOnUnmappableChar;
864         }
865
866         internal String _val;
867
868         public DllImportAttribute(String dllName)
869         {
870             _val = dllName;
871         }
872         public String Value { get { return _val; } }
873
874         public String EntryPoint;
875         public CharSet CharSet;
876         public bool SetLastError;
877         public bool ExactSpelling;
878         public bool PreserveSig;
879         public CallingConvention CallingConvention;
880         public bool BestFitMapping;
881         public bool ThrowOnUnmappableChar;
882
883     }
884
885     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
886     [System.Runtime.InteropServices.ComVisible(true)]
887     public unsafe sealed class StructLayoutAttribute : Attribute
888     {
889         private const int DEFAULT_PACKING_SIZE = 8;
890
891         [System.Security.SecurityCritical]  // auto-generated
892         internal static StructLayoutAttribute GetCustomAttribute(RuntimeType type)
893         {
894             if (!IsDefined(type))
895                 return null;
896
897             int pack = 0, size = 0;
898             LayoutKind layoutKind = LayoutKind.Auto;
899             switch (type.Attributes & TypeAttributes.LayoutMask)
900             {
901                 case TypeAttributes.ExplicitLayout: layoutKind = LayoutKind.Explicit; break;
902                 case TypeAttributes.AutoLayout: layoutKind = LayoutKind.Auto; break;
903                 case TypeAttributes.SequentialLayout: layoutKind = LayoutKind.Sequential; break;
904                 default: Contract.Assume(false); break;
905             }
906
907             CharSet charSet = CharSet.None;
908             switch (type.Attributes & TypeAttributes.StringFormatMask)
909             {
910                 case TypeAttributes.AnsiClass: charSet = CharSet.Ansi; break;
911                 case TypeAttributes.AutoClass: charSet = CharSet.Auto; break;
912                 case TypeAttributes.UnicodeClass: charSet = CharSet.Unicode; break;
913                 default: Contract.Assume(false); break;
914             }
915
916 #if MONO
917             type.GetPacking (out pack, out size);
918 #else
919             type.GetRuntimeModule().MetadataImport.GetClassLayout(type.MetadataToken, out pack, out size);
920 #endif
921
922             // Metadata parameter checking should not have allowed 0 for packing size.
923             // The runtime later converts a packing size of 0 to 8 so do the same here
924             // because it's more useful from a user perspective. 
925             if (pack == 0)
926                 pack = DEFAULT_PACKING_SIZE;
927
928             return new StructLayoutAttribute(layoutKind, pack, size, charSet);
929         }
930
931         internal static bool IsDefined(RuntimeType type)
932         {
933             if (type.IsInterface || type.HasElementType || type.IsGenericParameter)
934                 return false;
935
936             return true;
937         }
938
939         internal LayoutKind _val;
940
941         internal StructLayoutAttribute(LayoutKind layoutKind, int pack, int size, CharSet charSet)
942         {
943             _val = layoutKind;
944             Pack = pack;
945             Size = size;
946             CharSet = charSet;
947         }
948
949         public StructLayoutAttribute(LayoutKind layoutKind)
950         {
951             _val = layoutKind;
952         }
953         public StructLayoutAttribute(short layoutKind)
954         {
955             _val = (LayoutKind)layoutKind;
956         }
957         public LayoutKind Value { get { return _val; } }
958         public int Pack;
959         public int Size;
960         public CharSet CharSet;
961     }
962
963     [AttributeUsage(AttributeTargets.Field, Inherited = false)]
964     [System.Runtime.InteropServices.ComVisible(true)]
965     public unsafe sealed class FieldOffsetAttribute : Attribute
966     {
967         [System.Security.SecurityCritical]  // auto-generated
968         internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
969         {
970             int fieldOffset;
971
972             if (field.DeclaringType != null &&
973 #if MONO
974                 (fieldOffset = field.GetFieldOffset ()) >= 0)
975 #else
976                 field.GetRuntimeModule().MetadataImport.GetFieldOffset(field.DeclaringType.MetadataToken, field.MetadataToken, out fieldOffset))
977 #endif
978                 return new FieldOffsetAttribute(fieldOffset);
979
980             return null;
981         }
982
983         [System.Security.SecurityCritical]  // auto-generated
984         internal static bool IsDefined(RuntimeFieldInfo field)
985         {
986             return GetCustomAttribute(field) != null;
987         }
988
989         internal int _val;
990         public FieldOffsetAttribute(int offset)
991         {
992             _val = offset;
993         }
994         public int Value { get { return _val; } }
995     }
996
997 #if FEATURE_COMINTEROP || MOBILE_LEGACY
998     [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] 
999     [System.Runtime.InteropServices.ComVisible(true)]
1000     public sealed class ComAliasNameAttribute : Attribute
1001     {
1002         internal String _val;
1003         public ComAliasNameAttribute(String alias)
1004         {
1005             _val = alias;
1006         }
1007         public String Value { get {return _val;} }  
1008     }    
1009
1010     [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)] 
1011     [System.Runtime.InteropServices.ComVisible(true)]
1012     public sealed class AutomationProxyAttribute : Attribute
1013     {
1014         internal bool _val;
1015         public AutomationProxyAttribute(bool val)
1016         {
1017             _val = val;
1018         }
1019         public bool Value { get {return _val;} }
1020     }
1021
1022     [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)] 
1023     [System.Runtime.InteropServices.ComVisible(true)]
1024     public sealed class PrimaryInteropAssemblyAttribute : Attribute
1025     {
1026         internal int _major;
1027         internal int _minor;
1028         
1029         public PrimaryInteropAssemblyAttribute(int major, int minor)
1030         {
1031             _major = major;
1032             _minor = minor;
1033         }
1034         
1035         public int MajorVersion { get {return _major;} }
1036         public int MinorVersion { get {return _minor;} }
1037     }
1038
1039     [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
1040     [System.Runtime.InteropServices.ComVisible(true)]
1041     public sealed class CoClassAttribute : Attribute
1042     {
1043         internal Type _CoClass;
1044
1045         public CoClassAttribute(Type coClass)
1046         {
1047             _CoClass = coClass;
1048         }
1049
1050         public Type CoClass { get { return _CoClass; } }
1051     }
1052
1053     [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
1054     [System.Runtime.InteropServices.ComVisible(true)]
1055     public sealed class ComEventInterfaceAttribute : Attribute
1056     {
1057         internal Type _SourceInterface;
1058         internal Type _EventProvider;
1059         
1060         public ComEventInterfaceAttribute(Type SourceInterface, Type EventProvider)
1061         {
1062             _SourceInterface = SourceInterface;
1063             _EventProvider = EventProvider;
1064         }
1065
1066         public Type SourceInterface { get {return _SourceInterface;} }       
1067         public Type EventProvider { get {return _EventProvider;} }
1068     }
1069 #endif
1070 #if FEATURE_COMINTEROP || MOBILE_LEGACY
1071     [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] 
1072     [System.Runtime.InteropServices.ComVisible(true)]
1073     public sealed class TypeLibVersionAttribute : Attribute
1074     {
1075         internal int _major;
1076         internal int _minor;
1077         
1078         public TypeLibVersionAttribute(int major, int minor)
1079         {
1080             _major = major;
1081             _minor = minor;
1082         }
1083         
1084         public int MajorVersion { get {return _major;} }
1085         public int MinorVersion { get {return _minor;} }
1086     }
1087
1088     [AttributeUsage(AttributeTargets.Assembly, Inherited = false)] 
1089     [System.Runtime.InteropServices.ComVisible(true)]
1090     public sealed class ComCompatibleVersionAttribute : Attribute
1091     {
1092         internal int _major;
1093         internal int _minor;
1094         internal int _build;
1095         internal int _revision;
1096         
1097         public ComCompatibleVersionAttribute(int major, int minor, int build, int revision)
1098         {
1099             _major = major;
1100             _minor = minor;
1101             _build = build;
1102             _revision = revision;
1103         }
1104         
1105         public int MajorVersion { get {return _major;} }
1106         public int MinorVersion { get {return _minor;} }
1107         public int BuildNumber { get {return _build;} }
1108         public int RevisionNumber { get {return _revision;} }
1109     }
1110
1111     [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
1112     [System.Runtime.InteropServices.ComVisible(true)]
1113     public sealed class BestFitMappingAttribute : Attribute
1114     {
1115         internal bool _bestFitMapping;
1116
1117         public BestFitMappingAttribute(bool BestFitMapping)
1118         {
1119             _bestFitMapping = BestFitMapping;
1120         }
1121
1122         public bool BestFitMapping { get { return _bestFitMapping; } }
1123         public bool ThrowOnUnmappableChar;
1124     }
1125
1126     [AttributeUsage(AttributeTargets.Module, Inherited = false)]
1127     [System.Runtime.InteropServices.ComVisible(true)]
1128     public sealed class DefaultCharSetAttribute : Attribute
1129     {
1130         internal CharSet _CharSet;
1131
1132         public DefaultCharSetAttribute(CharSet charSet)
1133         {
1134             _CharSet = charSet;
1135         }
1136
1137         public CharSet CharSet { get { return _CharSet; } }
1138     }
1139
1140     [Obsolete("This attribute has been deprecated.  Application Domains no longer respect Activation Context boundaries in IDispatch calls.", false)]
1141     [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1142     [System.Runtime.InteropServices.ComVisible(true)]
1143     public sealed class SetWin32ContextInIDispatchAttribute : Attribute
1144     {
1145         public SetWin32ContextInIDispatchAttribute()
1146         {
1147         }
1148     }
1149
1150     [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
1151     [System.Runtime.InteropServices.ComVisible(false)]
1152     public sealed class ManagedToNativeComInteropStubAttribute : Attribute
1153     {
1154         internal Type _classType;
1155         internal String _methodName;
1156
1157         public ManagedToNativeComInteropStubAttribute(Type classType, String methodName)
1158         {
1159             _classType = classType;
1160             _methodName = methodName;
1161         }
1162
1163         public Type ClassType { get { return _classType; } }
1164         public String MethodName { get { return _methodName; } }
1165     }    
1166 #endif
1167 #endif
1168 }