private void espMItemCreateNew_Click(object sender, EventArgs e) { TreeView tv = GetCurrentTreeView(); TreeNode node = tv.SelectedNode; TreeNodeInstanceBind bind = node.Tag as TreeNodeInstanceBind; if (bind == null) { ShowEror("节点绑定Tag对象类型不为{0},操作不支持!", typeof(TreeNodeInstanceBind).FullName); } else { TreeNodeInstanceBind newBind = new TreeNodeInstanceBind { IsArrayItem = bind.IsArrayItem, IsESPData = bind.IsESPData, IsFirstNode = bind.IsFirstNode, StoreIndex = bind.StoreIndex, NodeType = bind.NodeType, SubType = bind.SubType }; if (bind.IsESPData) { ESPDataBase instance = Activator.CreateInstance(bind.NodeType) as ESPDataBase; newBind.NodeItem = instance; newBind.StoreLength = instance.GetContentLength(); node.Tag = newBind; TesterPlugConfig.Instance.CurrentPlug.RefreshSelectNode(node, WriteLog); } else { ShowEror("节点绑定Tag对象的值不为{0},操作不支持!", typeof(ESPDataBase).FullName); } } }
void ReportToTreeView(ESPDataBase instance, TreeNodeCollection targetNodes, LogWriter writer, bool appendRoot) { targetNodes.Clear(); Type instanceType = instance.GetType(); string targetObjectType = instanceType.FullName; TreeNode rootNode = null; TreeNodeInstanceBind nodeBind = null; if (appendRoot) { rootNode = new TreeNode(targetObjectType); rootNode.Name = targetObjectType; rootNode.ImageIndex = rootNode.SelectedImageIndex = (int)DataSpecTester.ImageListIcon.Class; nodeBind = new TreeNodeInstanceBind { IsFirstNode = true, IsArrayItem = false, IsESPData = true, NodeItem = instance, StoreIndex = 0, NodeType = instanceType, StoreLength = instance.GetContentLength() }; rootNode.Tag = nodeBind; targetNodes.Add(rootNode); } ObjectTransferOrderAttribute[] configs = new ObjectTransferOrderAttribute[0]; PropertyInfo[] transPropertys = SpecUtil.GetTransferProperties(instance.GetType(), out configs); PropertyInfo lastProperty = null; for (int m = 0, n = transPropertys.Length; m < n; m++) { PropertyInfo pInfo = transPropertys[m]; #region 依据属性依次绑定 TreeNode propertyNode = new TreeNode(string.Format("{0} [{1}]", pInfo.Name, pInfo.PropertyType)); propertyNode.Name = pInfo.Name; propertyNode.ImageIndex = propertyNode.SelectedImageIndex = (int)DataSpecTester.ImageListIcon.Property; if (configs[m].Conditional) { propertyNode.Text = propertyNode.Text + " *"; } object propertyVal = pInfo.GetValue(instance, null); if (appendRoot && rootNode != null) { rootNode.Nodes.Add(propertyNode); } else { targetNodes.Add(propertyNode); } //if (propertyVal == null) continue; nodeBind = new TreeNodeInstanceBind { IsFirstNode = lastProperty == null, IsArrayItem = false, IsESPData = false, NodeItem = propertyVal, NodeType = pInfo.PropertyType, StoreIndex = GetTreeNodeStoreIndex(propertyNode), StoreLength = 0 //TODO }; if (pInfo.PropertyType.IsSubclassOf(typeof(ESPDataBase))) { ESPDataBase subInstance = (ESPDataBase)propertyVal; nodeBind.IsESPData = true; propertyNode.ImageIndex = propertyNode.SelectedImageIndex = (int)DataSpecTester.ImageListIcon.Class; propertyNode.Tag = nodeBind; if (subInstance != null) { //递归子级属性 nodeBind.StoreLength = subInstance.GetContentLength(); ReportToTreeView(subInstance, propertyNode.Nodes, writer, false); } } else { if (pInfo.PropertyType.IsEnum) { nodeBind.StoreLength = SpecUtil.GetCLRTypeByteLength(pInfo.PropertyType); propertyNode.ImageIndex = propertyNode.SelectedImageIndex = (int)DataSpecTester.ImageListIcon.Enum; propertyNode.Text = string.Format("{0} [{1}({2})]", pInfo.Name, pInfo.PropertyType, Enum.GetUnderlyingType(pInfo.PropertyType)); if (configs[m].Conditional) { propertyNode.Text = propertyNode.Text + " *"; } propertyNode.Tag = nodeBind; } else if (pInfo.PropertyType.IsArray) { Type elementType = pInfo.PropertyType.GetElementType(); nodeBind.SubType = elementType; propertyNode.Tag = nodeBind; if (elementType.IsSubclassOf(typeof(ESPDataBase))) { #region 封装对象数组 ESPDataBase[] subItems = (ESPDataBase[])propertyVal; if (subItems != null) { long totalLength = 0, currentLength = 0; for (int p = 0, q = subItems.Length; p < q; p++) { currentLength = subItems[p].GetContentLength(); totalLength += currentLength; TreeNode subPropertyNode = new TreeNode(); subPropertyNode.Name = subItems[p].GetType().FullName + "[" + p + "]"; subPropertyNode.Text = subItems[p].GetType().FullName + "[" + p + "]"; propertyNode.Nodes.Add(subPropertyNode); subPropertyNode.ImageIndex = subPropertyNode.SelectedImageIndex = (int)DataSpecTester.ImageListIcon.Class; subPropertyNode.Tag = new TreeNodeInstanceBind { IsFirstNode = p == 0, IsArrayItem = true, IsESPData = true, NodeItem = subItems[p], SubType = null, NodeType = elementType, StoreIndex = GetTreeNodeStoreIndex(subPropertyNode), StoreLength = currentLength }; ReportToTreeView(subItems[p], subPropertyNode.Nodes, writer, false); } nodeBind.StoreLength = totalLength; } #endregion } else { #region 字节数组 if (propertyVal == null) { propertyVal = new byte[0]; } if (elementType.Equals(typeof(byte))) { nodeBind.StoreLength = ((byte[])propertyVal).LongLength; if (nodeBind.StoreLength > 0 && (instanceType.Equals(typeof(NetworkSwitchRequest)) || instanceType.Equals(typeof(NetworkSwitchResponse))) ) { propertyNode.ImageIndex = propertyNode.SelectedImageIndex = (int)DataSpecTester.ImageListIcon.Class; if (instanceType.Equals(typeof(NetworkSwitchRequest))) { #region NetworkSwitchRequest RequestBase subRequest = ((NetworkSwitchRequest)instance).GetSubRequest(); if (subRequest != null) { propertyNode.Text = string.Format("{0} [{1}({2})]", pInfo.Name, pInfo.PropertyType, subRequest.GetType()); nodeBind.IsESPData = true; nodeBind.SubType = subRequest.GetType(); ReportToTreeView(subRequest, propertyNode.Nodes, writer, false); } #endregion } else { //nodeBind.NodeItem #region NetworkSwitchResponse object currentReqObj = ExchangeGet("Plug_Current_Request"); if (currentReqObj != null) { //Trace.WriteLine("NetworkSwitchResponse of " + currentReqObj.GetType()); RequestBase currentRequest = currentReqObj as RequestBase; if (currentReqObj.GetType().Equals(typeof(NetworkSwitchRequest))) { currentRequest = ((NetworkSwitchRequest)currentReqObj).GetSubRequest(); } if (currentRequest != null) { ESPDataBase subResponse = ((NetworkSwitchResponse)instance).GetSubResponse(currentRequest); nodeBind.IsESPData = true; nodeBind.SubType = subResponse.GetType(); propertyNode.Text = string.Format("{0} [{1}({2})]", pInfo.Name, pInfo.PropertyType, nodeBind.SubType); ReportToTreeView(subResponse, propertyNode.Nodes, writer, false); } } #endregion } } } #endregion } } else { nodeBind.StoreLength = SpecUtil.GetCLRTypeByteLength(pInfo.PropertyType); propertyNode.Tag = nodeBind; } } #endregion lastProperty = pInfo; } if (appendRoot && rootNode != null) { rootNode.Expand(); } }
private void btnOK_Click(object sender, EventArgs e) { if (BindInstance == null) { BindInstance = new TreeNodeInstanceBind(); } BindInstance.StoreIndex = Convert.ToInt64(tbxStartIndex.Text.Trim()); BindInstance.StoreLength = Convert.ToInt64(tbxStoreLength.Text.Trim()); BindInstance.TagModified = true; if (BindInstance.IsESPData) { ESPDataBase instance = Activator.CreateInstance(BindInstance.NodeType) as ESPDataBase; if (instance != null) { try { if (cbxReadFromRoot.Checked && RootStream != null) { RootStream.Result.Position = BindInstance.StoreIndex; SpecUtil.BindFromNetworkStream(instance, RootStream.Result, BindInstance.StoreIndex, false); BindInstance.StoreLength = instance.GetContentLength(); } else { if (tbxNodeItem.Text.Trim() != string.Empty) { SpecUtil.BindFromNetworkStream(instance, SpecUtil.HexPatternStringToByteArray(tbxNodeItem.Text.Trim()).AsMemoryStream(), 0, false); } } } catch (Exception exp) { Desktop.ShowEror(this, "Message:{0}\r\n{1}", exp.Message, exp.StackTrace); } BindInstance.NodeItem = instance; } } else { if (cbxReadFromRoot.Checked) { RootStream.Result.Position = BindInstance.StoreIndex; if (BindInstance.NodeType.Equals(typeof(byte[]))) { BindInstance.NodeItem = RootStream.ReadSpecialLength((int)BindInstance.StoreLength); } else { BindInstance.NodeItem = RootStream.ReadAsValue(BindInstance.NodeType); } } } DialogResult = DialogResult.OK; this.Close(); }