static void Main(string[] args)
    {
        MyDataTable t1 = new MyDataTable();

        t1.Columns.Add(new DataColumn("Name", typeof(string)));
        t1.Columns.Add(new DataColumn("DateOfBirth", typeof(DateTime)));
        MyDataRow r1 = t1.NewRow() as MyDataRow;

        r1["Name"]        = "Bob";
        r1["DateOfBirth"] = new DateTime(1970, 5, 12);
        t1.Rows.Add(r1);
    }
Пример #2
0
        /// <summary>
        /// This is currently designed to only work for primitive types as I am only using it for
        /// generic export. It can be made better.
        /// </summary>
        /// <typeparam name="T">This may be a type generated at runtime</typeparam>
        /// <param name="items"></param>
        /// <param name="validColumns">NB: No need to put S/No column. It's added automatically</param>
        /// <param name="generatedType">If null, it defaults to typeof(T). If <paramref name="T"/> is a generated type, supply the type here to aid in the resolution, because it will read as Object.</param>
        /// <returns></returns>
        public static MyDataTable ToDataTable <T>(this IList <T> items, IList <MyDataColumn> validColumns, Type generatedType = null)
        {
            if (generatedType == null)
            {
                generatedType = typeof(T);
            }
            var dt = new MyDataTable(generatedType.Name);

            dt.Columns.Add("No", new MyDataColumn("No", typeof(int)));
            foreach (var item in validColumns)
            {
                dt.Columns.Add(item.ColumnName, item);
            }
            string propName;

            for (int i = 0; i < items.Count; i++)
            {
                var row = dt.NewRow();
                row["No"] = i + 1;
                foreach (var col in dt.Columns)
                {
                    if (col.Key == "No")
                    {
                        continue;
                    }

                    propName = col.Value.PropertyName;
                    object theVal = null;
                    try
                    {
                        theVal = generatedType.GetProperty(propName).GetValue(items[i], null);
                    }
                    catch (TargetException)
                    {
                        theVal = null;
                    }
                    catch (RuntimeBinderException)
                    {
                        theVal = null;
                    }
                    row[col.Key] = theVal;
                }
                dt.Rows.Add(row);
            }
            return(dt);
        }
