Exemplo n.º 1
0
        // Define a data field within this class.
        private FieldBuilder DefineData(String name, byte[] data,
                                        int size, FieldAttributes attributes)
        {
            // Validate the parameters.
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            else if (name == String.Empty)
            {
                throw new ArgumentException(_("Emit_NameEmpty"));
            }
            else if (size <= 0 || size > 0x003EFFFF)
            {
                throw new ArgumentException(_("Emit_DataSize"));
            }

            // We must not have created the type yet.
            CheckNotCreated();

            // Look for or create a value type for the field.
            String typeName = "$ArrayType$" + size.ToString();
            Type   type     = module.GetType(typeName);

            if (type == null)
            {
                TypeBuilder builder;
                builder = module.DefineType
                              (typeName,
                              TypeAttributes.Public |
                              TypeAttributes.Sealed |
                              TypeAttributes.ExplicitLayout,
                              typeof(System.ValueType),
                              PackingSize.Size1, size);
                type = builder.CreateType();
            }

            // Define the field and set the data on it.
            FieldBuilder field = DefineField
                                     (name, type, attributes | FieldAttributes.Static);

            field.SetData(data, size);
            return(field);
        }