public string GenerateDDL(Type tp, SchemaContext context) { // Fix type name => '.' to '_' TableMeta tableMeta = new TableMeta(tp.Name.Replace('.', '_'), context); // Loop through all obj properties // Leaf -> Column // Complex -> Build path / Cache mapping PropertyInfo[] properties = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in properties) { Type unerLyingType = TypeInterrogator.GetUnderlyingType(pi.PropertyType); //if (IsSkippedType(unerLyingType)) // return; if (TypeInterrogator.IsSingleValueType(unerLyingType)) { tableMeta.ColMetaList.Add(BuildColumnMeta(pi, unerLyingType)); } //else if (TypeInterrogator.IsCollectionType(unerLyingType)) // BuildCollectionCtrl(pi, pi.PropertyType, typeObj); //else // BuildCompositeCtrl(unerLyingType, pi.GetValue(typeObj, null)); } return(tableMeta.BuildCreateDDL()); }
private ColumnMeta BuildColumnMeta(PropertyInfo pi, Type tp) { ColumnMeta colMeta = null; if (TypeInterrogator.IsNumericType(tp)) { colMeta = new ColumnMeta(pi.Name, DataTypeEnum.NUMERIC, new SqlsvrDTDictionary()); colMeta.DataType.Precision = 10; colMeta.DataType.Scale = 2; } else if (TypeInterrogator.IsBoolType(tp)) { colMeta = new ColumnMeta(pi.Name, DataTypeEnum.Bit, new SqlsvrDTDictionary()); } else if (TypeInterrogator.IsDateTimeType(tp)) { colMeta = new ColumnMeta(pi.Name, DataTypeEnum.DATETIME, new SqlsvrDTDictionary()); } else if (TypeInterrogator.IsEnumType(tp)) { colMeta = new ColumnMeta(pi.Name, DataTypeEnum.Int32, new SqlsvrDTDictionary()); //colMeta.DataType.Scale = 2; } else if (TypeInterrogator.IsStringType(tp)) { colMeta = new ColumnMeta(pi.Name, DataTypeEnum.AnsiString, new SqlsvrDTDictionary()); colMeta.DataType.StringLength = 256; } return(colMeta); }
private void BuildCompositeCtrl(Type tp, object obj, ObjIdentifier objId) { PropertyInfo[] properties = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in properties) { Type unerLyingType = TypeInterrogator.GetUnderlyingType(pi.PropertyType); if (IsSkipped(pi, unerLyingType)) { continue; } if (IsLeafCtrl(unerLyingType)) { BuildLeafCtrl(pi, unerLyingType, obj, objId); } else if (IsCollectionCtrl(unerLyingType)) { BuildCollectionCtrl(pi, pi.PropertyType, obj, objId); } else { BuildCompositeCtrl(unerLyingType, pi.GetValue(obj, null), new ObjIdentifier(pi.Name, objId)); } } }
public static Type BuildKeyValueType(Type dictionaryT) { if (!TypeInterrogator.IsDictionaryType(dictionaryT)) { System.Diagnostics.Debug.Assert(false); return(null); } return(KVType.MakeGenericType(TypeInterrogator.GetItemTypes(dictionaryT))); }
public static IEnumerable ToValues(Type enumType, IEnumerable <string> nameCollection) { Debug.Assert(TypeInterrogator.IsEnumType(enumType)); IList values = (IList)GenBuilder.BuildInstance(typeof(List <>), new Type[] { enumType }); foreach (string name in nameCollection) { values.Add(Enum.Parse(enumType, name)); } return(values); }
public static IList ToList(IEnumerable source, Type itemT) { if (TypeInterrogator.IsListType(source.GetType())) { return((IList)source); } IList target = (IList)GenBuilder.BuildInstance(typeof(List <>), new Type[] { itemT }); foreach (object it in source) { target.Add(it); } return(target); }
public static Array ToArray(IEnumerable source, Type itemT) { if (TypeInterrogator.IsArrayType(source.GetType())) { return((Array)source); } int count = GetCount(source); Array target = Array.CreateInstance(itemT, count); int ind = 0; foreach (object it in source) { target.SetValue(it, ind++); } return(target); }
public int SetObject(IEnumerable objCollection, CellBorderStyle borderStyle) { _borderStyle = borderStyle; Type objType = objCollection.GetType(); if (TypeInterrogator.IsDictionaryType(objType)) { _iter = new Traverser(CollConverter.ToList(objCollection, GenBuilder.BuildKeyValueType(objType))); } else { _iter = new Traverser(CollConverter.ToList(objCollection)); } _objControl.SetObject(_iter.First, null, _borderStyle); return(0); }
public object ToPropertyValue(object cv) { Decimal?dcm = null; if (TypeInterrogator.IsStringType(cv.GetType())) { dcm = Decimal.Parse(cv.ToString()); } else { dcm = (Decimal)cv; } if (_itemType == typeof(double)) { return(Decimal.ToDouble((Decimal)dcm)); } else if (_itemType == typeof(float)) { return(Decimal.ToSingle((Decimal)dcm)); } else if (_itemType == typeof(int)) { return(Decimal.ToInt32((Decimal)dcm)); } else if (_itemType == typeof(long)) { return(Decimal.ToInt64((Decimal)dcm)); } else if (_itemType == typeof(uint)) { return(Decimal.ToUInt32((Decimal)dcm)); } else if (_itemType == typeof(ulong)) { return(Decimal.ToUInt64((Decimal)dcm)); } else { return(null); } }
/// <summary> /// Return true if any of the following conditions is satisfied: /// 1. Property is Delegate / Event /// 2. Can not read or write /// 3. Marked with XmlIgnore /// </summary> private bool IsSkipped(PropertyInfo pi, Type tp) { if (TypeInterrogator.IsDelegateType(tp) || TypeInterrogator.IsEventType(tp)) { return(true); } if (!pi.CanRead || !pi.CanWrite) // skip readonly properties. may revisit this { return(true); } System.ComponentModel.BrowsableAttribute brw = AttributeInterrogator.GetAttribute <System.ComponentModel.BrowsableAttribute>(pi); if (brw != null && !brw.Browsable) { return(true); } return(false); }
public void IsValueTypeTest() { Eclipse?it = new Eclipse(); Assert.IsTrue(TypeInterrogator.IsValueType(it.GetType())); SoundLevle sl = SoundLevle.Deaf; Assert.IsTrue(TypeInterrogator.IsValueType(sl.GetType())); double x = 6.0; Assert.IsTrue(TypeInterrogator.IsNumericType(x.GetType())); Assert.IsTrue(TypeInterrogator.IsStrcutType(typeof(Eclipse))); Assert.IsFalse(TypeInterrogator.IsStrcutType(typeof(EmployeeBase <int>))); Assert.IsFalse(TypeInterrogator.IsStrcutType(typeof(EmployeeBase <>))); Assert.IsTrue(TypeInterrogator.IsReferenceType(typeof(EmployeeBase <>))); Assert.IsTrue(TypeInterrogator.IsReferenceType(typeof(Employee.HasNetWork))); Assert.IsTrue(TypeInterrogator.IsDelegateType(typeof(Employee.HasNetWork))); }
private void LoadComposite(Type tp, object typeObj, string objName) { PropertyInfo[] properties = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in properties) { Type unerLyingType = TypeInterrogator.GetUnderlyingType(pi.PropertyType); if (IsSkipped(pi, unerLyingType)) { continue; } if (IsLeafCtrl(unerLyingType) || IsCollectionCtrl(unerLyingType)) { PropertyCtrlInfo pci = _controlMap[PropertyInterrogator.GetFullName(pi)]; pci.ParentObject = typeObj; pci.Binder.Value = pi.GetValue(typeObj, null); } else { LoadComposite(unerLyingType, pi.GetValue(typeObj, null), pi.Name); } } }
//[TestMethod()] public void SubTypeTest() { Assert.IsTrue(TypeInterrogator.IsSubclass(typeof(EmployeeBase <>), typeof(Manager))); Assert.IsTrue(TypeInterrogator.IsSubclass(typeof(EmployeeBase <>), typeof(TemplatedInspector <int>))); Employee inspector = new TemplatedInspector <Insight>(); Assert.IsTrue(TypeInterrogator.IsSubclass(typeof(EmployeeBase <>), inspector.GetType())); Employee emp = new Employee(); Assert.IsFalse(emp.GetType().IsGenericType); Assert.IsTrue(TypeInterrogator.IsSubclass(emp.GetType(), inspector.GetType())); Assert.IsTrue(TypeInterrogator.IsSubclass(typeof(EmployeeBase <>), emp.GetType())); Eclipse?it = new Eclipse(); Assert.IsFalse(TypeInterrogator.IsSubclass(typeof(EmployeeBase <>), it.GetType())); List <EmployeeBase <int> > xlist = new List <EmployeeBase <int> >(); Assert.IsTrue(xlist.GetType().IsGenericType); }
public RadioButtonBinder(Type enumType, int value) { Debug.Assert(TypeInterrogator.IsEnumType(enumType)); Init(enumType, value); }
private bool IsCollectionCtrl(Type tp) { return(TypeInterrogator.IsCollectionType(tp)); }
private bool IsLeafCtrl(Type tp) { return(tp == typeof(System.Data.DataTable) || TypeInterrogator.IsDictionaryType(tp) || TypeInterrogator.IsSingleValueType(tp)); }
private void BuildCollectionCtrl(PropertyInfo pi, Type tp, object parentObj, ObjIdentifier parentId) { PropertyCtrlInfo ci = BuildControlInfo(pi, parentObj); Page page = (ci.Layout.IsAdvanced) ? Macros.SafeGet(ref _advancedPage) : _mainPage; object piValue = (parentObj == null) ? null : pi.GetValue(parentObj, null); Type itemT = TypeInterrogator.GetItemType(tp); if (TypeInterrogator.IsSingleValueType(itemT)) { if (TypeInterrogator.IsEnumType(itemT)) { BitFlagsPCV converter = new BitFlagsPCV(itemT); ci.Binder = new CheckedListBoxBinder(converter.ToControlValue(itemT), converter.ToControlValue((IEnumerable)piValue)); ci.Converter = converter; } else if (TypeInterrogator.IsStringType(itemT)) { if (TypeInterrogator.IsArrayType(tp)) { ci.Converter = new ArrayPCV(itemT); } else if (TypeInterrogator.IsListType(tp)) { ci.Converter = new ListPCV(itemT); } if (_objHints.ValueHints != null && _objHints.ValueHints.ContainsKey(pi.Name)) { ci.Binder = new ListBoxBinder(_objHints.ValueHints[pi.Name], (IEnumerable <string>)piValue); } else { ci.Binder = new ListBoxBinder((IEnumerable <string>)piValue); } } else if (TypeInterrogator.IsNumericType(itemT)) { if (TypeInterrogator.IsArrayType(tp)) { ci.Converter = new ArrayPCV(itemT); } else if (TypeInterrogator.IsListType(tp)) { ci.Converter = new ListPCV(itemT); } if (_objHints.ValueHints != null && _objHints.ValueHints.ContainsKey(pi.Name)) { ci.Binder = new ListBoxBinder(_objHints.ValueHints[pi.Name], CollConverter.ToStringArray((IEnumerable)piValue)); } else { ci.Binder = new ListBoxBinder(CollConverter.ToStringArray((IEnumerable)piValue)); } } else { Debug.Assert(false); } } else { ci.Binder = new ObjCollectionBinder((IEnumerable)piValue, null); } AddControlInfo(page, ci, parentId); }
private void BuildLeafCtrl(PropertyInfo pi, Type tp, object parentObj, ObjIdentifier parentId) { PropertyCtrlInfo ci = BuildControlInfo(pi, parentObj); Page page = (ci.Layout.IsAdvanced) ? Macros.SafeGet(ref _advancedPage) : _mainPage; object piValue = (parentObj == null) ? null : pi.GetValue(parentObj, null); if (TypeInterrogator.IsNumericType(tp)) { DecimalPCV converter = new DecimalPCV(tp); ci.Converter = converter; NumericUpDownHint upDownHint = AttributeInterrogator.GetAttribute <NumericUpDownHint>(pi); if (upDownHint != null) { ci.Binder = new NumericUpDownSingleBinder((piValue == null) ? Decimal.MinValue : Decimal.Parse(piValue.ToString()), upDownHint); } else { TextBoxHint tbHint = AttributeInterrogator.GetAttribute <TextBoxHint>(pi); if (tbHint == null) { tbHint = new TextBoxHint(); } tbHint.CharsAccepted = TextBoxHint.Numbers; ci.Binder = new TextBoxBinder((piValue == null) ? null : piValue.ToString(), tbHint); } } else if (TypeInterrogator.IsBoolType(tp)) { ci.Binder = new CheckBoxBinder(ci.Layout.Label, (piValue == null) ? false : (bool)piValue); ci.Layout.Label = null; ci.Layout.Section = Section.Footer; } else if (TypeInterrogator.IsDateTimeType(tp)) { ci.Binder = new DateTimePickerBinder((piValue == null) ? DateTime.Today : (DateTime)piValue, null); } else if (TypeInterrogator.IsEnumType(tp)) { const int MaxRadioButtonCount = 3; IEnumerable <string> names = EnumConverter.ToNames(tp, true); if (CollConverter.GetCount(names) <= MaxRadioButtonCount) { ci.Binder = new RadioButtonBinder(tp, (piValue == null) ? -1 : (int)piValue); } else { EnumPCV converter = new EnumPCV(tp); ci.Binder = new ComboBoxBinder(piValue.ToString(), names, ComboBoxStyle.DropDownList); ci.Converter = converter; } } else if (TypeInterrogator.IsStringType(tp)) { if (_objHints.ValueHints != null && _objHints.ValueHints.ContainsKey(pi.Name)) { ci.Binder = new ComboBoxBinder((piValue == null) ? null : piValue.ToString(), _objHints.ValueHints[pi.Name], ComboBoxStyle.DropDown); } else { LayoutHint lhint = AttributeInterrogator.GetAttribute <LayoutHint>(pi); ci.Binder = new TextBoxBinder((piValue == null) ? null : piValue.ToString(), lhint); } } else if (tp == typeof(System.Data.DataTable)) { ci.Binder = new DataGridViewBinder((piValue == null) ? null : piValue as System.Data.DataTable); ci.Layout.ColumnCount = 2; } else if (TypeInterrogator.IsDictionaryType(tp)) { DictionaryPCV converter = new DictionaryPCV(); Dictionary <string, string> converted = (piValue == null) ? null : converter.ToControlValue(piValue); ci.Binder = new DictionaryCtrlBinder(converted, _objHints.DictionaryHint); ci.Converter = converter; ci.Layout.Label = null; ci.Layout.ColumnCount = 2; ci.Layout.RowCount = (converted != null) ? (int)((converted.Count + 1) * 1.5) : 2; } AddControlInfo(page, ci, parentId); }
public static IList ToList <ItemT>(IEnumerable <ItemT> source) { return(ToList(source, TypeInterrogator.GetItemType(source.GetType()))); }
public static Array ToArray <ItemT>(IEnumerable <ItemT> source) { return(ToArray(source, TypeInterrogator.GetItemType(source.GetType()))); }