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/FieldBuilder.cs
29 // Paolo Molaro (lupus@ximian.com)
31 // (C) 2001-2002 Ximian, Inc. http://www.ximian.com
35 using System
.Reflection
;
36 using System
.Reflection
.Emit
;
37 using System
.Globalization
;
38 using System
.Runtime
.CompilerServices
;
39 using System
.Runtime
.InteropServices
;
41 namespace System
.Reflection
.Emit
{
42 public sealed class FieldBuilder
: FieldInfo
{
43 private FieldAttributes attrs
;
46 private object def_value
;
48 private int table_idx
;
49 internal TypeBuilder typeb
;
50 private byte[] rva_data
;
51 private CustomAttributeBuilder
[] cattrs
;
52 private UnmanagedMarshal marshal_info
;
53 private RuntimeFieldHandle handle
;
54 private Type
[] modReq
;
55 private Type
[] modOpt
;
57 internal FieldBuilder (TypeBuilder tb
, string fieldName
, Type type
, FieldAttributes attributes
, Type
[] modReq
, Type
[] modOpt
) {
65 table_idx
= tb
.get_next_table_index (this, 0x04, true);
68 public override FieldAttributes Attributes
{
72 public override Type DeclaringType
{
76 public override RuntimeFieldHandle FieldHandle
{
78 throw CreateNotSupportedException ();
82 public override Type FieldType
{
86 public override string Name
{
90 public override Type ReflectedType
{
94 public override object[] GetCustomAttributes(bool inherit
) {
95 throw CreateNotSupportedException ();
98 public override object[] GetCustomAttributes(Type attributeType
, bool inherit
) {
99 throw CreateNotSupportedException ();
102 public FieldToken
GetToken() {
103 return new FieldToken (0x04000000 | table_idx
);
106 public override object GetValue(object obj
) {
107 throw CreateNotSupportedException ();
110 public override bool IsDefined( Type attributeType
, bool inherit
) {
111 throw CreateNotSupportedException ();
114 internal override int GetFieldOffset () {
119 internal void SetRVAData (byte[] data
) {
120 rva_data
= (byte[])data
.Clone ();
123 public void SetConstant( object defaultValue
) {
126 /*if (defaultValue.GetType() != type)
127 throw new ArgumentException ("Constant doesn't match field type");*/
128 def_value
= defaultValue
;
131 public void SetCustomAttribute (CustomAttributeBuilder customBuilder
) {
134 string attrname
= customBuilder
.Ctor
.ReflectedType
.FullName
;
135 if (attrname
== "System.Runtime.InteropServices.FieldOffsetAttribute") {
136 byte[] data
= customBuilder
.Data
;
137 offset
= (int)data
[2];
138 offset
|= ((int)data
[3]) << 8;
139 offset
|= ((int)data
[4]) << 16;
140 offset
|= ((int)data
[5]) << 24;
142 } else if (attrname
== "System.NonSerializedAttribute") {
143 attrs
|= FieldAttributes
.NotSerialized
;
145 } else if (attrname
== "System.Runtime.InteropServices.MarshalAsAttribute") {
146 attrs
|= FieldAttributes
.HasFieldMarshal
;
147 marshal_info
= CustomAttributeBuilder
.get_umarshal (customBuilder
, true);
148 /* FIXME: check for errors */
151 if (cattrs
!= null) {
152 CustomAttributeBuilder
[] new_array
= new CustomAttributeBuilder
[cattrs
.Length
+ 1];
153 cattrs
.CopyTo (new_array
, 0);
154 new_array
[cattrs
.Length
] = customBuilder
;
157 cattrs
= new CustomAttributeBuilder
[1];
158 cattrs
[0] = customBuilder
;
162 public void SetCustomAttribute( ConstructorInfo con
, byte[] binaryAttribute
) {
164 SetCustomAttribute (new CustomAttributeBuilder (con
, binaryAttribute
));
167 public void SetMarshal( UnmanagedMarshal unmanagedMarshal
) {
169 marshal_info
= unmanagedMarshal
;
170 attrs
|= FieldAttributes
.HasFieldMarshal
;
173 public void SetOffset( int iOffset
) {
178 public override void SetValue( object obj
, object val
, BindingFlags invokeAttr
, Binder binder
, CultureInfo culture
) {
179 throw CreateNotSupportedException ();
182 private Exception
CreateNotSupportedException ()
184 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
187 private void RejectIfCreated ()
189 if (typeb
.is_created
)
190 throw new InvalidOperationException ("Unable to change after type has been created.");
193 #if NET_2_0 || BOOTSTRAP_NET_2_0
194 public override FieldInfo
Mono_GetGenericFieldDefinition ()