Merge pull request #496 from nicolas-raoul/unit-test-for-issue2907
[mono.git] / mcs / class / corlib / System.Reflection.Emit / PropertyBuilder.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/PropertyBuilder.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001 Ximian, Inc.  http://www.ximian.com
32 //
33
34 #if !FULL_AOT_RUNTIME
35 using System;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41
42 namespace System.Reflection.Emit {
43         [ComVisible (true)]
44         [ComDefaultInterface (typeof (_PropertyBuilder))]
45         [ClassInterface (ClassInterfaceType.None)]
46         [StructLayout (LayoutKind.Sequential)]
47         public sealed class PropertyBuilder : PropertyInfo, _PropertyBuilder {
48
49 // Managed version of MonoReflectionPropertyBuilder
50 #pragma warning disable 169, 414
51                 private PropertyAttributes attrs;
52                 private string name;
53                 private Type type;
54                 private Type[] parameters;
55                 private CustomAttributeBuilder[] cattrs;
56                 private object def_value;
57                 private MethodBuilder set_method;
58                 private MethodBuilder get_method;
59                 private int table_idx = 0;
60                 internal TypeBuilder typeb;
61                 private Type[] returnModReq;
62                 private Type[] returnModOpt;
63                 private Type[][] paramModReq;
64                 private Type[][] paramModOpt;
65                 CallingConventions callingConvention;           
66 #pragma warning restore 169, 414
67                 
68                 internal PropertyBuilder (TypeBuilder tb, string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
69                 {
70                         this.name = name;
71                         this.attrs = attributes;
72                         this.callingConvention = callingConvention;
73                         this.type = returnType;
74                         this.returnModReq = returnModReq;
75                         this.returnModOpt = returnModOpt;
76                         this.paramModReq = paramModReq;
77                         this.paramModOpt = paramModOpt;
78                         if (parameterTypes != null) {
79                                 this.parameters = new Type [parameterTypes.Length];
80                                 System.Array.Copy (parameterTypes, this.parameters, this.parameters.Length);
81                         }
82                         typeb = tb;
83                         table_idx = tb.get_next_table_index (this, 0x17, true);
84                 }
85
86                 public override PropertyAttributes Attributes {
87                         get {return attrs;}
88                 }
89                 public override bool CanRead {
90                         get {return get_method != null;}
91                 }
92                 public override bool CanWrite {
93                         get {return set_method != null;}
94                 }
95                 public override Type DeclaringType {
96                         get {return typeb;}
97                 }
98                 public override string Name {
99                         get {return name;}
100                 }
101                 public PropertyToken PropertyToken {
102                         get {return new PropertyToken ();}
103                 }
104                 public override Type PropertyType {
105                         get {return type;}
106                 }
107                 public override Type ReflectedType {
108                         get {return typeb;}
109                 }
110                 public void AddOtherMethod( MethodBuilder mdBuilder) {
111                 }
112                 public override MethodInfo[] GetAccessors( bool nonPublic) {
113                         return null;
114                 }
115                 public override object[] GetCustomAttributes(bool inherit) {
116                         throw not_supported ();
117                 }
118                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
119                         throw not_supported ();
120                 }
121                 public override MethodInfo GetGetMethod( bool nonPublic) {
122                         return get_method;
123                 }
124                 public override ParameterInfo[] GetIndexParameters() {
125                         throw not_supported ();
126                 }
127                 public override MethodInfo GetSetMethod( bool nonPublic) {
128                         return set_method;
129                 }
130                 public override object GetValue(object obj, object[] index) {
131                         return null;
132                 }
133                 public override object GetValue( object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) {
134                         throw not_supported ();
135                 }
136                 public override bool IsDefined( Type attributeType, bool inherit) {
137                         throw not_supported ();
138                 }
139                 public void SetConstant( object defaultValue) {
140                         def_value = defaultValue;
141                 }
142                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
143                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
144                         if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
145                                 attrs |= PropertyAttributes.SpecialName;
146                                 return;
147                         }
148
149                         if (cattrs != null) {
150                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
151                                 cattrs.CopyTo (new_array, 0);
152                                 new_array [cattrs.Length] = customBuilder;
153                                 cattrs = new_array;
154                         } else {
155                                 cattrs = new CustomAttributeBuilder [1];
156                                 cattrs [0] = customBuilder;
157                         }
158                 }
159
160                 [ComVisible (true)]
161                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
162                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
163                 }
164                 public void SetGetMethod( MethodBuilder mdBuilder) {
165                         get_method = mdBuilder;
166                 }
167                 public void SetSetMethod( MethodBuilder mdBuilder) {
168                         set_method = mdBuilder;
169                 }
170                 public override void SetValue( object obj, object value, object[] index) {
171                 }
172                 public override void SetValue( object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) {
173                 }
174
175                 public override Module Module {
176                         get {
177                                 return base.Module;
178                         }
179                 }
180
181                 void _PropertyBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
182                 {
183                         throw new NotImplementedException ();
184                 }
185
186                 void _PropertyBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
187                 {
188                         throw new NotImplementedException ();
189                 }
190
191                 void _PropertyBuilder.GetTypeInfoCount (out uint pcTInfo)
192                 {
193                         throw new NotImplementedException ();
194                 }
195
196                 void _PropertyBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
197                 {
198                         throw new NotImplementedException ();
199                 }
200
201                 private Exception not_supported ()
202                 {
203                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
204                 }
205         }
206 }
207
208 #endif