static Type DefineRecord(this ModuleBuilder mb, RecordSpec spec) { const TypeAttributes attribs = TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.ExplicitLayout | TypeAttributes.Serializable | TypeAttributes.AnsiClass; var fullName = string.IsNullOrWhiteSpace(spec.Namespace) ? spec.Namespace : $"{spec.Namespace}.{spec.TypeName}"; TypeBuilder tb = mb.DefineType(fullName, attribs, typeof(ValueType)); foreach (var field in spec.Fields) { tb.DefineField(field); } var type = tb.CreateType(); return(type); }
/// <summary> /// Manufactures the type that reifies the record definition /// </summary> /// <param name="spec">The record definition</param> public static Type CreateType(this RecordSpec spec) => Record.CreateType(spec);
/// <summary> /// Manufactures the type that reifies a supplied record definition /// </summary> /// <param name="spec">The record definition</param> public static Type CreateType(RecordSpec spec) => DefineModule().DefineRecord(spec);