Source/NFX/Environment/MacroRunner.cs
/// <summary>
/// Returns a string value converted to desired type with optional default and format
/// </summary>
/// <param name="value">String value to convert</param>
/// <param name="type">A type to convert string value into i.e. "decimal"</param>
/// <param name="dflt">Default value which is used when conversion of original value can not be made</param>
/// <param name="fmt">Format string that formats the converted value. Example: 'Goods: {0}'. The '0' index is the value</param>
/// <returns>Converted value to desired type then back to string, using optional formatting and default if conversion did not succeed</returns>
Returns a string value converted to desired type with optional default and format
Returns a string value converted to desired type with optional default and format
public static string GetValueAs(string value, string type, string dflt = null, string fmt = null)
            {
              var mi = typeof(StringValueConversion).GetMethod("As"+type.CapitalizeFirstChar(), BindingFlags.Public | BindingFlags.Static);
              object result;
              if (!string.IsNullOrWhiteSpace(dflt))
              {
                  var dval = mi.Invoke(null, new object[] { dflt, null});
                  result = mi.Invoke(null, new object[] { value, dval});
              }
              else
                result = mi.Invoke(null, new object[] { value, null});
              if (result==null) return string.Empty;
              if (!string.IsNullOrWhiteSpace(fmt))
                return string.Format(fmt, result);
              else
                return result.ToString();
            }
public static string GetValueAs ( string value , string type , string dflt = null , string fmt = null ) { var mi = typeof ( StringValueConversion ) . GetMethod ( " " + type . CapitalizeFirstChar ( ) , BindingFlags . Public | BindingFlags . Static ) ; object result ; if ( ! string . IsNullOrWhiteSpace ( dflt ) ) { var dval = mi . Invoke ( null , new object [ ] { dflt , null } ) ; result = mi . Invoke ( null , new object [ ] { value , dval } ) ; } else result = mi . Invoke ( null , new object [ ] { value , null } ) ; if ( result == null ) return string . Empty ; if ( ! string . IsNullOrWhiteSpace ( fmt ) ) return string . Format ( fmt , result ) ; else return result . ToString ( ) ; }
Returns a string value converted to desired type with optional default and format
Returns a string value converted to desired type with optional default and format
Converted value to desired type then back to string, using optional formatting and default if conversion did not succeed
Converted value to desired type then back to string using optional formatting and default if conversion did not succeed
value
string
String value to convert
String value to convert
type
string
A type to convert string value into i.e. "decimal"
A type to convert string value into i . e . " decimal "
dflt
string
Default value which is used when conversion of original value can not be made
Default value which is used when conversion of original value can not be made
fmt
string
Format string that formats the converted value.
Format string that formats the converted value .