/// <summary> /// 添加明细表信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAdd_Click(object sender, RoutedEventArgs e) { Button btn = sender as Button; FixtureFurnaceMainEntity main = (FixtureFurnaceMainEntity)btn.Tag;// as FixtureFurnaceMain; FixtureFurnaceDetaiViewModel vmModel = new FixtureFurnaceDetaiViewModel(); ObjectReflection.AutoMapping(main, vmModel); bool?nullable = new ChuckingDetailDialog(vmModel, 1).ShowDialog(); bool flag = true; if ((nullable.GetValueOrDefault() == flag ? (nullable.HasValue ? 1 : 0) : 0) == 0) { return; } this.RefreshData(); }
private void btnOK_Click(object sender, RoutedEventArgs e) { try { DetailModel = new FixtureFurnaceDetailEntity(); ObjectReflection.AutoMapping(Model, DetailModel); //明细表 DetailModel.FFDState = (int)Enum.Parse(typeof(FFDState), cmbFFDState.SelectedValue.ToString()); DetailModel.FFDIsAccomplish = (int)Enum.Parse(typeof(YesOrNoType), Accomplish.SelectedValue.ToString()); DetailModel.CAId = cmbCAI.SelectedValue == null ? 0 : (int)cmbCAI.SelectedValue; if (operateType == 1) { if ((new FixtureFurnaceDetailDB().Insert(DetailModel) < 1)) { CustomMessageBox.Show("添加数据过程出现错误!", CustomMessageBoxButton.OKCancel, CustomMessageBoxIcon.Question); return; } } else { if (!(new FixtureFurnaceDetailDB()).Update(DetailModel)) { CustomMessageBox.Show("修改数据过程出现错误!", CustomMessageBoxButton.OKCancel, CustomMessageBoxIcon.Question); return; } } this.DialogResult = new bool?(true); this.Close(); } catch (Exception ex) { MessageBoxX.Error(ex.Message); } }
public void RegisterAttributes() { Type type = GetType(); Type serializableType = typeof(Serializable); // Only do this for user types. if (type.Assembly == serializableType.Assembly || !type.IsSubclassOf(serializableType)) { return; } ObjectReflection reflection = Context.GetReflection(GetTypeHash()); // Register field attributes of this class foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (field.DeclaringType?.Assembly == serializableType.Assembly) { continue; } if (field.IsNotSerialized) { continue; } var attribute = Attribute.GetCustomAttribute(field, typeof(SerializeFieldAttribute)) as SerializeFieldAttribute; if (field.IsPrivate && attribute == null) { continue; } Type fieldType = field.FieldType; if (fieldType.IsEnum) { fieldType = Enum.GetUnderlyingType(fieldType); } VariantType variantType = Variant.GetVariantType(fieldType); // TODO: Support VariantType.VarPtr if (variantType == VariantType.VarNone || variantType == VariantType.VarPtr || variantType == VariantType.VarVoidPtr) { // Incompatible type. Log.Warning($"Trying to register attribute {field.DeclaringType?.FullName}.{field.Name} which has incompatible type {field.FieldType.Name}."); continue; } StringList enumNames = _emptyStringList; if (field.FieldType.IsEnum) { enumNames = new StringList(); foreach (string name in field.FieldType.GetEnumNames()) { enumNames.Add(name); } } var accessor = new VariantFieldAccessor(field, variantType); var defaultValue = new Variant(); try { accessor.Get(this, defaultValue); } catch (ArgumentNullException) { defaultValue = new Variant(variantType); } string attributeName = attribute?.Name ?? field.Name; var info = new AttributeInfo(accessor.VariantType, attributeName, accessor, enumNames, defaultValue, attribute?.Mode ?? AttributeMode.AmDefault); reflection.AddAttribute(info); } // Register property attributes of this class foreach (PropertyInfo property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (property.DeclaringType?.Assembly == serializableType.Assembly) { continue; } // Properties must have both getter and setter for them to be serializable. if (property.GetMethod == null || property.SetMethod == null) { continue; } // Private (even if partially) properties are not serialized by default. var attribute = Attribute.GetCustomAttribute(property, typeof(SerializeFieldAttribute)) as SerializeFieldAttribute; if ((property.GetMethod.IsPrivate || property.SetMethod.IsPrivate) && attribute == null) { continue; } if (!property.CanRead || !property.CanWrite) { Log.Warning($"Trying to register attribute {property.DeclaringType?.FullName}.{property.Name} which is not readable and writable."); continue; } Type propertyType = property.PropertyType; if (propertyType.IsEnum) { propertyType = Enum.GetUnderlyingType(propertyType); } VariantType variantType = Variant.GetVariantType(propertyType); // TODO: Support VariantType.VarPtr if (variantType == VariantType.VarNone || variantType == VariantType.VarPtr || variantType == VariantType.VarVoidPtr) { // Incompatible type. Log.Warning($"Trying to register attribute {property.DeclaringType?.FullName}.{property.Name} which has incompatible type {property.PropertyType.Name}."); continue; } StringList enumNames = _emptyStringList; if (property.PropertyType.IsEnum) { enumNames = new StringList(); foreach (string name in property.PropertyType.GetEnumNames()) { enumNames.Add(name); } } var accessor = new VariantPropertyAccessor(property, variantType); var defaultValue = new Variant(); try { accessor.Get(this, defaultValue); } catch (ArgumentNullException) { defaultValue = new Variant(variantType); } string attributeName = attribute?.Name ?? property.Name; var info = new AttributeInfo(accessor.VariantType, attributeName, accessor, enumNames, defaultValue, attribute?.Mode ?? AttributeMode.AmDefault); reflection.AddAttribute(info); } }
private Dictionary <string, string> GetDataFromMembers(Object ReflectedObject) { Dictionary <string, string> ObjectValues = new Dictionary <string, string>(); var DReflectedAttribute = ReflectedObject.GetType().GetCustomAttributes(typeof(DesignAttribute), false).FirstOrDefault() as DesignAttribute; if (DReflectedAttribute == null) { return(ObjectValues); } else if (!DReflectedAttribute.VisibleAtPresentation) { return(ObjectValues); } string Value = ""; string Name = ""; ObjectReflection oOR = new ObjectReflection(); foreach (MemberInfo oM in oOR.GetMemberInfo(ReflectedObject)) { DesignAttribute oDa = (DesignAttribute)Attribute.GetCustomAttribute(oM, typeof(DesignAttribute)); if (oDa != null && oDa.VisibleAtPresentation) { Value = ""; Name = ""; switch (oM.MemberType) { case MemberTypes.Constructor: break; case MemberTypes.Event: break; case MemberTypes.Field: FieldInfo oFieldInfo = ReflectedObject.GetType().GetFields(oOR.BindingFlags).Where(f => f.Name.Equals(oM.Name)).FirstOrDefault(); if (oDa.Expand && oFieldInfo.GetValue(ReflectedObject) != null) { //get the underlying object to show; foreach (KeyValuePair <string, string> oRec in GetDataFromMembers(oFieldInfo.GetValue(ReflectedObject))) { ObjectValues.Add( oRec.Key, oRec.Value ); // do something with entry.Value or entry.Key } } else { if (oFieldInfo.GetValue(ReflectedObject) != null) { Value = oFieldInfo.GetValue(ReflectedObject).ToString(); Name = oFieldInfo.Name; } } break; case MemberTypes.Method: break; case MemberTypes.Property: PropertyInfo oPropertyInfo = ReflectedObject.GetType().GetProperties(oOR.BindingFlags).Where(f => f.Name.Equals(oM.Name)).FirstOrDefault(); if (oPropertyInfo.GetValue(ReflectedObject) != null) { Value = oPropertyInfo.GetValue(ReflectedObject).ToString(); Name = oPropertyInfo.Name; } break; case MemberTypes.TypeInfo: break; case MemberTypes.Custom: break; case MemberTypes.NestedType: break; case MemberTypes.All: break; default: break; } if (Name != "") { ObjectValues.Add(Name, Value); } } } return(ObjectValues); }
private Dictionary <string, string> GetDataFromMembers(Object ReflectedObject, string[] ObjectNamesToInclude) { Dictionary <string, string> ObjectValues = new Dictionary <string, string>(); string Value = ""; string Name = ""; ObjectReflection oOR = new ObjectReflection(); foreach (MemberInfo oM in oOR.GetMemberInfo(ReflectedObject)) { if (Array.FindIndex( ObjectNamesToInclude, element => element.Equals(oM.Name) ) >= 0) { Value = ""; Name = ""; switch (oM.MemberType) { case MemberTypes.Constructor: break; case MemberTypes.Event: break; case MemberTypes.Field: FieldInfo oFieldInfo = ReflectedObject.GetType().GetFields(oOR.BindingFlags).Where(f => f.Name.Equals(oM.Name)).FirstOrDefault(); if (oFieldInfo.GetValue(ReflectedObject) != null) { Value = oFieldInfo.GetValue(ReflectedObject).ToString(); Name = oFieldInfo.Name; } break; case MemberTypes.Method: break; case MemberTypes.Property: PropertyInfo oPropertyInfo = ReflectedObject.GetType().GetProperties(oOR.BindingFlags).Where(f => f.Name.Equals(oM.Name)).FirstOrDefault(); if (oPropertyInfo.GetValue(ReflectedObject) != null) { Value = oPropertyInfo.GetValue(ReflectedObject).ToString(); Name = oPropertyInfo.Name; } break; case MemberTypes.TypeInfo: break; case MemberTypes.Custom: break; case MemberTypes.NestedType: break; case MemberTypes.All: break; default: break; } if (Name != "") { ObjectValues.Add(Name, Value); } } } return(ObjectValues); }
public bool Start(bool isStart, ref string errorMsg) { try { InitConfig(DbType);//初始化配置 Obj = ObjectReflection.CreateObject(_commProgramName.Substring(0, _commProgramName.IndexOf(".dll"))); IResolve = Obj as IDataResolve; if (!Dic.ContainsKey(StrDeviceId)) { Dic.TryAdd(StrDeviceId, IResolve); } else { Dic[StrDeviceId] = IResolve; } string param1 = string.Empty; IResolve.GetRules(StrDeviceId, DbType, ref param1); _server = new TcpPullServer <ClientInfo>(); // 设置服务器事件 _server.OnPrepareListen -= new TcpServerEvent.OnPrepareListenEventHandler(OnPrepareListen); _server.OnAccept -= new TcpServerEvent.OnAcceptEventHandler(OnAccept); _server.OnSend -= new TcpServerEvent.OnSendEventHandler(OnSend); _server.OnReceive -= new TcpPullServerEvent.OnReceiveEventHandler(OnReceive); _server.OnClose -= new TcpServerEvent.OnCloseEventHandler(OnClose); _server.OnShutdown -= new TcpServerEvent.OnShutdownEventHandler(OnShutdown); _server.OnPrepareListen += new TcpServerEvent.OnPrepareListenEventHandler(OnPrepareListen); _server.OnAccept += new TcpServerEvent.OnAcceptEventHandler(OnAccept); _server.OnSend += new TcpServerEvent.OnSendEventHandler(OnSend); _server.OnReceive += new TcpPullServerEvent.OnReceiveEventHandler(OnReceive); _server.OnClose += new TcpServerEvent.OnCloseEventHandler(OnClose); _server.OnShutdown += new TcpServerEvent.OnShutdownEventHandler(OnShutdown); _server.IpAddress = _hostIp; _server.Port = ushort.Parse(_port); if (isStart) { // 启动服务 if (_server.Start()) { InitServiceInfo(true); //Log4Helper.WriteInfoLog(this.GetType(), // $@"设备id:{StrDeviceId} 设备名称:{_name} $Server Start OK -> ({_hostIp}:{_port})"); if (!DicTcpServer.ContainsKey(StrDeviceId)) { DicTcpServer.TryAdd(StrDeviceId, _server); } else { DicTcpServer[StrDeviceId] = _server; } return(true); } else { InitServiceInfo(false); string msg = $@"加载【{_name}】失败!×原因:{ string.Format("$Server Start Error -> {0}({1}) Port:{2}", _server.ErrorMessage, _server.ErrorCode, _port) }"; //EmailHelper.SendQQEmail(InitDeviceScheduler.发件人邮箱, // InitDeviceScheduler.收件人邮箱列表.Split(','), // InitDeviceScheduler.ServiceName, InitDeviceScheduler.ServiceName + $@"加载【{_name}】失败!", // $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:" + InitDeviceScheduler.ServiceName + // msg, // InitDeviceScheduler.发件人邮箱授权码); errorMsg = msg; return(false); } } else { InitServiceInfo(false); return(false); } } catch (Exception e) { //Log4Helper.WriteErrorLog(this.GetType(), $@"设备id:{StrDeviceId} 设备名称:{_name} " + e.ToString()); errorMsg = e.ToString(); return(false); } finally { //InitMonitorService.AutoResetEvent.Set(); } }