Click or drag to resize

StringExtensionsInterpolateT(String, T) Method

Applies string interpolation to the given format string at runtime.

Namespace: GSF
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.181-beta
Syntax
public static string Interpolate<T>(
	this string format,
	T parameters
)
View Source

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