/// <summary> /// Creates DTO type (class) /// </summary> /// <param name="data">data as a data source</param> /// <param name="addSupportProperties">Tru for generating properties with suffixes Required, Error, and HasError</param> /// <returns>Newly created type</returns> private static Type CreateType(DataCollection data, bool addSupportProperties, string dtoTypeFullName) { // Get type builder from newly created assembly // Define dynamic assembly AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = dtoTypeFullName + ".dll"; AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name); // Define type TypeBuilder typeBuilder = moduleBuilder.DefineType(dtoTypeFullName , TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout , typeof(BindableObject)); // Get ConstructorInfo for base class (BindableObject) ConstructorInfo constructorInfo = typeof(BindableObject).GetConstructor(new Type[] { typeof(DataCollection) }); // Define DTO constructor //var defaultConstructor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null); Type[] constructorArguments = new Type[] { typeof(DataCollection) }; ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, constructorArguments); // Generate DTO constructor ILGenerator getILGenerator = constructorBuilder.GetILGenerator(); getILGenerator.Emit(OpCodes.Ldarg_0); // Load BindableObject on stack getILGenerator.Emit(OpCodes.Ldarg_1); // Load DataRow on stack getILGenerator.Emit(OpCodes.Call, constructorInfo); // Call BindableObject constructor getILGenerator.Emit(OpCodes.Ret); //// Generate DTO constructor //var defaultgenerator = defaultConstructor.GetILGenerator(); //defaultgenerator.Emit(OpCodes.Ldarg_0); // Load BindableObject on stack //defaultgenerator.Emit(OpCodes.Ldarg_1); // Load DataRow on stack ////defaultgenerator.Emit(OpCodes.Call); // Call BindableObject constructor //defaultgenerator.Emit(OpCodes.Ret); foreach (var dc in data.Items) { Type datatype = typeof(string); if (Data.TypeHelper.IsNumber(dc.DataType)) { datatype = typeof(decimal); } else if (typeof(DateTime) == dc.DataType) { datatype = typeof(DateTime); } CreateProperty(typeBuilder, dc.Key, datatype); //if (addSupportProperties) //{ // CreateSupportProperty(typeBuilder, dc.ColumnName, "Required", Type.GetType("System.Boolean"), true, false); // CreateSupportProperty(typeBuilder, dc.ColumnName, "HasError", Type.GetType("System.Boolean"), true, false); // CreateSupportProperty(typeBuilder, dc.ColumnName, "Error", Type.GetType("System.String"), true, false); // CreateSupportProperty(typeBuilder, dc.ColumnName, "DataSourceNullValue", Type.GetType("System.String"), true, true); //} } //if (addSupportProperties) //{ // CreateSupportProperty(typeBuilder, null, "HasNoErrors", Type.GetType("System.Boolean"), true, false); //} return(typeBuilder.CreateType()); }
/// <summary> /// Generates bindable object /// </summary> /// <param name="dataRow">DataRow instance</param> /// <param name="addSupportProperties">True for generating properties with suffixes Required, Error, and HasError</param> /// <returns>Generated bindable object</returns> public static BindableObject GetBindableData(DataCollection data, Type dtoType) { var obj = Activator.CreateInstance(dtoType, data);//, new object[] { dataRow } return((BindableObject)obj); }
public BindableObject(DataCollection collection) { _dataCollection = collection; }