Пример #3
0
        private static void BuildNewRow(MyDataTable dt, PropertyInfo[] props, object item, long savedBy)
        {
            var row = dt.NewRow();

            for (int j = 0; j < props.Length; j++)
            {
                var prop = props[j];
                if (!dt.Columns.ContainsKey(prop.GetPropertyName()))
                {
                    if (prop.PropertyType.IsClass && prop.PropertyType != typeof(string))
                    {
                        var innerProps = prop.PropertyType.GetProperties();
                        if (innerProps.Any(x => x.Name == "Id")) // Handle properties mapped as Reference
                        {
                            string columnName = string.Format("{0}Id", prop.Name.StartsWith("The") ? prop.Name.Remove(0, 3) : prop.Name);
                            if (!dt.Columns.ContainsKey(columnName))
                            {
                                continue;
                            }

                            object theRefValue;
                            try
                            {
                                dynamic propValue = prop.GetValue(item, null);
                                theRefValue = propValue.Id;
                            }
                            catch (TargetException)
                            {
                                theRefValue = null;
                            }
                            catch (RuntimeBinderException)
                            {
                                theRefValue = null;
                            }
                            catch (Exception) { throw; }
                            if (theRefValue == null)
                            {
                                row[columnName] = System.DBNull.Value;
                                continue;
                            }
                            row[columnName] = theRefValue;
                        }
                        else //Handling Compositely mapped Class properties
                        {
                            var modifyFieldNameAttr = prop.GetCustomAttribute <CompositeMappingModifyFieldNamesAttribute>();

                            for (int k = 0; k < innerProps.Length; k++)
                            {
                                var    innerProp     = innerProps[k];
                                string innerPropName = null;
                                if (modifyFieldNameAttr.UseAllPPropertiesWithTHeirDefaultNames)
                                {
                                    if (!dt.Columns.ContainsKey(innerProp.GetPropertyName()))
                                    {
                                        continue;
                                    }

                                    innerPropName = innerProp.GetPropertyName();
                                }
                                else
                                {
                                    if (!modifyFieldNameAttr.FieldAndPropNames.ContainsKey(innerProp.GetPropertyName()))
                                    {
                                        continue;
                                    }

                                    innerPropName = modifyFieldNameAttr.FieldAndPropNames[innerProp.GetPropertyName()];
                                    if (!dt.Columns.ContainsKey(innerPropName))
                                    {
                                        continue;
                                    }
                                }
                                object theInnerValue;
                                try
                                {
                                    theInnerValue = innerProp.GetValue(prop.GetValue(item, null), null);
                                    if (theInnerValue == null && innerProp.IsTypeNullable())
                                    {
                                        theInnerValue = Nullable.GetUnderlyingType(innerProp.PropertyType).GetDefaultValue(); //innerProp.PropertyType.GenericTypeArguments[0].GetDefaultValue();
                                    }
                                }
                                catch (TargetException)
                                {
                                    theInnerValue = null;
                                }
                                catch (Exception) { throw; }
                                if (string.IsNullOrWhiteSpace(innerPropName))
                                {
                                    innerPropName = modifyFieldNameAttr.FieldAndPropNames[innerProp.GetPropertyName()];
                                }
                                if (theInnerValue == null)
                                {
                                    row[innerPropName] = DBNull.Value;
                                    continue;
                                }

                                if (innerProp.PropertyType == typeof(DateTime) && theInnerValue != null && Convert.ToDateTime(theInnerValue) < new DateTime(1900, 1, 1))
                                {
                                    theInnerValue = new DateTime(1900, 1, 1);
                                }

                                if (innerProp.PropertyType.IsEnum && theInnerValue != null)
                                {
                                    //DO NOT use Enum.Parse(innerProp.PropertyType, theInnerValue.ToString()); since we store enums as integers conventionally
                                    theInnerValue = (int)theInnerValue;
                                }
                                row[innerPropName] = theInnerValue;
                            }
                        }
                        continue;
                    }
                    continue;
                }

                object theValue;
                try
                {
                    if (prop.Name == "DateMigrated")
                    {
                        theValue = DateTime.Now.GetLocalTime();
                    }
                    else if (prop.Name == "SavedBy")
                    {
                        theValue = savedBy;
                    }
                    else
                    {
                        theValue = prop.GetValue(item, null);
                    }
                    if (theValue == null)
                    {
                        if (prop.Name == "DateCreated")
                        {
                            theValue = new DateTime(1900, 1, 1);
                        }
                        else if (prop.Name == "IsDisabled" || prop.Name == "IsDeleted")
                        {
                            theValue = false;
                        }
                        else if (prop.IsTypeNullable())
                        {
                            var mainType = Nullable.GetUnderlyingType(prop.PropertyType);
                            if (mainType == typeof(DateTime))
                            {
                                theValue = new DateTime(1900, 1, 1);
                            }
                            else
                            {
                                theValue = mainType.GetDefaultValue();
                            }
                        }
                    }
                }
                catch (TargetException)
                {
                    theValue = null;
                }
                catch (Exception) { throw; }
                if (theValue == null)
                {
                    row[prop.GetPropertyName()] = DBNull.Value;
                    continue;
                }
                if (prop.PropertyType == typeof(DateTime) && theValue != null && Convert.ToDateTime(theValue) < new DateTime(1900, 1, 1))
                {
                    theValue = new DateTime(1900, 1, 1);
                }
                if (prop.PropertyType.IsEnum && theValue != null)
                {
                    //DO NOT use Enum.Parse(prop.PropertyType, theValue.ToString()); since we store enums as integers conventionally
                    theValue = (int)theValue;
                }

                row[prop.GetPropertyName()] = theValue;
            }

            dt.Rows.Add(row);
        }
Пример #4
0
    static void Main(string[] args)
    {
        MyDataTable t = new MyDataTable();

        t.Rows.Add(t.NewRow());     // <-- Exception here, wrong type (base doesn't count).
    }