This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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         [Serializable]
41         public sealed class UnmanagedMarshal {
42                 private int count;
43                 private UnmanagedType t;
44                 private UnmanagedType tbase;
45                 string guid;
46                 string mcookie;
47                 string marshaltype;
48                 Type marshaltyperef;
49                 
50                 private UnmanagedMarshal (UnmanagedType maint, int cnt) {
51                         count = cnt;
52                         t = maint;
53                         tbase = maint;
54                 }
55                 private UnmanagedMarshal (UnmanagedType maint, UnmanagedType elemt) {
56                         count = 0;
57                         t = maint;
58                         tbase = elemt;
59                 }
60                 
61                 public UnmanagedType BaseType {
62                         get {
63                                 if (t == UnmanagedType.LPArray || t == UnmanagedType.SafeArray)
64                                         throw new ArgumentException ();
65                                 return tbase;
66                         }
67                 }
68
69                 public int ElementCount {
70                         get {return count;}
71                 }
72
73                 public UnmanagedType GetUnmanagedType {
74                         get {return t;}
75                 }
76
77                 public Guid IIDGuid {
78                         get {return new Guid (guid);}
79                 }
80
81                 public static UnmanagedMarshal DefineByValArray( int elemCount) {
82                         return new UnmanagedMarshal (UnmanagedType.ByValArray, elemCount);
83                 }
84
85                 public static UnmanagedMarshal DefineByValTStr( int elemCount) {
86                         return new UnmanagedMarshal (UnmanagedType.ByValTStr, elemCount);
87                 }
88
89                 public static UnmanagedMarshal DefineLPArray( UnmanagedType elemType) {
90                         return new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
91                 }
92
93                 public static UnmanagedMarshal DefineSafeArray( UnmanagedType elemType) {
94                         return new UnmanagedMarshal (UnmanagedType.SafeArray, elemType);
95                 }
96
97                 public static UnmanagedMarshal DefineUnmanagedMarshal( UnmanagedType unmanagedType) {
98                         return new UnmanagedMarshal (unmanagedType, unmanagedType);
99                 }
100
101                 /* this one is missing from the MS implementation */
102                 public static UnmanagedMarshal DefineCustom (Type typeref, string cookie, string mtype, Guid id) {
103                         UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.CustomMarshaler, UnmanagedType.CustomMarshaler);
104                         res.mcookie = cookie;
105                         res.marshaltype = mtype;
106                         res.marshaltyperef = typeref;
107                         if (id == Guid.Empty)
108                                 res.guid = "";
109                         else
110                                 res.guid = id.ToString ();
111                         return res;
112                 }
113
114         }
115 }