DefineInitializedData() public method

public DefineInitializedData ( string name, byte data, System attributes ) : System.Reflection.Emit.FieldBuilder
name string
data byte
attributes System
return System.Reflection.Emit.FieldBuilder
Example #1
0
 // Define initialized data as a global field.
 public FieldBuilder DefineInitializedData
     (String name, byte[] data, FieldAttributes attributes)
 {
     try
     {
         StartSync();
         return(moduleType.DefineInitializedData
                    (name, data, attributes));
     }
     finally
     {
         EndSync();
     }
 }
Example #2
0
        private void DefineField(TypeBuilder typeBuilder, Cci.IFieldDefinition fieldDef)
        {
            FieldAttributes attrs = (FieldAttributes)Cci.MetadataWriter.GetFieldFlags(fieldDef);
            FieldBuilder fieldBuilder;

            if (!fieldDef.MappedData.IsDefault)
            {
                // TODO (tomat, Dev12): Unfortunately, Reflection.Emit doesn't allow us to directly set FieldRVA on arbitrary FieldBuilder.
                // Instead it creates the mapping types itself and provides APIs to define mapped fields.
                // So we use that API and ignore the mapping types provided by the compiler.

                // FieldRVA
                fieldBuilder = typeBuilder.DefineInitializedData(fieldDef.Name, fieldDef.MappedData.ToArray(), attrs);
            }
            else
            {
                // Field types that are value types need to be loaded before the declaring type.
                // If the field type is a nested type, e.g. A.B.C only the inner-most type needs to be loaded before the declaring type.
                Type type = ResolveType(
                    fieldDef.GetType(_context),
                    genericContext: default(GenericContext),
                    dependentType: typeBuilder,
                    valueTypeDependency: true);

                // TODO (tomat, Dev12): this doesn't handle types constructed from modified types, we need Ref.Emit support for that:
                Type[] reqMods = null, optMods = null;
                ModifiedType modified = type as ModifiedType;
                if (modified != null)
                {
                    reqMods = modified.RequiredModifiers;
                    optMods = modified.OptionalModifiers;
                    type = modified.UnmodifiedType;
                }

                fieldBuilder = typeBuilder.DefineField(fieldDef.Name, type, reqMods, optMods, attrs);
            }

            // FieldLayout
            if (fieldDef.ContainingTypeDefinition.Layout == LayoutKind.Explicit && !fieldDef.IsStatic)
            {
                fieldBuilder.SetOffset((int)fieldDef.Offset);
            }

            // FieldMarshal
            if (fieldDef.IsMarshalledExplicitly)
            {
                var marshallingInformation = fieldDef.MarshallingInformation;

                if (marshallingInformation != null)
                {
                    fieldBuilder.SetCustomAttribute(GetMarshalAsAttribute(marshallingInformation));
                }
                else
                {
                    Debug.Assert(!fieldDef.MarshallingDescriptor.IsDefaultOrEmpty);
                    // TODO:
                    throw new NotImplementedException();
                }
            }

            _fieldBuilders.Add(fieldDef, fieldBuilder);
        }