New test.
[mono.git] / mcs / class / corlib / System.Reflection.Emit / UnmanagedMarshal.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
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:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
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.
23 //
24
25 //
26 // System.Reflection.Emit/UnmanagedMarshal.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001-2002 Ximian, Inc.  http://www.ximian.com
32 //
33
34 using System.Reflection.Emit;
35 using System.Runtime.InteropServices;
36 using System;
37
38 namespace System.Reflection.Emit {
39
40 #if NET_2_0
41         [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
42         [ComVisible (true)]
43 #endif
44         [Serializable]
45         public sealed class UnmanagedMarshal {
46                 private int count;
47                 private UnmanagedType t;
48                 private UnmanagedType tbase;
49                 string guid;
50                 string mcookie;
51                 string marshaltype;
52                 Type marshaltyperef;
53                 private int param_num;
54                 private bool has_size;
55                 
56                 private UnmanagedMarshal (UnmanagedType maint, int cnt) {
57                         count = cnt;
58                         t = maint;
59                         tbase = maint;
60                 }
61                 private UnmanagedMarshal (UnmanagedType maint, UnmanagedType elemt) {
62                         count = 0;
63                         t = maint;
64                         tbase = elemt;
65                 }
66                 
67                 public UnmanagedType BaseType {
68                         get {
69                                 if (t == UnmanagedType.LPArray || t == UnmanagedType.SafeArray)
70                                         throw new ArgumentException ();
71                                 return tbase;
72                         }
73                 }
74
75                 public int ElementCount {
76                         get {return count;}
77                 }
78
79                 public UnmanagedType GetUnmanagedType {
80                         get {return t;}
81                 }
82
83                 public Guid IIDGuid {
84                         get {return new Guid (guid);}
85                 }
86
87                 public static UnmanagedMarshal DefineByValArray( int elemCount) {
88                         return new UnmanagedMarshal (UnmanagedType.ByValArray, elemCount);
89                 }
90
91                 public static UnmanagedMarshal DefineByValTStr( int elemCount) {
92                         return new UnmanagedMarshal (UnmanagedType.ByValTStr, elemCount);
93                 }
94
95                 public static UnmanagedMarshal DefineLPArray( UnmanagedType elemType) {
96                         return new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
97                 }
98
99                 public static UnmanagedMarshal DefineSafeArray( UnmanagedType elemType) {
100                         return new UnmanagedMarshal (UnmanagedType.SafeArray, elemType);
101                 }
102
103                 public static UnmanagedMarshal DefineUnmanagedMarshal( UnmanagedType unmanagedType) {
104                         return new UnmanagedMarshal (unmanagedType, unmanagedType);
105                 }
106
107                 /* this one is missing from the MS implementation */
108                 public static UnmanagedMarshal DefineCustom (Type typeref, string cookie, string mtype, Guid id) {
109                         UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.CustomMarshaler, UnmanagedType.CustomMarshaler);
110                         res.mcookie = cookie;
111                         res.marshaltype = mtype;
112                         res.marshaltyperef = typeref;
113                         if (id == Guid.Empty)
114                                 res.guid = String.Empty;
115                         else
116                                 res.guid = id.ToString ();
117                         return res;
118                 }
119
120                 // sizeConst and sizeParamIndex can be -1 meaning they are not specified
121                 internal static UnmanagedMarshal DefineLPArrayInternal (UnmanagedType elemType, int sizeConst, int sizeParamIndex) {
122                         UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
123                         res.count = sizeConst;
124                         res.param_num = sizeParamIndex;
125                         res.has_size = true;
126
127                         return res;
128                 }
129
130                 internal MarshalAsAttribute ToMarshalAsAttribute () {
131                         MarshalAsAttribute attr = new MarshalAsAttribute (t);
132                         attr.ArraySubType = tbase;
133                         attr.MarshalCookie = mcookie;
134                         attr.MarshalType = marshaltype;
135                         attr.MarshalTypeRef = marshaltyperef;
136                         if (count == -1)
137                                 attr.SizeConst = 0;
138                         else
139                                 attr.SizeConst = count;
140                         if (param_num == -1)
141                                 attr.SizeParamIndex = 0;
142                         else
143                                 attr.SizeParamIndex = (short)param_num;
144                         return attr;
145                 }
146         }
147 }