public static DataTable ToTable(List <BsonDocument> result) { DataTable dt = new DataTable(); if (result == null) { return(dt); } Dictionary <string, Type> cols = new Dictionary <string, Type>(); int rowindex = 0; foreach (var item in result) { foreach (BsonDocument val in item.Values) { BsonElement be = val.GetElement(0); BsonValue bv = val.GetValue(be.Name); if (!cols.ContainsKey(be.Name)) { dt.Columns.Add(be.Name, bv.GetType()); cols.Add(be.Name, bv.GetType()); } } DataRow dr = dt.NewRow(); foreach (BsonDocument val in item.Values) { BsonElement be = val.GetElement(0); BsonValue bv = val.GetValue(be.Name); dr[be.Name] = bv; } dt.Rows.Add(dr); rowindex++; } return(dt); }
private static object ConvertBsonValue(BsonValue bsonValue, ResourceType resourceType, ResourceProperty resourceProperty, string propertyName, MongoMetadata mongoMetadata) { if (bsonValue == null) { return(null); } object propertyValue = null; bool convertValue; if (bsonValue.GetType() == typeof(BsonDocument)) { var bsonDocument = bsonValue.AsBsonDocument; if (IsCsharpNullDocument(bsonDocument)) { convertValue = false; } else { propertyValue = CreateDSPResource(bsonDocument, mongoMetadata, propertyName, MongoMetadata.GetQualifiedTypePrefix(resourceType.Name)); convertValue = true; } } else if (bsonValue.GetType() == typeof(BsonArray)) { var bsonArray = bsonValue.AsBsonArray; if (bsonArray != null && bsonArray.Count > 0) { propertyValue = ConvertBsonArray(bsonArray, resourceType, propertyName, mongoMetadata); } convertValue = false; } else if (bsonValue.GetType() == typeof(BsonNull) && resourceProperty.Kind == ResourcePropertyKind.Collection) { propertyValue = ConvertBsonArray(new BsonArray(0), resourceType, propertyName, mongoMetadata); convertValue = false; } else { propertyValue = ConvertRawValue(bsonValue); convertValue = true; } if (propertyValue != null && convertValue) { var propertyType = resourceProperty.ResourceType.InstanceType; Type underlyingNonNullableType = Nullable.GetUnderlyingType(resourceProperty.ResourceType.InstanceType); if (underlyingNonNullableType != null) { propertyType = underlyingNonNullableType; } propertyValue = Convert.ChangeType(propertyValue, propertyType); } return(propertyValue); }
private static Type ResolveProviderType(BsonValue elementValue, bool isKey) { if (elementValue.GetType() == typeof(BsonArray) || elementValue.GetType() == typeof(BsonDocument)) { return(elementValue.GetType()); } else if (BsonTypeMapper.MapToDotNetValue(elementValue) != null) { return(GetRawValueType(elementValue, isKey)); } else { return(null); } }
public BsonValueMemberProvider(BsonValue value) { this.mValue = value; this.mPropsToWrite = mValue.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(p => !IgnoredProperties.Contains(p.Name) && p.GetIndexParameters().Length == 0) .ToArray(); }
private static TreeListNode GetNode(BsonValue element) { TreeListNode node = new TreeListNode(); if (element.GetType() == typeof(BsonNull)) { node.SubItems.Add(dataTypeMapping[typeof(BsonNull)]); node.SubItems.Add(dataTypeMapping[typeof(BsonNull)]); } else if (element.GetType() == typeof(BsonObjectId)) { node.SubItems.Add($"ObjectId(\"{element.ToString()}\")"); node.SubItems.Add("ObjectId"); } else if (element.GetType() == typeof(BsonDocument)) { BsonDocument innerBsonDocument = (BsonDocument)element; node.SubItems.Add($"{{ { innerBsonDocument.ElementCount} {(innerBsonDocument.ElementCount == 1 ? UiConstants.Field : UiConstants.Fields)} }}"); node.SubItems.Add("Document"); var nodeChildren = GetChildren(innerBsonDocument); node.Nodes.AddRange(nodeChildren.ToArray()); } else if (element.GetType() == typeof(BsonArray)) { BsonArray innerArray = (BsonArray)element; node.SubItems.Add($"[ { innerArray.Count} {(innerArray.Count == 1 ? UiConstants.Element : UiConstants.Elements)} ]"); node.SubItems.Add("Array"); var nodeChildren = GetArrayChildren(innerArray); node.Nodes.AddRange(nodeChildren.ToArray()); } else { node.SubItems.Add(element.ToString()); node.SubItems.Add(dataTypeMapping[element.GetType()]); } return(node); }
public static JToken ToJson(this BsonValue source) { switch (source.BsonType) { case BsonType.Document: return(source.AsBsonDocument.ToJson()); case BsonType.Array: return(source.AsBsonArray.ToJson()); case BsonType.Double: return(new JValue(source.AsDouble)); case BsonType.String: return(new JValue(source.AsString)); case BsonType.Boolean: return(new JValue(source.AsBoolean)); case BsonType.DateTime: return(new JValue(source.ToUniversalTime())); case BsonType.Int32: return(new JValue(source.AsInt32)); case BsonType.Int64: return(new JValue(source.AsInt64)); case BsonType.Decimal128: return(new JValue(source.AsDecimal)); case BsonType.Binary: return(new JValue(source.AsBsonBinaryData.Bytes)); case BsonType.Null: return(JValue.CreateNull()); case BsonType.Undefined: return(JValue.CreateUndefined()); } throw new NotSupportedException($"Cannot convert {source.GetType()} to Json."); }
public BsonValueMemberProvider(BsonValue value) { this.mValue = value; this.mPropsToWrite = mValue.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(p => !IgnoredProperties.Contains(p.Name) && p.GetIndexParameters().Length == 0) .ToArray(); }
private static Type ResolveProviderType(BsonValue elementValue, bool isKey) { if (elementValue.GetType() == typeof(BsonArray) || elementValue.GetType() == typeof(BsonDocument)) { return elementValue.GetType(); } else if (BsonTypeMapper.MapToDotNetValue(elementValue) != null) { return GetRawValueType(elementValue, isKey); } else { return null; } }
/// <summary> /// Convert type /// </summary> /// <typeparam name="T"></typeparam> /// <param name="self"></param> /// <param name="name"></param> /// <param name="defaultValue"></param> /// <param name="required">value is required</param> /// <exception cref="ArgumentException">throw if value is not present and required</exception> /// <returns></returns> public static T Get <T>(this BsonDocument self, string name, T defaultValue = default, bool required = true) { Verify.IsNotNull(nameof(self), self); Verify.IsNotEmpty(nameof(name), name); if (!self.Contains(name)) { if (required) { throw new ArgumentException($"Field {name} does not exist and required"); } return(defaultValue); } BsonValue value = self[name]; if (typeof(T) == typeof(string)) { return((T)Convert.ChangeType((string)value, typeof(T))); } if (typeof(T) == typeof(bool)) { return((T)Convert.ChangeType((bool)value, typeof(T))); } if (typeof(T) == typeof(bool?)) { return((T)Convert.ChangeType((bool?)value, typeof(T))); } if (typeof(T) == typeof(int)) { return((T)Convert.ChangeType((int)value, typeof(T))); } if (typeof(T) == typeof(int?)) { return((T)Convert.ChangeType((int?)value, typeof(T))); } if (typeof(T) == typeof(long)) { return((T)Convert.ChangeType((long)value, typeof(T))); } if (typeof(T) == typeof(long?)) { return((T)Convert.ChangeType((long?)value, typeof(T))); } if (typeof(T) == typeof(decimal)) { return((T)Convert.ChangeType((decimal)value, typeof(T))); } if (typeof(T) == typeof(decimal?)) { return((T)Convert.ChangeType((decimal?)value, typeof(T))); } if (typeof(T) == typeof(DateTime)) { return((T)Convert.ChangeType((DateTime)value, typeof(T))); } if (typeof(T) == typeof(DateTime?)) { return((T)Convert.ChangeType((DateTime?)value, typeof(T))); } if (typeof(T) == typeof(Guid)) { return((T)Convert.ChangeType((Guid)value, typeof(T))); } if (typeof(T) == typeof(Guid?)) { return((T)Convert.ChangeType((Guid?)value, typeof(T))); } throw new InvalidOperationException($"Unsupported type: {value.GetType()}"); }
private static object ConvertBsonValue(BsonValue bsonValue, ResourceType resourceType, ResourceProperty resourceProperty, string propertyName, MongoMetadata mongoMetadata) { if (bsonValue == null) return null; object propertyValue = null; bool convertValue; if (bsonValue.GetType() == typeof(BsonDocument)) { var bsonDocument = bsonValue.AsBsonDocument; if (IsCsharpNullDocument(bsonDocument)) { convertValue = false; } else { propertyValue = CreateDSPResource(bsonDocument, mongoMetadata, propertyName, MongoMetadata.GetQualifiedTypePrefix(resourceType.Name)); convertValue = true; } } else if (bsonValue.GetType() == typeof(BsonArray)) { var bsonArray = bsonValue.AsBsonArray; if (bsonArray != null && bsonArray.Count > 0) propertyValue = ConvertBsonArray(bsonArray, resourceType, propertyName, mongoMetadata); convertValue = false; } else if (bsonValue.GetType() == typeof(BsonNull) && resourceProperty.Kind == ResourcePropertyKind.Collection) { propertyValue = ConvertBsonArray(new BsonArray(0), resourceType, propertyName, mongoMetadata); convertValue = false; } else { propertyValue = ConvertRawValue(bsonValue); convertValue = true; } if (propertyValue != null && convertValue) { var propertyType = resourceProperty.ResourceType.InstanceType; Type underlyingNonNullableType = Nullable.GetUnderlyingType(resourceProperty.ResourceType.InstanceType); if (underlyingNonNullableType != null) { propertyType = underlyingNonNullableType; } propertyValue = Convert.ChangeType(propertyValue, propertyType); } return propertyValue; }
private static Type ResolveProviderType(BsonValue elementValue) { if (elementValue.GetType() == typeof(BsonArray) || elementValue.GetType() == typeof(BsonDocument)) { return elementValue.GetType(); } else if (elementValue.RawValue != null) { switch (elementValue.BsonType) { case BsonType.DateTime: return typeof(DateTime); default: return elementValue.RawValue.GetType(); } } return null; }
public static BsonDocument zCreateDocuments(this BsonValue value, IEnumerable <string> names) { if (value == null) { throw new PBException("unable to create documents from a null BsonValue"); } foreach (string name in names) { if (!(value is BsonDocument)) { throw new PBException("unable to create documents, value is not a document ({0})", value.GetType().zGetTypeName()); } BsonDocument document = (BsonDocument)value; if (!document.Contains(name)) { value = new BsonDocument(); document.Add(name, value); } else { value = document[name]; } } return(value.AsBsonDocument); }
private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { e.DrawDefault = false; Rectangle rect = e.Bounds; if (rect.Height == 0) { ///在展开节点的时候会出现根节点绘制错误的问题 return; } if ((e.State & TreeNodeStates.Selected) != 0) { if ((e.State & TreeNodeStates.Focused) != 0) { e.Graphics.FillRectangle(SystemBrushes.Highlight, rect); } else { e.Graphics.FillRectangle(SystemBrushes.Control, rect); } } else { e.Graphics.FillRectangle(Brushes.White, rect); } int IndentWidth = DatatreeView.Indent * e.Node.Level + 25; e.Graphics.DrawRectangle(SystemPens.Control, rect); Rectangle StringRect = new Rectangle(e.Bounds.X + IndentWidth, e.Bounds.Y, colName.Width - IndentWidth, e.Bounds.Height); String TreeNameString = e.Node.Text; if (TreeNameString.EndsWith(MongoDBHelper.Array_Mark)) { //Array_Mark 在计算路径的时候使用,不过,在表示的时候,则不能表示 TreeNameString = TreeNameString.Substring(0, TreeNameString.Length - MongoDBHelper.Array_Mark.Length); } if (TreeNameString.EndsWith(MongoDBHelper.Document_Mark)) { //Document_Mark 在计算路径的时候使用,不过,在表示的时候,则不能表示 TreeNameString = TreeNameString.Substring(0, TreeNameString.Length - MongoDBHelper.Document_Mark.Length); } //感谢cyrus的建议,选中节点的文字表示,底色变更 if ((e.State & TreeNodeStates.Selected) != 0 && (e.State & TreeNodeStates.Focused) != 0) { e.Graphics.DrawString(TreeNameString, this.Font, new SolidBrush(SystemColors.HighlightText), StringRect); } else { e.Graphics.DrawString(TreeNameString, this.Font, new SolidBrush(Color.Black), StringRect); } BsonElement mElement = e.Node.Tag as BsonElement; BsonValue mValue = e.Node.Tag as BsonValue; //画框 if (e.Node.GetNodeCount(true) > 0 || (mElement != null && (mElement.Value.IsBsonDocument || mElement.Value.IsBsonArray))) { //感谢Cyrus测试出来的问题:RenderWithVisualStyles应该加上去的。 if (VisualStyleRenderer.IsSupported && Application.RenderWithVisualStyles) { int LeftPoint = e.Bounds.X + IndentWidth - 20; //感谢 Shadower http://home.cnblogs.com/u/14697/ 贡献的代码 var thisNode = e.Node; var glyph = thisNode.IsExpanded ? VisualStyleElement.TreeView.Glyph.Opened : VisualStyleElement.TreeView.Glyph.Closed; var vsr = new VisualStyleRenderer(glyph); vsr.DrawBackground(e.Graphics, new Rectangle(LeftPoint, e.Bounds.Y + 4, 16, 16)); } else { int LeftPoint = e.Bounds.X + IndentWidth - 20; e.Graphics.DrawRectangle(new Pen(Color.Black), new Rectangle(LeftPoint, e.Bounds.Y + 4, 12, 12)); Point LeftMid = new Point(LeftPoint + 2, e.Bounds.Y + 10); Point RightMid = new Point(LeftPoint + 10, e.Bounds.Y + 10); Point TopMid = new Point(LeftPoint + 6, e.Bounds.Y + 6); Point BottomMid = new Point(LeftPoint + 6, e.Bounds.Y + 14); e.Graphics.DrawLine(new Pen(Color.Black), LeftMid, RightMid); if (!e.Node.IsExpanded) { e.Graphics.DrawLine(new Pen(Color.Black), TopMid, BottomMid); } } } for (int intColumn = 1; intColumn < 3; intColumn++) { rect.Offset(this.listView.Columns[intColumn - 1].Width, 0); rect.Width = this.listView.Columns[intColumn].Width; e.Graphics.DrawRectangle(SystemPens.Control, rect); if (mElement != null || mValue != null) { string strColumnText = String.Empty; if (intColumn == 1) { if (mElement != null) { if (!mElement.Value.IsBsonDocument && !mElement.Value.IsBsonArray) { strColumnText = mElement.Value.ToString(); } } else { if (mValue != null) { if (!mValue.IsBsonDocument && !mValue.IsBsonArray) { if (e.Node.Level > 0) { //根节点有Value,可能是ID,用来取得选中节点的信息 strColumnText = mValue.ToString(); } } //Type这里已经有表示Type的标识了,这里就不重复显示了。 //else //{ //if (mValue.IsBsonDocument) { strColumnText = MongoDBHelper.Document_Mark; } //if (mValue.IsBsonArray) { strColumnText = MongoDBHelper.Array_Mark; } //} } } } else { if (mElement != null) { strColumnText = mElement.Value.GetType().Name.Substring(4); } else { strColumnText = mValue.GetType().Name.Substring(4); } } TextFormatFlags flags = TextFormatFlags.EndEllipsis; switch (this.listView.Columns[intColumn].TextAlign) { case HorizontalAlignment.Center: flags |= TextFormatFlags.HorizontalCenter; break; case HorizontalAlignment.Left: flags |= TextFormatFlags.Left; break; case HorizontalAlignment.Right: flags |= TextFormatFlags.Right; break; default: break; } rect.Y++; if ((e.State & TreeNodeStates.Selected) != 0 && (e.State & TreeNodeStates.Focused) != 0) { TextRenderer.DrawText(e.Graphics, strColumnText, e.Node.NodeFont, rect, SystemColors.HighlightText, flags); } else { TextRenderer.DrawText(e.Graphics, strColumnText, e.Node.NodeFont, rect, e.Node.ForeColor, e.Node.BackColor, flags); } rect.Y--; } } }