CsvMappingBuilderAddPropertyT(String, TypeConverterT) Method

Adds a new DynamicProperty instance that accesses a single column of a CSV file with its column name.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public CsvMappingBuilder AddProperty<T>(
	string propertyName,
	TypeConverter<T> converter
)

Parameters

propertyName  String
The identifier of the dynamic .NET property and the corresponding column of the CSV file. The value of the argument must follow the rules for C# identifiers. Only ASCII characters are accepted.
converter  TypeConverterT
The TypeConverterT that does the type conversion.

Type Parameters

T
The .NET data type of the dynamic property.

Return Value

CsvMappingBuilder
The CsvMappingBuilder to chain calls.

Remarks

When using this method, propertyName and the referenced CSV column name must match, and propertyName must meet the requirements for C# identifiers. Use the HasCaseSensitiveColumnNames property to determine whether the comparison is case-sensitive.

If the column name of the CSV file does not meet the requirements of C# identifiers, use AddPropertyT(String, IEnumerableString, TypeConverterT) instead.

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.

Object serialization with CSV:

C#
using FolkerKinzel.CsvTools;
using FolkerKinzel.CsvTools.Mappings;
using FolkerKinzel.CsvTools.Mappings.TypeConverters;

namespace Benchmarks;

internal static partial class CalculationWriter
{
    internal static string WriteDefault(Calculation[] data)
    {
        var doubleConverter = new DoubleConverter();

        CsvMapping mapping = CsvMappingBuilder
            .Create()
            .AddProperty("First", doubleConverter)
            .AddProperty("Operator", new CharConverter())
            .AddProperty("Second", doubleConverter)
            .AddProperty("Result", doubleConverter)
            .Build();

        return data.ToCsv(
            mapping, 
            static (calculation, mapping) =>
                   {
                       mapping.First = calculation.First;
                       mapping.Operator = calculation.Operator;
                       mapping.Second = calculation.Second;
                       mapping.Result = calculation.Result;
                   });
    }
}

Exceptions

ArgumentNullExceptionpropertyName or converter is null.
ArgumentException

propertyName does not conform to the rules for C# identifiers (only ASCII characters)

- or -

a DynamicProperty with the same PropertyName has already been added.

RegexMatchTimeoutException Validating of propertyName takes too long.

See Also