3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 // System.Reflection.Emit/UnmanagedMarshal.cs
29 // Paolo Molaro (lupus@ximian.com)
31 // (C) 2001-2002 Ximian, Inc. http://www.ximian.com
35 using System.Reflection.Emit;
36 using System.Runtime.InteropServices;
39 namespace System.Reflection.Emit {
41 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
44 [StructLayout (LayoutKind.Sequential)]
45 public sealed class UnmanagedMarshal {
46 #pragma warning disable 169, 414
48 private UnmanagedType t;
49 private UnmanagedType tbase;
54 private int param_num;
55 private bool has_size;
56 #pragma warning restore 169, 414
58 private UnmanagedMarshal (UnmanagedType maint, int cnt) {
63 private UnmanagedMarshal (UnmanagedType maint, UnmanagedType elemt) {
69 public UnmanagedType BaseType {
71 if (t == UnmanagedType.LPArray)
72 throw new ArgumentException ();
74 #if FEATURE_COMINTEROP
75 if (t == UnmanagedType.SafeArray)
76 throw new ArgumentException ();
82 public int ElementCount {
86 public UnmanagedType GetUnmanagedType {
91 get {return new Guid (guid);}
94 public static UnmanagedMarshal DefineByValArray( int elemCount) {
95 return new UnmanagedMarshal (UnmanagedType.ByValArray, elemCount);
98 public static UnmanagedMarshal DefineByValTStr( int elemCount) {
99 return new UnmanagedMarshal (UnmanagedType.ByValTStr, elemCount);
102 public static UnmanagedMarshal DefineLPArray( UnmanagedType elemType) {
103 return new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
105 #if FEATURE_COMINTEROP
106 public static UnmanagedMarshal DefineSafeArray( UnmanagedType elemType) {
107 return new UnmanagedMarshal (UnmanagedType.SafeArray, elemType);
110 public static UnmanagedMarshal DefineUnmanagedMarshal( UnmanagedType unmanagedType) {
111 return new UnmanagedMarshal (unmanagedType, unmanagedType);
113 #if FEATURE_COMINTEROP
114 internal static UnmanagedMarshal DefineCustom (Type typeref, string cookie, string mtype, Guid id) {
115 UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.CustomMarshaler, UnmanagedType.CustomMarshaler);
116 res.mcookie = cookie;
117 res.marshaltype = mtype;
118 res.marshaltyperef = typeref;
119 if (id == Guid.Empty)
120 res.guid = String.Empty;
122 res.guid = id.ToString ();
126 // sizeConst and sizeParamIndex can be -1 meaning they are not specified
127 internal static UnmanagedMarshal DefineLPArrayInternal (UnmanagedType elemType, int sizeConst, int sizeParamIndex) {
128 UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
129 res.count = sizeConst;
130 res.param_num = sizeParamIndex;