DynamicPropertyExtensionAsITypedPropertyT Method

Casts a DynamicProperty to a ITypedPropertyT in order to have type safe access to its Value without having to use dynamic .NET properties ("late binding").

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public static ITypedProperty<T> AsITypedProperty<T>(
	this DynamicProperty property
)

Parameters

property  DynamicProperty
The DynamicProperty to cast.

Type Parameters

T
The DataType of the TypeConverterT that property has been initialized with.

Return Value

ITypedPropertyT
property casted as ITypedPropertyT.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type DynamicProperty. 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 method exists in order to support high-performance scenarios. It allows you to process value types without boxing and unboxing.

The method is just syntactic sugar around a simple cast. When passing a null reference as argument the compiler will give you a nullability warning but the return type will be null.

  Caution

When using nullable reference types, take care to cast to the correct nullability: The compiler won't warn you when casting incorrectly!

Example

Object serialization with CSV:

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

namespace Benchmarks;

internal static partial class CalculationReader
{
    internal static IList<Calculation> ReadPerformance(string csv)
    {
        // Strict parsers are faster but less flexible:
        var doubleConverter = new DoubleConverter(styles: NumberStyles.Integer
                                                        | NumberStyles.AllowLeadingSign
                                                        | NumberStyles.AllowDecimalPoint);

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

        using var stringReader = new StringReader(csv);
        using var csvReader = new CsvReader(stringReader, options: CsvOpts.Default
        /* Using the DisableCaching option avoids cloning the */ | CsvOpts.DisableCaching);
        /* the CsvRecord instance with each Read().           */

        var list = new List<Calculation>(50); // (This data storage allows further optimization.)

        // Using ITypedProperty<T> instances avoids boxing and unboxing of value types.
        // The AsITypedProperty<T>() extension method is "syntactic sugar" that can be
        // replaced with a simple cast to save a few nanoseconds.
        ITypedProperty<double> first = mapping[0].AsITypedProperty<double>();
        ITypedProperty<char> op = mapping[1].AsITypedProperty<char>();
        ITypedProperty<double> second = mapping[2].AsITypedProperty<double>();
        ITypedProperty<double> result = mapping[3].AsITypedProperty<double>();

        CsvRecord? record;

        while ((record = csvReader.Read()) != null)
        {
            mapping.Record = record;
            list.Add(new Calculation(first.Value, op.Value, second.Value, result.Value));
        }

        return list;
    }
}

Exceptions

InvalidCastExceptionproperty is not of type ITypedPropertyT.

See Also