当前位置: 首页>>代码示例>>C#>>正文


C# TypeBuilder.DefineInitializedData方法代码示例

本文整理汇总了C#中System.Reflection.Emit.TypeBuilder.DefineInitializedData方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.DefineInitializedData方法的具体用法?C# TypeBuilder.DefineInitializedData怎么用?C# TypeBuilder.DefineInitializedData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.TypeBuilder的用法示例。


在下文中一共展示了TypeBuilder.DefineInitializedData方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DefineField

        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);
        }
开发者ID:furesoft,项目名称:roslyn,代码行数:62,代码来源:ReflectionEmitter.cs


注:本文中的System.Reflection.Emit.TypeBuilder.DefineInitializedData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。