Class MessageArguments

Namespace
RentADeveloper.ResXLocalization
Assembly
ResXLocalization.Core.dll

The arguments for a translation lookup: positional arguments from values, named arguments from name/value tuples, a dictionary or an object's properties. Build them with the static methods and pass them to ILocalizer.Get, for example Localizer.Current.Get("FilePosition", MessageArguments.FromValues(3, 10)).

public sealed class MessageArguments : IEnumerable<MessageArgument>, IEnumerable
Inheritance
MessageArguments
Implements
Inherited Members

Remarks

An argument's position is its Position, and only that: an argument's index in the enumeration is not guaranteed to be the same as its Position. The positions are 0 to Count minus 1, each exactly once; no name is an empty string, and no name occurs twice. The static methods check this when they build the arguments, and an instance cannot be changed afterwards.

The default formatter (StringFormatMessageFormatter) ignores names and uses positions; a formatter that understands names (ICU MessageFormat) finds a named argument under both.

Properties

Count

Gets the number of arguments.

public int Count { get; }

Property Value

int

Empty

Gets an instance without arguments.

public static MessageArguments Empty { get; }

Property Value

MessageArguments

Methods

FromDictionary(IReadOnlyDictionary<string, object?>)

Builds named arguments from an existing dictionary: MessageArguments.FromDictionary(new Dictionary<string, object?> { ["Count"] = 5 }). The positions follow the dictionary's enumeration order: the first entry has position 0, the next 1, and so on.

public static MessageArguments FromDictionary(IReadOnlyDictionary<string, object?> arguments)

Parameters

arguments IReadOnlyDictionary<string, object>

The arguments, keyed by name.

Returns

MessageArguments

One named argument per entry.

Exceptions

ArgumentNullException

arguments is null.

ArgumentException

A key is an empty string.

FromObject<T>(T)

Builds named arguments from the public instance properties of an object, for example an anonymous one: MessageArguments.FromObject(new { Count = 5, Total = 10 }). Meant for named arguments.

public static MessageArguments FromObject<T>(T source)

Parameters

source T

The object to read the property values from.

Returns

MessageArguments

One named argument per property.

Type Parameters

T

The type whose public instance properties become the arguments.

Remarks

The properties are those of the static type T, without indexers, in metadata order - the declaration order for a class or an anonymous type; the first property has position 0, the next 1, and so on. A value typed object yields no arguments. An exception thrown by a property getter propagates.

Trim and Native AOT safe: the properties of T are preserved. A generic method of your own that forwards its type parameter to MessageArguments.FromObject must annotate that parameter with the same [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)], or the trimmer reports IL2091 at that call.

Exceptions

ArgumentNullException

source is null.

ArgumentException

Two properties have the same name, for example a base property hidden with new and a different property type.

FromTuples(params (string? Name, object? Value)[])

Builds arguments from name/value tuples, in declaration order: MessageArguments.FromTuples(("Count", 5), ("Total", 10)). A null name makes that argument positional only, so named and unnamed arguments can be mixed: MessageArguments.FromTuples((null, file), ("Total", 10)).

public static MessageArguments FromTuples(params (string? Name, object? Value)[] arguments)

Parameters

arguments (string Name, object Value)[]

The name/value pairs; the first has position 0, the next 1, and so on.

Returns

MessageArguments

One argument per tuple.

Exceptions

ArgumentNullException

arguments is null.

ArgumentException

A name is an empty string, or two tuples have the same name (compared ordinally, so A and a differ).

FromValues(params object?[])

Builds positional arguments from values: MessageArguments.FromValues(3, 10) for File {0} of {1}.

public static MessageArguments FromValues(params object?[] values)

Parameters

values object[]

The argument values; the first has position 0, the next 1, and so on.

Returns

MessageArguments

One unnamed argument per value.

Remarks

Because of params, MessageArguments.FromValues(dictionary) is one positional argument whose value is the dictionary itself; use FromDictionary(IReadOnlyDictionary<string, object?>) for named arguments. One null value is MessageArguments.FromValues((object?)null).

Exceptions

ArgumentNullException

values is null.

GetEnumerator()

Returns an enumerator over the arguments. An argument's index in the enumeration is not guaranteed to be the same as its Position; read the position from Position.

public IEnumerator<MessageArgument> GetEnumerator()

Returns

IEnumerator<MessageArgument>

The enumerator.