2 Copyright (C) 2009 Jeroen Frijters
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.
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:
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.
25 using System.Collections.Generic;
28 using IKVM.Reflection.Emit;
29 using IKVM.Reflection.Writer;
30 using IKVM.Reflection.Reader;
32 namespace IKVM.Reflection
34 sealed class FieldSignature : Signature
36 private readonly Type fieldType;
37 private readonly Type[] optionalCustomModifiers;
38 private readonly Type[] requiredCustomModifiers;
40 internal static FieldSignature Create(Type fieldType, Type[] optionalCustomModifiers, Type[] requiredCustomModifiers)
42 return new FieldSignature(fieldType, Util.Copy(optionalCustomModifiers), Util.Copy(requiredCustomModifiers));
45 private FieldSignature(Type fieldType, Type[] optionalCustomModifiers, Type[] requiredCustomModifiers)
47 this.fieldType = fieldType;
48 this.optionalCustomModifiers = optionalCustomModifiers;
49 this.requiredCustomModifiers = requiredCustomModifiers;
52 public override bool Equals(object obj)
54 FieldSignature other = obj as FieldSignature;
56 && other.fieldType.Equals(fieldType)
57 && Util.ArrayEquals(other.optionalCustomModifiers, optionalCustomModifiers)
58 && Util.ArrayEquals(other.requiredCustomModifiers, requiredCustomModifiers);
61 public override int GetHashCode()
63 return fieldType.GetHashCode() ^ Util.GetHashCode(optionalCustomModifiers) ^ Util.GetHashCode(requiredCustomModifiers);
66 internal Type FieldType
68 get { return fieldType; }
71 internal Type[] GetOptionalCustomModifiers()
73 return Util.Copy(optionalCustomModifiers);
76 internal Type[] GetRequiredCustomModifiers()
78 return Util.Copy(requiredCustomModifiers);
81 internal FieldSignature ExpandTypeParameters(Type declaringType)
83 return new FieldSignature(
84 fieldType.BindTypeParameters(declaringType),
85 BindTypeParameters(declaringType, optionalCustomModifiers),
86 BindTypeParameters(declaringType, requiredCustomModifiers));
89 internal static FieldSignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context)
91 if (br.ReadByte() != FIELD)
93 throw new BadImageFormatException();
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);
103 internal override void WriteSig(ModuleBuilder module, ByteBuffer bb)
106 WriteCustomModifiers(module, bb, ELEMENT_TYPE_CMOD_OPT, optionalCustomModifiers);
107 WriteCustomModifiers(module, bb, ELEMENT_TYPE_CMOD_REQD, requiredCustomModifiers);
108 WriteType(module, bb, fieldType);