public bool Set <T>(T t) { //var res=Activator.CreateInstance(type,ItemRank.magic); Type[] arr; if (!CacheInterfaceDictionary.ContainsKey(typeof(T))) { arr = InterfaceUtils.GetInterfaces <IFormattable <T> >(); if (arr.Length != 1) { throw new Exception(); } CacheInterfaceDictionary[typeof(T)] = arr; } else { arr = CacheInterfaceDictionary[typeof(T)]; }; //if(arr.Length!=1) // throw new Exception(); var target = arr.First(); IFormattable <T> formattable = (IFormattable <T>)Activator.CreateInstance(target); formattable.Set(t); //return new FormattableMaster{ FormattableName = TypeUtils.TypeToString(formattable.GetType()); DataType = formattable.DataType; Data = formattable.Data; return(true); //}; }
}//PrintFormat() /// <summary> /// Outputs the data with the specified format. /// </summary> /// <param name="inputData"></param> /// Data item /// <param name="formatString"></param> /// Format string /// <param name="outFormat"></param> /// Special output format for custom output spacing. /// <param name="provider"></param> /// Custom format provider public static void PrintFormat(IFormattable inputData, string formatString, IFormatProvider provider) { try { if (provider == null) { Console.WriteLine("{0}\t{1}", formatString, inputData.ToString(formatString, provider)); }//if else { string formstr = "{0} in the custom " + formatString + " format is {1:" + formatString + "}"; Console.WriteLine(string.Format(provider, formstr, inputData, inputData)); } //else } //try catch (Exception e) { Console.WriteLine("Exception in PrintFormat(): {0}", e.Message); Console.WriteLine("Data was {0}, Format string was: {1}, Type was {2}", inputData, formatString, inputData.GetType().Name); } //catch } //PrintFormat(IFormattable inputData, string formatString, IFormatProvider provider)
/// <summary>Formats the object using the formatting arguments from the collection.</summary> /// <param name="obj"> /// The IFormattable object to get the formatted string representation from. /// </param> /// <returns> /// A formatted string representing the object. /// </returns> public string ToString(IFormattable obj) { if (obj == null) { return(null); } var arguments = Get(obj.GetType()); return(arguments.ToString(obj)); }
public static string FormatForConsole(this IFormattable o) { var objType = o.GetType(); var result = $"[{objType.Name}] - "; var allProps = objType.GetProperties(); foreach (var propInfo in allProps) { var propName = propInfo.Name; var propValue = propInfo.GetValue(o); result += $"{propName}={propValue}\t"; } return(result); }
/// <summary>Formats the object using the formatting arguments from the collection.</summary> /// <param name="obj"> /// The IFormattable object to get the formatted string representation from. /// </param> /// <returns> /// A formatted string representing the object. /// </returns> public string ToString(IFormattable obj) { if (obj is null) { #pragma warning disable S2225 // "ToString()" method should not return null // if the origin is null, it should not become string.Empty. return(null); #pragma warning restore S2225 } var arguments = Get(obj.GetType()); return(arguments.ToString(obj)); }
/// boxing and unboxing operations can hurt performance; however, generic solve this issue static void Main(string[] args) { // All interfaces that INT32 implements IFormattable a = 3; Console.WriteLine(a); Console.WriteLine(a.GetType()); IComparable b = 4; Console.WriteLine(b); Console.WriteLine(b.GetType()); IConvertible c = 5; Console.WriteLine(c); Console.WriteLine(c.GetType()); IComparable <int> d = 6; IEquatable <int> f = 7; // PrimitiveTypes.Example(); }
}//PrintFormat() /// <summary> /// Outputs the data with the specified format. /// </summary> /// <param name="inputData"></param> /// Data item /// <param name="formatString"></param> /// Format string /// <param name="outFormat"></param> /// Special output format for custom output spacing. /// <param name="provider"></param> /// Custom format provider public static void PrintFormat(IFormattable inputData, string formatString, IFormatProvider provider) { try { if (provider == null) { Console.WriteLine("{0}\t{1}", formatString, inputData.ToString(formatString, provider)); }//if else { string formstr = "{0} in the custom " + formatString + " format is {1:" + formatString + "}"; Console.WriteLine(string.Format(provider, formstr, inputData, inputData)); }//else }//try catch (Exception e) { Console.WriteLine("Exception in PrintFormat(): {0}", e.Message); Console.WriteLine("Data was {0}, Format string was: {1}, Type was {2}", inputData, formatString, inputData.GetType().Name); }//catch }//PrintFormat(IFormattable inputData, string formatString, IFormatProvider provider)