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