public Type Compile(IMetadataSchema schema, ModuleBuilder module, string @namespace, string typeName) { if (schema == null) { throw new ArgumentNullException(nameof(schema)); } if (schema.Definitions == null) { throw new ArgumentException("There is no definition is schema.", nameof(schema)); } if (module == null) { throw new ArgumentNullException(nameof(module)); } if (@namespace == null) { throw new ArgumentNullException(nameof(@namespace)); } if (string.IsNullOrWhiteSpace(@namespace)) { throw new ArgumentException("namespace cannot be white space.", nameof(@namespace)); } if (typeName == null) { throw new ArgumentNullException(nameof(typeName)); } if (string.IsNullOrWhiteSpace(typeName)) { throw new ArgumentException("typeName cannot be white space.", nameof(typeName)); } ValidateProperties(); return CompileCore(schema, module, @namespace, typeName); }
public void Compile(IMetadataSchema schema, string assemblyName, string @namespace, string typeName) { if (schema == null) { throw new ArgumentNullException(nameof(schema)); } if (schema.Definitions == null) { throw new ArgumentException("There is no definition is schema.", nameof(schema)); } if (assemblyName == null) { throw new ArgumentNullException(nameof(assemblyName)); } if (string.IsNullOrWhiteSpace(assemblyName)) { throw new ArgumentException("assemblyName cannot be white space.", nameof(assemblyName)); } if (@namespace == null) { throw new ArgumentNullException(nameof(@namespace)); } if (string.IsNullOrWhiteSpace(@namespace)) { throw new ArgumentException("namespace cannot be white space.", nameof(@namespace)); } if (typeName == null) { throw new ArgumentNullException(nameof(typeName)); } if (string.IsNullOrWhiteSpace(typeName)) { throw new ArgumentException("typeName cannot be white space.", nameof(typeName)); } ValidateProperties(); var file = assemblyName + ".dll"; var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Save); var module = assembly.DefineDynamicModule(assemblyName, file); CompileCore(schema, module, @namespace, typeName); assembly.Save(file); }
public Type CompileCore(IMetadataSchema schema, ModuleBuilder module, string @namespace, string typeName) { var type = module.DefineType(@namespace + "." + typeName, TypeAttributes.Public); int index = 0; foreach (var pair in schema.Definitions) { var pt = GetMetadataType(pair.Value); var name = Namer?.Invoke(pair.Key) ?? pair.Key; var field = type.DefineField("$field$" + (++index).ToString(), pt, FieldAttributes.Private); var prop = type.DefineProperty(name, PropertyAttributes.None, pt, Type.EmptyTypes); SetAttributes(pair, prop); prop.SetGetMethod(CreateGetMethod(type, pt, name, field)); prop.SetSetMethod(CreateSetMethod(type, pt, name, field)); } if (ShouldEmitAdditional) { if (string.IsNullOrWhiteSpace(NameOfAdditional)) { throw new InvalidOperationException($"{nameof(NameOfAdditional)} cannot be null or whitespace."); } CreateAdditional(type, ++index); } return type.CreateType(); }