/// <summary> /// Turns a string into a typed value. Useful for auto-conversion routines /// like form variable or XML parsers. /// <seealso>Class wwUtils</seealso> /// </summary> /// <param name="SourceString"> /// The string to convert from /// </param> /// <param name="TargetType"> /// The type to convert to /// </param> /// <param name="Culture"> /// Culture used for numeric and datetime values. /// </param> /// <returns>object. Throws exception if it cannot be converted.</returns> public static object StringToTypedValue(string SourceString, Type TargetType, CultureInfo Culture) { object Result = null; if (TargetType == typeof(string)) { Result = SourceString; } else if (TargetType == typeof(int)) { Result = int.Parse(SourceString, NumberStyles.Integer, Culture.NumberFormat); } else if (TargetType == typeof(byte)) { Result = Convert.ToByte(SourceString); } else if (TargetType == typeof(decimal)) { Result = Decimal.Parse(SourceString, NumberStyles.Any, Culture.NumberFormat); } else if (TargetType == typeof(double)) { Result = Double.Parse(SourceString, NumberStyles.Any, Culture.NumberFormat); } else if (TargetType == typeof(bool)) { if (SourceString.ToLower() == "true" || SourceString.ToLower() == "on" || SourceString == "1") { Result = true; } else { Result = false; } } else if (TargetType == typeof(DateTime)) { Result = Convert.ToDateTime(SourceString, Culture.DateTimeFormat); } else if (TargetType.IsEnum) { Result = Enum.Parse(TargetType, SourceString, true); } else { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(TargetType); if (converter != null && converter.CanConvertFrom(typeof(string))) { Result = converter.ConvertFromString(null, Culture, SourceString); } else { System.Diagnostics.Debug.Assert(false, "Type Conversion not handled in StringToTypedValue for " + TargetType.Name + " " + SourceString); throw (new Exception("Type Conversion not handled in StringToTypedValue")); } } return(Result); }
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { string str = value as string; if (str == null) { return(base.ConvertFrom(context, culture, value)); } str = str.Trim(); if (str.Length == 0) { return(null); } if (culture == null) { culture = System.Globalization.CultureInfo.CurrentCulture; } char ch = culture.TextInfo.ListSeparator[0]; string[] strArray = str.Split(new char[] { ch }); int[] numArray = new int[strArray.Length]; System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(int)); for (int i = 0; i < numArray.Length; i++) { numArray[i] = (int)converter.ConvertFromString(context, culture, strArray[i]); } if (numArray.Length != 4) { throw new System.ArgumentException(string.Format("{0}长度必须为4位,当前为{1},参考格式:{2}。", new object[] { "value", str, "left, top, right, bottom" })); } return(new Padding(numArray[0], numArray[1], numArray[2], numArray[3])); }
static private TData parseData <TData>(System.Reflection.FieldInfo[] fieldInfoList, ConfigTable table, ConfigRow configRow) where TData : new() { TData data = new TData(); foreach (System.Reflection.FieldInfo fieldInfo in fieldInfoList) { string configValue = getConfigValue(fieldInfo, table, configRow); if (!string.IsNullOrEmpty(configValue)) { try { System.ComponentModel.TypeConverter typeConverter = getTypeConverter(fieldInfo); object fieldValue = typeConverter.ConvertFromString(configValue); fieldInfo.SetValue(data, fieldValue); } catch (Exception) { act.debug.PrintSystem.LogError("[CONFIG] error in field : " + fieldInfo.Name); throw; } } } return(data); }
/// <summary> /// Interprets the input string as a hex number in the form 0x..... and converts that to an int, and then to color /// </summary> public static TCODColor HexToColor(string hexColor) { System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Int32)); Int32 color = (Int32)typeConverter.ConvertFromString(hexColor); return(IntToColor(color)); }
public static void SetFont(string fontString1) { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); Font myFont1 = (Font)converter.ConvertFromString(fontString1); KatanaForm.form3.programOutputText.Font = myFont1; }
public static Color GetColor(string strColorString) { // Create the ColorConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Color)); return((Color)converter.ConvertFromString(strColorString)); }
public static bool _ConvertFromString_System_ComponentModel_TypeConverter_System_ComponentModel_ITypeDescriptorContext_System_String( ) { //class object System.ComponentModel.TypeConverter _System_ComponentModel_TypeConverter = new System.ComponentModel.TypeConverter(); //Parameters System.ComponentModel.ITypeDescriptorContext context = null; System.String text = null; //ReturnType/Value System.Object returnVal_Real = null; System.Object returnVal_Intercepted = null; //Exception System.Exception exception_Real = null; System.Exception exception_Intercepted = null; InterceptionMaintenance.disableInterception( ); try { returnVal_Real = _System_ComponentModel_TypeConverter.ConvertFromString(context, text); } catch (System.Exception e) { exception_Real = e; } InterceptionMaintenance.enableInterception( ); try { returnVal_Intercepted = _System_ComponentModel_TypeConverter.ConvertFromString(context, text); } catch (System.Exception e) { exception_Intercepted = e; } return((exception_Real.Messsage == exception_Intercepted.Message) && (returnValue_Real == returnValue_Intercepted)); }
private static Dictionary <string, Color[]> LoadNoteColours(string defs) { System.ComponentModel.TypeConverter colorConverter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Color)); // (Color)colorConverter.ConvertFromString("#00000000") Dictionary <string, Color[]> noteColours_Default = new Dictionary <string, Color[]> { { NoteTypes.NoteTypeDefs.None.TypeName, new Color[] { Color.Transparent, Color.Transparent } }, { NoteTypes.NoteTypeDefs.Tap.TypeName, new Color[] { Color.Blue, Color.Transparent } }, { NoteTypes.NoteTypeDefs.Hold.TypeName, new Color[] { Color.LimeGreen, Color.Transparent } }, { NoteTypes.NoteTypeDefs.SimulTap.TypeName, new Color[] { Color.DeepPink, Color.Transparent } }, { NoteTypes.NoteTypeDefs.SimulHoldStart.TypeName, new Color[] { Color.DeepPink, Color.Transparent } }, { NoteTypes.NoteTypeDefs.SimulHoldRelease.TypeName, new Color[] { Color.DeepPink, Color.Transparent } }, { NoteTypes.NoteTypeDefs.FlickLeft.TypeName, new Color[] { Color.FromArgb(0xc0, 0xc0, 0xc0), Color.FromArgb(0x70, 0, 0x78) } }, { NoteTypes.NoteTypeDefs.HoldEndFlickLeft.TypeName, new Color[] { Color.LightGray, Color.FromArgb(0x70, 0, 0x78) } }, { NoteTypes.NoteTypeDefs.SwipeLeftStartEnd.TypeName, new Color[] { Color.FromArgb(0xc0, 0xc0, 0xc0), Color.DarkViolet } }, { NoteTypes.NoteTypeDefs.SwipeLeftMid.TypeName, new Color[] { Color.FromArgb(0xc0, 0xc0, 0xc0), Color.Violet } }, { NoteTypes.NoteTypeDefs.SwipeChangeDirR2L.TypeName, new Color[] { Color.FromArgb(0xc0, 0xc0, 0xc0), Color.Violet } }, { NoteTypes.NoteTypeDefs.FlickRight.TypeName, new Color[] { Color.FromArgb(0xc0, 0xc0, 0xc0), Color.FromArgb(0xcc, 0x88, 0) } }, { NoteTypes.NoteTypeDefs.HoldEndFlickRight.TypeName, new Color[] { Color.LightGray, Color.FromArgb(0xcc, 0x88, 0) } }, { NoteTypes.NoteTypeDefs.SwipeRightStartEnd.TypeName, new Color[] { Color.FromArgb(0xc0, 0xc0, 0xc0), Color.DarkOrange } }, { NoteTypes.NoteTypeDefs.SwipeRightMid.TypeName, new Color[] { Color.FromArgb(0xc0, 0xc0, 0xc0), Color.Gold } }, { NoteTypes.NoteTypeDefs.SwipeChangeDirL2R.TypeName, new Color[] { Color.FromArgb(0xc0, 0xc0, 0xc0), Color.Gold } }, { NoteTypes.NoteTypeDefs.ExtendHoldMid.TypeName, new Color[] { Color.LightGray, Color.Transparent } }, { NoteTypes.NoteTypeDefs.GbsFlick.TypeName, new Color[] { Color.Gold, Color.LightYellow } }, { NoteTypes.NoteTypeDefs.GbsHoldEndFlick.TypeName, new Color[] { Color.LightGray, Color.Gold } }, { NoteTypes.NoteTypeDefs.GbsSimulFlick.TypeName, new Color[] { Color.Goldenrod, Color.LightYellow } }, { NoteTypes.NoteTypeDefs.GbsClock.TypeName, new Color[] { Color.Blue, Color.Gold } }, { NoteTypes.NoteTypeDefs.GbsSimulClock.TypeName, new Color[] { Color.DeepPink, Color.Gold } } }; Dictionary <string, Color[]> noteColours = noteColours_Default; string[] defslines = defs.Split("\n".ToCharArray()); for (int i = 0; i < defslines.Length; i++) { if (defslines[i].StartsWith("#") || defslines[i].Trim().Length == 0) { continue; } string cleanDef = defslines[i].Replace(" ", ""); if (!cleanDef.Contains(":")) { continue; } string type = cleanDef.Split(":".ToCharArray())[0]; string[] vals = cleanDef.Split(":".ToCharArray())[1].Split(",".ToCharArray()); noteColours[type] = new Color[] { (Color)colorConverter.ConvertFromString(vals[0]), (Color)colorConverter.ConvertFromString(vals[1]) }; } return(noteColours); }
public static bool _ConvertFromString_System_ComponentModel_TypeConverter_System_ComponentModel_ITypeDescriptorContext_System_String( ) { //class object System.ComponentModel.TypeConverter _System_ComponentModel_TypeConverter = new System.ComponentModel.TypeConverter(); //Parameters System.ComponentModel.ITypeDescriptorContext context = null; System.String text = null; //ReturnType/Value System.Object returnVal_Real = null; System.Object returnVal_Intercepted = null; //Exception System.Exception exception_Real = null; System.Exception exception_Intercepted = null; InterceptionMaintenance.disableInterception( ); try { returnVal_Real = _System_ComponentModel_TypeConverter.ConvertFromString(context,text); } catch( System.Exception e ) { exception_Real = e; } InterceptionMaintenance.enableInterception( ); try { returnVal_Intercepted = _System_ComponentModel_TypeConverter.ConvertFromString(context,text); } catch( System.Exception e ) { exception_Intercepted = e; } return( ( exception_Real.Messsage == exception_Intercepted.Message ) && ( returnValue_Real == returnValue_Intercepted ) ); }
public static bool vTryParse <T>(this System.Enum theEnum, string valueToParse, out T returnValue) { returnValue = default(T); if (System.Enum.IsDefined(typeof(T), valueToParse)) { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)); returnValue = (T)converter.ConvertFromString(valueToParse); return(true); } return(false); }
public static Component ComponentFromXmlNode(XmlNode node) { string componentName = node.Name; // make a component of the right type Type componentType = Type.GetType("AstrologyGame.Components." + componentName, false); // handle the case in which no component with that name exists if (componentType == null) { // throw an exception Exception exception = new Exception($"No component of name '{componentName}' exists. " + $"Check {GameManager.ENTITIES_PATH} and make sure there are no errors."); throw (exception); } Component component = Activator.CreateInstance(componentType) as Component; // set the properties of the component foreach (XmlAttribute attribute in node.Attributes) { string propertyName = attribute.Name; string propertyValueAsString = attribute.Value; PropertyInfo propertyInfo = componentType.GetProperty(propertyName); // handle the case where the component does not have the specified property if (propertyInfo == null) { // throw an exception Exception exception = new Exception($"The component '{componentName}' does not have a property named '{propertyName}'. " + $"Check {GameManager.ENTITIES_PATH} and make sure there are no errors."); throw (exception); } Type propertyType = propertyInfo.PropertyType; object propertyValue; if (propertyType == typeof(Color)) { propertyValue = GameManager.ColorFromString(propertyValueAsString); } else { System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(propertyType); propertyValue = typeConverter.ConvertFromString(propertyValueAsString); } propertyInfo.SetValue(component, propertyValue); } return(component); }
private System.Drawing.Font ConvertToFont(string fontString) { try { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(System.Drawing.Font)); return((System.Drawing.Font)converter.ConvertFromString(fontString)); } catch { System.Diagnostics.Debug.WriteLine("Unable to convert"); } return(null); }
public static void DrawText(string color, string fontString, float x, float y, string textToDraw) { Color myTextColor = System.Drawing.ColorTranslator.FromHtml(color); Graphics g = KatanaForm.form2.CreateGraphics(); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; SolidBrush myTextBrush = new SolidBrush(myTextColor); System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); Font myFont = (Font)converter.ConvertFromString(fontString); g.DrawString(textToDraw, myFont, myTextBrush, x, y); }
private static Dictionary <string, Color> LoadUIColours(string defs) { System.ComponentModel.TypeConverter colorConverter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Color)); // (Color)colorConverter.ConvertFromString("#00000000") Dictionary <string, Color> uiColours_Default = new Dictionary <string, Color> { { UIColours.UIColourDefs.Chart_BG.TypeName, SystemColors.ControlLight }, { UIColours.UIColourDefs.Chart_BG_Lane1.TypeName, Color.Transparent }, { UIColours.UIColourDefs.Chart_BG_Lane2.TypeName, Color.Transparent }, { UIColours.UIColourDefs.Chart_BG_Lane3.TypeName, Color.Transparent }, { UIColours.UIColourDefs.Chart_BG_Lane4.TypeName, Color.Transparent }, { UIColours.UIColourDefs.Chart_BG_Lane5.TypeName, Color.Transparent }, { UIColours.UIColourDefs.Chart_BG_Lane6.TypeName, Color.Transparent }, { UIColours.UIColourDefs.Chart_BG_Lane7.TypeName, Color.Transparent }, { UIColours.UIColourDefs.Chart_BG_Lane8.TypeName, Color.Transparent }, { UIColours.UIColourDefs.Chart_LaneLine.TypeName, Color.LightGray }, { UIColours.UIColourDefs.Chart_BarLine.TypeName, Color.SlateGray }, { UIColours.UIColourDefs.Chart_BarText.TypeName, Color.DarkSlateGray }, { UIColours.UIColourDefs.Chart_QuarterLine.TypeName, Color.LightSlateGray }, { UIColours.UIColourDefs.Chart_EigthLine.TypeName, Color.LightGray }, { UIColours.UIColourDefs.Chart_Playhead.TypeName, Color.DarkSlateGray }, { UIColours.UIColourDefs.Form_BG.TypeName, Form1.DefaultBackColor }, { UIColours.UIColourDefs.Form_Text.TypeName, Form1.DefaultForeColor } }; Dictionary <string, Color> uiColours = uiColours_Default; string[] defslines = defs.Split("\n".ToCharArray()); for (int i = 0; i < defslines.Length; i++) { if (defslines[i].StartsWith("#") || defslines[i].Trim().Length == 0) { continue; } string cleanDef = defslines[i].Replace(" ", ""); if (!cleanDef.Contains(":")) { continue; } string type = cleanDef.Split(":".ToCharArray())[0]; string val = cleanDef.Split(":".ToCharArray())[1]; uiColours[type] = (Color)colorConverter.ConvertFromString(val); } return(uiColours); }
protected override bool InternalExecute(ProcessExecutingContext context) { ProcessSchemaManager processSchemaManager = UserConnection.ProcessSchemaManager; Guid uid = ProcessSchemaUId; if (processSchemaManager.FindItemByUId(ProcessSchemaUId) == null) { uid = ((Select) new Select(UserConnection).Top(1) .Column("UId") .From("SysSchema") .Where("Id").IsEqual(Column.Parameter(ProcessSchemaUId))) .ExecuteScalar <Guid>(); } ProcessSchema processSchema = GetProcessSchema(uid); if (!processSchema.Enabled) { return(true); } Dictionary <string, string> nameValueMap = new Dictionary <string, string>(); if (!string.IsNullOrWhiteSpace(CustomPropertyValues)) { nameValueMap = Common.Json.Json.Deserialize <Dictionary <string, string> >(CustomPropertyValues); } nameValueMap["OpportunityId"] = OpportunityId.ToString(); if (CanUseFlowEngine(processSchema)) { RunFlowEngineProcess(processSchema, nameValueMap); } else { Process process = processSchema.CreateProcess(UserConnection); foreach (KeyValuePair <string, string> keyValuePair in nameValueMap) { var parameter = processSchema.Parameters.FindByName(keyValuePair.Key); if (parameter != null) { var valueType = parameter.DataValueType.ValueType; System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(valueType); object value = typeConverter.ConvertFromString(keyValuePair.Value); process.SetPropertyValue(keyValuePair.Key, value); } } process.Execute(UserConnection); } return(true); }
/// <summary> /// 文字列を値に変換 /// </summary> /// <typeparam name="T">値の型</typeparam> /// <param name="str">文字列</param> /// <param name="val">値</param> /// <returns>変換に成功したらtrue、書式違いなどで変換できなかったらfalse</returns> public static bool TryParse <T>(string str, out T val) { try { System.Type type = typeof(T); if (type == typeof(string)) { val = (T)(object)str; } else if (type.IsEnum) { val = (T)System.Enum.Parse(typeof(T), str); } else if (type == typeof(Color)) { Color color = Color.white; bool ret = ColorUtil.TryParseColor(str, ref color); val = ret ? (T)(object)color : default(T); return(ret); } else if (type == typeof(int)) { val = (T)(object)int.Parse(str); } else if (type == typeof(float)) { val = (T)(object)float.Parse(str); } else if (type == typeof(double)) { val = (T)(object)double.Parse(str); } else if (type == typeof(bool)) { val = (T)(object)bool.Parse(str); } else { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(type); val = (T)converter.ConvertFromString(str); } return(true); } catch { val = default(T); return(false); } }
// 根据一个字体字符串获得一个 Font 对象 // 不能处理动态加载的条码字体 public static Font BuildFont(string strFontString) { if (String.IsNullOrEmpty(strFontString) == false) { // Create the FontConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); return((Font)converter.ConvertFromString(strFontString)); } else { return(Control.DefaultFont); } }
protected T getHTMLAttr <T>(string attrName, T defaultValue = default(T)) { List <HTMLAttribute> attrs = GetMoreHTMLAttributesValue(); HTMLAttribute attr = attrs.Find(a => string.Compare(a.AttributeName, attrName /*, true*/) == 0); if (attr != null) { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)); if (converter != null) { return((T)(converter.ConvertFromString(attr.AttributeValue))); } } return(defaultValue); }
private object GetAppSettingValue(InjectionTargetInfo target) { string key = target.Name.Substring(0, target.Name.LastIndexOf(AppSettingsPostFix)); string configurationValue = this.appSettingRetriever(key); // ConfigurationManager.AppSettings[key]; if (configurationValue != null) { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(target.TargetType); return(converter.ConvertFromString(null, CultureInfo.InvariantCulture, configurationValue)); } throw new ActivationException( "No application setting with key '" + key + "' could be found in the " + "application's configuration file."); }
public bool Load(string fileName) { XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(fileName); this.InputName = xmlDoc.SelectSingleNode("//inputName").InnerText; this.text = xmlDoc.SelectSingleNode("//text").InnerText; Type t = this.inputFormat.GetType(); XmlNode inputNode = xmlDoc.SelectSingleNode("/query/inputFormat"); foreach (XmlNode node in inputNode.ChildNodes) { PropertyInfo propertyInfo = t.GetProperty(node.Name, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (propertyInfo == null) { throw new System.ArgumentException(node.Name + " parameter does not exists for inputFormat " + this.inputName); } // read the actual (default) value object _defaultValue = propertyInfo.GetValue(inputFormat, null); // read actual value and update only if different System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(propertyInfo.PropertyType); object _value = typeConverter.ConvertFromString(node.InnerText); if (!_defaultValue.Equals(_value)) { propertyInfo.SetValue(inputFormat, _value, null); } } xmlDoc = null; } catch (XmlException xex) { System.Diagnostics.Debug.Write(xex); return(false); } // catch(System.Reflection.TargetInvocationException ex) // { // System.Diagnostics.Debug.Write(ex); // return false; // } return(true); /* * * XmlTextReader reader = new XmlTextReader(fileName); * reader.Read(); * reader.ReadStartElement("query"); * this.InputName = reader.ReadElementString("inputName"); * Type t = this.inputFormat.GetType(); * * this.text = reader.ReadElementString("text"); * * reader.ReadStartElement("inputFormat"); * * while (reader.Read()) * { * if (reader.NodeType != XmlNodeType.Element) * continue; * * //PropertyInfo property = t.GetProperty(reader.Name); * * PropertyInfo propertyInfo = t.GetProperty(reader.Name,System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public ); | if(propertyInfo == null) | { | throw new System.ArgumentException(reader.Name + " parameter does not exists for inputFormat " + this.inputName); | } | System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(propertyInfo.PropertyType); | object _value = typeConverter.ConvertFromString(reader.ReadInnerXml()); | propertyInfo.SetValue(inputFormat, _value, null); | //property.SetValue(this.inputFormat, reader.Value, null); | } | | reader.Close(); */ }
public static T TryParse <T>(string inValue) { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)); return((T)converter.ConvertFromString(null, System.Globalization.CultureInfo.InvariantCulture, inValue)); }
private void LoadFontSettings() { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); this.Font = (Font)converter.ConvertFromString(ConfigurationManager.AppSettings["fontSettings"]); }
static public T ConvertTo <T>(object data, System.Globalization.NumberStyles numFormat) { T rez = default(T); try { switch (typeof(T).ToString()) { case "System.Int16": { rez = (T)Convert.ChangeType(System.Int16.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.Int32": { rez = (T)Convert.ChangeType(System.Int32.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.Int64": { rez = (T)Convert.ChangeType(System.Int64.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.UInt16": { rez = (T)Convert.ChangeType(System.UInt16.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.UInt32": { rez = (T)Convert.ChangeType(System.UInt32.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.UInt64": { rez = (T)Convert.ChangeType(System.UInt64.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.Byte": { rez = (T)Convert.ChangeType(System.Byte.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.SByte": { rez = (T)Convert.ChangeType(System.SByte.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.Decimal": { rez = (T)Convert.ChangeType(System.Decimal.Parse(data.ToString(), numFormat), typeof(T)); break; } case "System.UIntPtr": { rez = (T)Convert.ChangeType(new System.UIntPtr(System.UInt32.Parse(data.ToString(), numFormat)), typeof(T)); break; } case "System.IntPtr": { rez = (T)Convert.ChangeType(new System.IntPtr(System.Int32.Parse(data.ToString(), numFormat)), typeof(T)); break; } case "System.Drawing.Font": { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(System.Drawing.Font)); rez = (T)converter.ConvertFromString(data.ToString()); break; } default: { rez = (T)Convert.ChangeType(data, typeof(T)); break; } } //rez = (T)data; } catch { } return(rez); }
/// <summary> /// 文字列から指定した型に変換を行います。 /// </summary> /// <param name="t">変換先の型を指定します。</param> /// <param name="value">変換元の文字列を指定します。</param> /// <returns>変換した結果のオブジェクトを返します。</returns> public static object To(System.Type t, string value) { switch (Types.GetTypeCode(t)) { case TypeCodes.SByte: return(ToSByte(value)); case TypeCodes.Byte: return(ToByte(value)); case TypeCodes.Short: return(ToInt16(value)); case TypeCodes.UShort: return(ToUInt16(value)); case TypeCodes.Int: return(ToInt32(value)); case TypeCodes.UInt: return(ToUInt32(value)); case TypeCodes.Long: return(ToInt64(value)); case TypeCodes.ULong: return(ToUInt64(value)); case TypeCodes.Decimal: return(ToDecimal(value)); case TypeCodes.Float: return(ToSingle(value)); case TypeCodes.Double: return(ToDouble(value)); case TypeCodes.Bool: return(ToBoolean(value)); case TypeCodes.Char: return(ToChar(value)); case TypeCodes.String: return(value); case TypeCodes.Guid: return(ToGuid(value)); case TypeCodes.TimeSpan: return(ToTimeSpan(value)); case TypeCodes.DateTime: return(ToDateTime(value)); case TypeCodes.IntPtr: return((System.IntPtr)ToInt64(value)); case TypeCodes.UIntPtr: return((System.UIntPtr)ToUInt64(value)); case TypeCodes.BoolArray: return(ToBooleanArray(value)); case TypeCodes.ByteArray: return(ToByteArray(value)); case TypeCodes.Type: if (t != typeof(System.Type)) { goto default; } object val = System.Type.GetType(value, false, false); return(val != null?val:System.Type.GetType(value, true, true)); default: // typeconv: if (t.IsEnum) { return(System.Enum.Parse(t, value)); //System.Enum } if (t.GetCustomAttributes(typeof(System.ComponentModel.TypeConverterAttribute), false).Length == 0) { goto serial; } System.ComponentModel.TypeConverter conv = System.ComponentModel.TypeDescriptor.GetConverter(t); if (conv.CanConvertFrom(typeof(string))) { try{ return(conv.ConvertFromString(value)); }catch {} } serial: if (t.GetCustomAttributes(typeof(System.SerializableAttribute), false).Length == 0) { goto op_implicit; } using (System.IO.MemoryStream memstr = new System.IO.MemoryStream(System.Convert.FromBase64String(value))){ return(binF.Deserialize(memstr)); } op_implicit: System.Reflection.MethodInfo[] ms = t.GetMethods(BF_PublicStatic); for (int i = 0; i < ms.Length; i++) { if (ms[i].ReturnType != t) { continue; } if (ms[i].Name != "op_Implicit" && ms[i].Name != "op_Explicit") { continue; } System.Reflection.ParameterInfo[] ps = ms[i].GetParameters(); if (ps.Length != 1 || ps[0].ParameterType != typeof(string)) { continue; } return(ms[i].Invoke(null, new object[] { value })); } throw new System.ArgumentException(string.Format(FROMSTR_NOTSUPPORTEDTYPE, t), "t"); } }
public void loadFont() { font = (Font)converter.ConvertFromString(fontstr); }
///******************************************************************************* ///(-) /// MODULE NAME : 付箋紙ロード /// MODULE ID : LoadStickyNotes /// /// PARAMETER IN : /// <param>(in)なし</param> /// PARAMETER OUT : /// <param>(out)なし</param> /// /// RETURN VALUE : /// <returns>なし</returns> /// /// FUNCTION : /// <summary> /// 呼び出し元の装置を立ち上げたときに、XMLに保存されている付箋紙をロードする /// </summary> /// ///******************************************************************************* public void LoadStickyNotes() { CStickyNotesCollection listObj = new CStickyNotesCollection(); XmlDocument doc; try { bool IsExist = File.Exists(m_SaveXMLPath); if (true == IsExist) { xmlSerializer = new XmlSerializer(typeof(CStickyNotesCollection)); doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.Load(m_SaveXMLPath); using (XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement)) { listObj = (CStickyNotesCollection)xmlSerializer.Deserialize(reader); } // trial 1 end Int32 cnt = listObj.CNoteProperties.Count(); for (int i = 0; i < cnt; i++) { StickyNote.Current_NoteProperties = new CNoteProperties(); // 付箋紙ID StickyNote.Current_NoteProperties.NoteID = i + 1; // テキストボックスの内容 StickyNote.Current_NoteProperties.Text = listObj.CNoteProperties[i].Text; // String型のフォント情報 StickyNote.Current_NoteProperties.FontString = listObj.CNoteProperties[i].FontString; // フォント:stringからFontに変換 System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); Font convertedFont = (Font)tc.ConvertFromString(StickyNote.Current_NoteProperties.FontString.FontString); // Font型のフォント情報 StickyNote.Current_NoteProperties.FontFont = convertedFont; // テキスト表示色 StickyNote.Current_NoteProperties.ForeColor = listObj.CNoteProperties[i].ForeColor; // 背景色 StickyNote.Current_NoteProperties.BackColor = listObj.CNoteProperties[i].BackColor; // サイズ StickyNote.Current_NoteProperties.Size = listObj.CNoteProperties[i].Size; // x,y座標 StickyNote.Current_NoteProperties.Location = listObj.CNoteProperties[i].Location; // 表示/非表示 StickyNote.Current_NoteProperties.Visible = listObj.CNoteProperties[i].Visible; // 明瞭 StickyNote.Current_NoteProperties.Opacity = listObj.CNoteProperties[i].Opacity; // 上記の情報を使って付箋紙を生成する StickyNoteForm = new FormStickyNote(2); if (null != StickyNoteForm) { this.AddtoNoteListMng(StickyNoteForm); UpdateStickyNoteList(); SaveStickyNotes(); StickyNote.Current_NoteProperties.Clear(); } } } listObj = null; } catch (Exception ex) { Debug.WriteLine(MethodBase.GetCurrentMethod() + ": " + ex.Message); } finally { doc = null; xmlSerializer = null; } }
public Font loadFont() { return((Font)converter.ConvertFromString(fontstr)); }
//private static readonly System.Windows.Forms.SelectionRangeConverter selectionRangeConv // =new System.Windows.Forms.SelectionRangeConverter(); /// <summary> /// 文字列から指定した型に変換を行います。 /// </summary> /// <param name="t">変換先の型を指定します。</param> /// <param name="value">変換元の文字列を指定します。</param> /// <returns>変換した結果のオブジェクトを返します。</returns> public static object FromString(System.Type t, string value) { switch (t.FullName.GetHashCode() & 0x7fffffff) { case 0x038D0F82: if (t != typeof(System.Int16)) { goto default; } return(System.Convert.ToInt16(value)); case 0x6D318EFD: if (t != typeof(System.UInt16)) { goto default; } return(System.Convert.ToUInt16(value)); case 0x1B47F8B8: if (t != typeof(System.Int32)) { goto default; } return(FromStringToInt32(value)); case 0x03FB8EF9: if (t != typeof(System.UInt32)) { goto default; } return(System.Convert.ToUInt32(value)); case 0x4A1B9AE7: if (t != typeof(System.Int64)) { goto default; } return(System.Convert.ToInt64(value)); case 0x61CC8EFB: if (t != typeof(System.UInt64)) { goto default; } return(System.Convert.ToUInt64(value)); case 0x4EE7D89D: if (t != typeof(System.SByte)) { goto default; } return(System.Convert.ToSByte(value)); case 0x2F001A17: if (t != typeof(System.Byte)) { goto default; } return(System.Convert.ToByte(value)); case 0x1B1EFB13: if (t != typeof(System.Decimal)) { goto default; } return(System.Convert.ToDecimal(value)); case 0x44059415: if (t != typeof(System.Char)) { goto default; } return(System.Convert.ToChar(value)); case 0x3EACB635: if (t != typeof(System.Single)) { goto default; } return(System.Convert.ToSingle(value)); case 0x0ED5FC72: if (t != typeof(System.Double)) { goto default; } return(System.Convert.ToDouble(value)); case 0x71309BFC: if (t != typeof(System.Boolean)) { goto default; } return(FromStringToBoolean(value)); case 0x5981F920: if (t != typeof(System.String)) { goto default; } return(value); case 0x1D77C984: if (t != typeof(System.IntPtr)) { goto default; } return((System.IntPtr)System.Convert.ToInt64(value)); case 0x65648F3B: if (t != typeof(System.UIntPtr)) { goto default; } return((System.UIntPtr)System.Convert.ToUInt64(value)); case 0x29BD8EB6: if (t != typeof(System.Guid)) { goto default; } return(System.Xml.XmlConvert.ToGuid(value)); case 0x1FE98930: if (t != typeof(System.TimeSpan)) { goto default; } return(FromStringToTimeSpan(value)); case 0x4A398CD8: if (t != typeof(System.DateTime)) { goto default; } return(FromStringToDateTime(value)); case 0x7F721A17: if (t != typeof(System.Type)) { goto default; } object val = System.Type.GetType(value, false, false); return(val != null?val:System.Type.GetType(value, true, true)); case 0x0458EA59: if (t != typeof(System.Boolean[])) { goto default; } return(FromStringToBooleanArray(value)); default: // typeconv: if (t.IsEnum) { return(System.Enum.Parse(t, value)); //System.Enum } if (t.GetCustomAttributes(typeof(System.ComponentModel.TypeConverterAttribute), false).Length == 0) { goto serial; } System.ComponentModel.TypeConverter conv = System.ComponentModel.TypeDescriptor.GetConverter(t); if (conv.CanConvertFrom(typeof(string))) { try{ return(conv.ConvertFromString(value)); }catch {} } serial: if (t.GetCustomAttributes(typeof(System.SerializableAttribute), false).Length == 0) { goto op_implicit; } using (System.IO.MemoryStream memstr = new System.IO.MemoryStream(System.Convert.FromBase64String(value))){ return(Convert.binF.Deserialize(memstr)); } op_implicit: System.Reflection.MethodInfo[] ms = t.GetMethods(BF_PublicStatic); for (int i = 0; i < ms.Length; i++) { if (ms[i].ReturnType != t) { continue; } if (ms[i].Name != "op_Implicit" && ms[i].Name != "op_Explicit") { continue; } System.Reflection.ParameterInfo[] ps = ms[i].GetParameters(); if (ps.Length != 1 || ps[0].ParameterType != typeof(string)) { continue; } return(ms[i].Invoke(null, new object[] { value })); } throw new System.ArgumentException(string.Format(FROMSTR_NOTSUPPORTEDTYPE, t), "t"); } #if NET1_1 switch (t.FullName.GetHashCode() & 0x7fffffff) { case 0x2DBDA61A: //case 0xE31638: if (t != typeof(System.Int16)) { goto default; } return(System.Convert.ToInt16(value)); case 0x1107D7EF: //case 0xE3166C: if (t != typeof(System.UInt16)) { goto default; } return(System.Convert.ToUInt16(value)); case 0x2DBDA65C: //case 0xE312F4: if (t != typeof(System.Int32)) { goto default; } return(FromStringToInt32(value)); case 0x1107D829: //case 0xE316A0: if (t != typeof(System.UInt32)) { goto default; } return(System.Convert.ToUInt32(value)); case 0x2DBDA63F: //case 0xE316D4: if (t != typeof(System.Int64)) { goto default; } return(System.Convert.ToInt64(value)); case 0x1107D84A: //case 0xE31708: if (t != typeof(System.UInt64)) { goto default; } return(System.Convert.ToUInt64(value)); case 0x2EF00F97: //case 0xE315D0: if (t != typeof(System.SByte)) { goto default; } return(System.Convert.ToSByte(value)); case 0x244D3E44: //case 0xE31604: if (t != typeof(System.Byte)) { goto default; } return(System.Convert.ToByte(value)); case 0x32C73145: //case 0xE317A4: if (t != typeof(System.Decimal)) { goto default; } return(System.Convert.ToDecimal(value)); case 0x244C7CD6: //case 0xE3159C: if (t != typeof(System.Char)) { goto default; } return(System.Convert.ToChar(value)); case 0x0EC74674: //case 0xE3173C: if (t != typeof(System.Single)) { goto default; } return(System.Convert.ToSingle(value)); case 0x5E38073B: //case 0xE31770: if (t != typeof(System.Double)) { goto default; } return(System.Convert.ToDouble(value)); case 0x604332EA: //case 0xE31568: if (t != typeof(System.Boolean)) { goto default; } return(FromStringToBoolean(value)); case 0x0DE37C3B: //case 0xE31328: if (t != typeof(System.String)) { goto default; } return(value); case 0x6572ED4B: //case 0xE37678: if (t != typeof(System.IntPtr)) { goto default; } return((System.IntPtr)System.Convert.ToInt64(value)); case 0x3203515E: //case 0xE376AC: if (t != typeof(System.UIntPtr)) { goto default; } return((System.UIntPtr)System.Convert.ToUInt64(value)); case 0x244AC511: //case 0xE376E0: if (t != typeof(System.Guid)) { goto default; } return(System.Xml.XmlConvert.ToGuid(value)); case 0x4BD7DD17: //case 0xE37714: if (t != typeof(System.TimeSpan)) { goto default; } return(FromStringToTimeSpan(value)); case 0x7F9DDECF: //case 0xE317D8://.NET Framework 2.0 : System.Xml.XmlDateTimeSerializationMode を指定 if (t != typeof(System.DateTime)) { goto default; } return(FromStringToDateTime(value)); case 0x24524716: //case 0xE37610: if (t != typeof(System.Type)) { goto default; } object val = System.Type.GetType(value, false, false); return(val != null?val:System.Type.GetType(value, true, true)); // case 0x2453BC7A://case 0xE37748: // if(t!=typeof(void))goto default; // break; case 0x7DDB9ECC: //case 0xE37778: if (t != typeof(System.Boolean[])) { goto default; } return(FromStringToBooleanArray(value)); // case 0x45EBBE0: // if(t!=typeof(System.Windows.Forms.SelectionRange))goto default; // return Convert.selectionRangeConv.ConvertFromString(value); // case 0x244D9E9D://case 0xE311BC: // if(t!=typeof(System.Enum))goto default; // return System.Xml.XmlConvert.ToInt64(value); default: // typeconv: if (t.IsEnum) { return(System.Enum.Parse(t, value)); //System.Enum } if (t.GetCustomAttributes(typeof(System.ComponentModel.TypeConverterAttribute), false).Length == 0) { goto serial; } System.ComponentModel.TypeConverter conv = System.ComponentModel.TypeDescriptor.GetConverter(t); if (conv.CanConvertFrom(typeof(string))) { try{ return(conv.ConvertFromString(value)); }catch {} } serial: if (t.GetCustomAttributes(typeof(System.SerializableAttribute), false).Length == 0) { goto op_implicit; } using (System.IO.MemoryStream memstr = new System.IO.MemoryStream(System.Convert.FromBase64String(value))){ return(Convert.binF.Deserialize(memstr)); } op_implicit: System.Reflection.MethodInfo[] ms = t.GetMethods(BF_PublicStatic); for (int i = 0; i < ms.Length; i++) { if (ms[i].ReturnType != t) { continue; } if (ms[i].Name != "op_Implicit" && ms[i].Name != "op_Explicit") { continue; } System.Reflection.ParameterInfo[] ps = ms[i].GetParameters(); if (ps.Length != 1 || ps[0].ParameterType != typeof(string)) { continue; } return(ms[i].Invoke(null, new object[] { value })); } throw new System.ArgumentException(string.Format(FROMSTR_NOTSUPPORTEDTYPE, t), "t"); } #endif }