[HttpWebRequest] EndGetResponse already does this.
[mono.git] / mcs / class / IKVM.Reflection / FieldSignature.cs
1 /*
2   Copyright (C) 2009 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Collections.Generic;
26 using System.IO;
27 using System.Text;
28 using IKVM.Reflection.Emit;
29 using IKVM.Reflection.Writer;
30 using IKVM.Reflection.Reader;
31
32 namespace IKVM.Reflection
33 {
34         sealed class FieldSignature : Signature
35         {
36                 private readonly Type fieldType;
37                 private readonly Type[] optionalCustomModifiers;
38                 private readonly Type[] requiredCustomModifiers;
39
40                 internal static FieldSignature Create(Type fieldType, Type[] optionalCustomModifiers, Type[] requiredCustomModifiers)
41                 {
42                         return new FieldSignature(fieldType, Util.Copy(optionalCustomModifiers), Util.Copy(requiredCustomModifiers));
43                 }
44
45                 private FieldSignature(Type fieldType, Type[] optionalCustomModifiers, Type[] requiredCustomModifiers)
46                 {
47                         this.fieldType = fieldType;
48                         this.optionalCustomModifiers = optionalCustomModifiers;
49                         this.requiredCustomModifiers = requiredCustomModifiers;
50                 }
51
52                 public override bool Equals(object obj)
53                 {
54                         FieldSignature other = obj as FieldSignature;
55                         return other != null
56                                 && other.fieldType.Equals(fieldType)
57                                 && Util.ArrayEquals(other.optionalCustomModifiers, optionalCustomModifiers)
58                                 && Util.ArrayEquals(other.requiredCustomModifiers, requiredCustomModifiers);
59                 }
60
61                 public override int GetHashCode()
62                 {
63                         return fieldType.GetHashCode() ^ Util.GetHashCode(optionalCustomModifiers) ^ Util.GetHashCode(requiredCustomModifiers);
64                 }
65
66                 internal Type FieldType
67                 {
68                         get { return fieldType; }
69                 }
70
71                 internal Type[] GetOptionalCustomModifiers()
72                 {
73                         return Util.Copy(optionalCustomModifiers);
74                 }
75
76                 internal Type[] GetRequiredCustomModifiers()
77                 {
78                         return Util.Copy(requiredCustomModifiers);
79                 }
80
81                 internal FieldSignature ExpandTypeParameters(Type declaringType)
82                 {
83                         return new FieldSignature(
84                                 fieldType.BindTypeParameters(declaringType),
85                                 BindTypeParameters(declaringType, optionalCustomModifiers),
86                                 BindTypeParameters(declaringType, requiredCustomModifiers));
87                 }
88
89                 internal static FieldSignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context)
90                 {
91                         if (br.ReadByte() != FIELD)
92                         {
93                                 throw new BadImageFormatException();
94                         }
95                         Type fieldType;
96                         Type[] optionalCustomModifiers;
97                         Type[] requiredCustomModifiers;
98                         ReadCustomModifiers(module, br, context, out requiredCustomModifiers, out optionalCustomModifiers);
99                         fieldType = ReadType(module, br, context);
100                         return new FieldSignature(fieldType, optionalCustomModifiers, requiredCustomModifiers);
101                 }
102
103                 internal override void WriteSig(ModuleBuilder module, ByteBuffer bb)
104                 {
105                         bb.Write(FIELD);
106                         WriteCustomModifiers(module, bb, ELEMENT_TYPE_CMOD_OPT, optionalCustomModifiers);
107                         WriteCustomModifiers(module, bb, ELEMENT_TYPE_CMOD_REQD, requiredCustomModifiers);
108                         WriteType(module, bb, fieldType);
109                 }
110         }
111 }