本文整理汇总了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);
}