25using System.Reflection.Metadata;
33 internal sealed class FieldBuilder :
FieldInfo
36 readonly TypeBuilder type;
38 readonly
int pseudoToken;
39 FieldAttributes attributes;
43 BlobHandle signatureBlobHandle;
53 internal FieldBuilder(TypeBuilder type,
string name,
Type fieldType, CustomModifiers customModifiers, FieldAttributes attributes)
57 this.attributes = attributes;
58 this.pseudoToken = type.ModuleBuilder.AllocPseudoToken();
59 this.signature =
FieldSignature.Create(fieldType, customModifiers);
60 this.type.ModuleBuilder.FieldTable.AddVirtualRecord();
64 signature.Write(type.ModuleBuilder, buf);
65 signatureBlobHandle = type.ModuleBuilder.GetOrAddBlob(buf.ToArray());
68 public void SetConstant(
object defaultValue)
70 attributes |= FieldAttributes.HasDefault;
71 type.ModuleBuilder.AddConstant(pseudoToken, defaultValue);
74 public override object GetRawConstantValue()
76 if (!type.IsCreated())
80 throw new NotSupportedException();
83 return type.Module.ConstantTable.GetRawConstantValue(type.Module, GetCurrentToken());
86 public override bool __TryGetFieldOffset(out
int offset)
88 return (offset = this.offset) != -1;
91 public void SetCustomAttribute(
ConstructorInfo con,
byte[] binaryAttribute)
93 SetCustomAttribute(
new CustomAttributeBuilder(con, binaryAttribute));
96 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
98 switch (customBuilder.KnownCA)
100 case KnownCA.FieldOffsetAttribute:
101 SetOffset((
int)customBuilder.DecodeBlob(
Module.Assembly).GetConstructorArgument(0));
103 case KnownCA.MarshalAsAttribute:
104 FieldMarshal.SetMarshalAsAttribute(type.ModuleBuilder, pseudoToken, customBuilder);
105 attributes |= FieldAttributes.HasFieldMarshal;
107 case KnownCA.NonSerializedAttribute:
108 attributes |= FieldAttributes.NotSerialized;
110 case KnownCA.SpecialNameAttribute:
111 attributes |= FieldAttributes.SpecialName;
114 type.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder);
119 public void SetOffset(
int iOffset)
124 public override FieldAttributes Attributes
126 get {
return attributes; }
129 public override Type DeclaringType
131 get {
return type.IsModulePseudoType ? null : type; }
134 public override string Name
139 public override int MetadataToken
141 get {
return pseudoToken; }
146 get {
return type.Module; }
149 public FieldToken GetToken()
151 return new FieldToken(pseudoToken);
154 internal void WriteFieldRecords()
156 type.ModuleBuilder.Metadata.AddFieldDefinition(
157 (
System.Reflection.FieldAttributes)attributes,
158 type.ModuleBuilder.GetOrAddString(name),
159 signatureBlobHandle);
162 internal void FixupToken(
int token)
168 rec.Field = pseudoToken;
169 type.ModuleBuilder.FieldLayoutTable.AddRecord(rec);
172 type.ModuleBuilder.RegisterTokenFixup(pseudoToken, token);
177 get {
return signature; }
180 internal override int ImportTo(ModuleBuilder other)
182 return other.ImportMethodOrField(type, name, signature);
185 internal override int GetCurrentToken()
187 if (type.ModuleBuilder.IsSaved)
188 return type.ModuleBuilder.ResolvePseudoToken(pseudoToken);
193 internal override bool IsBaked
195 get {
return type.IsBaked; }
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.FieldInfo FieldInfo
global::java.lang.invoke.LambdaForm.Name Name
KnownCA
These are the pseudo-custom attributes that are recognized by name by the runtime (i....