/// <summary>Object synchronize from the JSon string.</summary> /// <param name="Object">The object.</param> /// <param name="json">The json.</param> /// <param name="showError">if set to <c>true</c> [show error].</param> /// <returns></returns> public bool Object_Set(object Object, JObject json, bool showError = true) { string name = Object.ToString(); string objectName = Object_GetName(json); foreach (KeyValuePair <string, JToken> item in json) { string property_orFieldName = item.Key; //if (property_orFieldName.zIn(objectName, "Name")) continue; // <========================================= // Search for property if (_Object_SetProperty(Object, property_orFieldName, item.Value) == false) { // Property not found -> lets try to find a field if (Object_SetField(Object, property_orFieldName, item.Value) == false) { if (property_orFieldName.ToLower() == "name") { continue; // If the class do not have a name -> ignore this } if (showError) { var ex = new InvalidOperationException($"Error! Property / Field '{property_orFieldName}' does not exist in object: '{name}'."); ex.zLogLibraryMsg(); throw ex; } // <======================================================================================== } } } return(true); }
/// <summary> /// Word_FromAbbreviation the object value to Enumeral value. /// </summary> /// <param name="value">The object string.</param> /// <param name="type">The type</param> /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param> /// <param name="ignoreStrPart">The ignore string part.</param> /// <returns> /// object /// </returns> public object Str_2EnumValue(string value, Type type, bool ignoreCase = false, string ignoreStrPart = "") { // Check parameters if (ignoreStrPart != "") { value = value.Replace(ignoreStrPart, ""); } if (value != null && value.Contains(".")) { var valueOld = value; var typeName = valueOld.zvar_Id("."); value = valueOld.zvar_Value("."); if (typeName != type.Name) { throw new ArgumentException($"Error! '{value}' is not part of '{type.Name}'.", nameof(value)); } } // Try the conversion try { var result = Enum.Parse(type, value, ignoreCase); return(result); } catch (Exception e) { var typeName = type.ToString(); var errMsg = $"ERROR: Value '{value}' was not found in type {typeName}"; // Provide better context to the exception var ex = new InvalidOperationException(errMsg, e); ex.zLogLibraryMsg(); throw ex; } }
/// <summary> /// Converts the stringtype name to type. /// </summary> /// <param name="strType2">The string type</param> /// <returns>Type</returns> public Type Type_FromStr(string strType) { if (strType == "string") strType = "System.String"; else if (strType.zIn("string", "Enum", "Exception", "Array")) strType = "System." + strType; else if (strType == "Assembly") return typeof(Assembly); var result = Type.GetType(strType); if (result == null) { var ex = new InvalidOperationException($"Error! Unable to find type '{strType}'."); ex.zLogLibraryMsg(); throw ex; } return result; }
/// <summary>Converts the class object to json string.</summary> /// <typeparam name="T"></typeparam> /// <param name="classObject">The class object. It must be of the original type</param> /// <param name="DoReferences">if set to <c>true</c> [do referenced] objects also.</param> /// <param name="filterFields">The filter fields.</param> /// <returns>string</returns> public string Convert_FromObject <T>(T classObject, bool DoReferences = false, params string[] filterFields) { if (classObject == null) { return(string.Empty); } // Settings var settings = new JsonSerializerSettings(); if (DoReferences) { settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; } else { settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; } // Filter for fields =================== IO_Json_ContractResolver <T> contract = null; if (filterFields.Length != 0) { contract = new IO_Json_ContractResolver <T>(filterFields); settings.ContractResolver = contract; } string json = JsonConvert.SerializeObject(classObject, Formatting.Indented, settings); // Error handling if (contract != null && contract.AllFieldsWereFound() == false) { var ex = new InvalidOperationException($"Error! The following fields were not found: {contract.MissedFields_AsStr()}"); ex.zLogLibraryMsg(); throw ex; } return(json); }
public string SQL_Q(object Object, bool addN = false) { if (this.Object.IsNull(Object)) { return("NULL"); } var result = As.Str_FromObj(Object); // default inputStr if (Object is string) { var N = (addN) ? "N'" : "'"; result = result.Replace("'", "''"); result = N + result.Replace("".NL(), SQL_NL()) + "'"; return(result); } if (Object is DateTime || Object is Guid) { return("'" + result + "'"); } if (Object is bool) { return(As.Bool_FromObj(Object) ? "1" : "0"); } if (this.Object.IsNumber(Object)) { return(result); } // =============== // Error condition // =============== var ex = new InvalidOperationException("Error! Cannot convert type '" + Object.GetType() + "' to SQL!"); ex.zLogLibraryMsg(); throw ex; }
/// <summary> /// Move to group box method. /// </summary> /// <param name="sender">The sender</param> /// <param name="moveToContainer">The move to container.</param> /// <param name="errorIfOtherControls">if set to <c>true</c> [error if other controls].</param> private void MoveToContainer(Control sender, Control moveToContainer, bool errorIfOtherControls = false) { // This method is private to so that the moveToContainer types can be filtered with overload methods - do not make it public (rather add an overload method) if (moveToContainer == null) { return; } if (moveToContainer.Parent == null) { return; } if ((moveToContainer is Panel || moveToContainer is GroupBox) == false) { "Error: The contrainer to move the control to, must be a Panel or a Group!".zOk(); return; } if (_lamedWin.libUI.WinForms.Controls.Control.IsHostedInDesigner(sender) == false) { return; } if (moveToContainer.Controls.Count != 0 && errorIfOtherControls) { var ex = new InvalidOperationException("Error! There are other controls on groupBox '" + moveToContainer.ToString() + "'!"); ex.zLogLibraryMsg(); throw ex; } moveToContainer.Controls.Add(sender); sender.BringToFront(); if (sender.Dock == DockStyle.None) { _lamedWin.libUI.WinForms.Controls.Control.Dock(sender, DockStyle.Top); } }