Skip to main content
added 209 characters in body
Source Link
Jin-K
  • 105
  • 1
  • 7

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match.

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution or should I (in MagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that)?

EDIT:

Finally, I created an extension method to handle this:

internal static string SpecialFormat(this string input, object parameters) { var type = parameters.GetType(); System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( "\\{(.*?)\\}" ); var sb = new System.Text.StringBuilder(); var pos = 0; foreach (System.Text.RegularExpressions.Match toReplace in regex.Matches( input )) { var capture = toReplace.Groups[ 0 ]; var paramName = toReplace.Groups[ toReplace.Groups.Count - 1 ].Value; var property = type.GetProperty( paramName ); if (property == null) continue; sb.Append( input.Substring( pos, capture.Index - pos) ); sb.Append( property.GetValue( parameters, null ) ); pos = capture.Index + capture.Length; } if (input.Length > pos + 1) sb.Append( input.Substring( pos ) ); return sb.ToString(); } 

and I call it like this:

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{parameter2} Today we're {parameter1}"; var result = retrievedString.SpecialFormat( new { parameter1, parameter2 } ); 

Now, I don't use double braces anymore.

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match.

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution or should I (in MagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that)?

EDIT:

Finally, I created an extension method to handle this:

internal static string SpecialFormat(this string input, object parameters) { var type = parameters.GetType(); System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( "\\{(.*?)\\}" ); var sb = new System.Text.StringBuilder(); var pos = 0; foreach (System.Text.RegularExpressions.Match toReplace in regex.Matches( input )) { var capture = toReplace.Groups[ 0 ]; var paramName = toReplace.Groups[ toReplace.Groups.Count - 1 ].Value; var property = type.GetProperty( paramName ); if (property == null) continue; sb.Append( input.Substring( pos, capture.Index - pos) ); sb.Append( property.GetValue( parameters, null ) ); pos = capture.Index + capture.Length; } if (input.Length > pos + 1) sb.Append( input.Substring( pos ) ); return sb.ToString(); } 

and I call it like this:

retrievedString.SpecialFormat( new { parameter1, parameter2 } ); 

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match.

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution or should I (in MagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that)?

EDIT:

Finally, I created an extension method to handle this:

internal static string SpecialFormat(this string input, object parameters) { var type = parameters.GetType(); System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( "\\{(.*?)\\}" ); var sb = new System.Text.StringBuilder(); var pos = 0; foreach (System.Text.RegularExpressions.Match toReplace in regex.Matches( input )) { var capture = toReplace.Groups[ 0 ]; var paramName = toReplace.Groups[ toReplace.Groups.Count - 1 ].Value; var property = type.GetProperty( paramName ); if (property == null) continue; sb.Append( input.Substring( pos, capture.Index - pos) ); sb.Append( property.GetValue( parameters, null ) ); pos = capture.Index + capture.Length; } if (input.Length > pos + 1) sb.Append( input.Substring( pos ) ); return sb.ToString(); } 

and I call it like this:

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{parameter2} Today we're {parameter1}"; var result = retrievedString.SpecialFormat( new { parameter1, parameter2 } ); 

Now, I don't use double braces anymore.

Found solution
Source Link
Jin-K
  • 105
  • 1
  • 7

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match.

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution or should I (in MagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that)?

EDIT:

Finally, I created an extension method to handle this:

internal static string SpecialFormat(this string input, object parameters) { var type = parameters.GetType(); System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( "\\{(.*?)\\}" ); var sb = new System.Text.StringBuilder(); var pos = 0; foreach (System.Text.RegularExpressions.Match toReplace in regex.Matches( input )) { var capture = toReplace.Groups[ 0 ]; var paramName = toReplace.Groups[ toReplace.Groups.Count - 1 ].Value; var property = type.GetProperty( paramName ); if (property == null) continue; sb.Append( input.Substring( pos, capture.Index - pos) ); sb.Append( property.GetValue( parameters, null ) ); pos = capture.Index + capture.Length; } if (input.Length > pos + 1) sb.Append( input.Substring( pos ) ); return sb.ToString(); } 

and I call it like this:

retrievedString.SpecialFormat( new { parameter1, parameter2 } ); 

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match.

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution or should I (in MagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that)?

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match.

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution or should I (in MagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that)?

EDIT:

Finally, I created an extension method to handle this:

internal static string SpecialFormat(this string input, object parameters) { var type = parameters.GetType(); System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( "\\{(.*?)\\}" ); var sb = new System.Text.StringBuilder(); var pos = 0; foreach (System.Text.RegularExpressions.Match toReplace in regex.Matches( input )) { var capture = toReplace.Groups[ 0 ]; var paramName = toReplace.Groups[ toReplace.Groups.Count - 1 ].Value; var property = type.GetProperty( paramName ); if (property == null) continue; sb.Append( input.Substring( pos, capture.Index - pos) ); sb.Append( property.GetValue( parameters, null ) ); pos = capture.Index + capture.Length; } if (input.Length > pos + 1) sb.Append( input.Substring( pos ) ); return sb.ToString(); } 

and I call it like this:

retrievedString.SpecialFormat( new { parameter1, parameter2 } ); 
Post Closed as "Duplicate" by Dennis c#
deleted 16 characters in body
Source Link
DavidG
  • 119.8k
  • 13
  • 232
  • 239

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match !.

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution ? Oror should I (in MagicMethodMagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that).

Thank you !?

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match !

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution ? Or should I (in MagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that).

Thank you !

I'm trying to format some string dynamically with available variables in a specific context/scope.

This strings would have parts with things like {{parameter1}}, {{parameter2}} and these variables would exist in the scope where I'll try to reformat the string. The variable names should match.

I looked for something like a dynamically string interpolation approach, or how to use FormattableStringFactory, but I found nothing that really gives me what I need.

var parameter1 = DateTime.Now.ToString(); var parameter2 = "Hello world!"; var retrievedString = "{{parameter2}} Today we're {{parameter1}}"; var result = MagicMethod(retrievedString, parameter1, parameter2); // or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 }); 

Is there an existing solution or should I (in MagicMethod) replace these parts in the retrievedString with matching members of the anonymous object given as parameter (using reflection or something like that)?

Source Link
Jin-K
  • 105
  • 1
  • 7
Loading