StringExtensionsInterpolateT(String, T) Method

Applies string interpolation to the given format string at runtime.

Definition

Namespace: Gemstone.StringExtensions
Assembly: Gemstone.Common (in Gemstone.Common.dll) Version: 1.0.128 -- Release Build+d050cfc5563c89a1188cc3c6b2d417530856f490
public static string Interpolate<T>(
	this string format,
	T parameters
)

Parameters

format  String
The format string to be interpolated.
parameters  T
The parameters that can be referenced by the format string.

Type Parameters

T
The type of the object that defines the parameters for the format string.

Return Value

String
A copy of format in which the format items have been replaced by the string representation of the corresponding parameters.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type String. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

This overload uses reflection to obtain the name and value of parameters' properties to make those available to the interpolated format string by name. This can be used with user-defined types, but is mainly intended to be used with anonymous types.

C#
string format = "{hello} {world}!";
var parameters = new { hello = "Hello", world = "World" };
Console.WriteLine(format.Interpolate(parameters));

If parameters can be safely cast to

C#
IEnumerable{KeyValuePair{string, object}}
, this function will skip the reflection and call Interpolate(String, IEnumerableKeyValuePairString, Object) directly.

See Also