KGy SOFT

PropertyAccessor Class

KGy SOFT Core Libraries Help
Provides an efficient way for setting and getting property values via dynamically created delegates.
See the Remarks section for details and an example.
Inheritance Hierarchy

SystemObject
  KGySoft.ReflectionMemberAccessor
    KGySoft.ReflectionPropertyAccessor

Namespace:  KGySoft.Reflection
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0-rc.1
Syntax

public abstract class PropertyAccessor : MemberAccessor

The PropertyAccessor type exposes the following members.

Properties

  NameDescription
Public propertyCanRead
Gets whether the property can be read (has get accessor).
Public propertyCanWrite
Gets whether the property can be written (has set accessor).
Public propertyMemberInfo
Gets the reflection member info of the accessed member.
(Inherited from MemberAccessor.)
Top
Methods

  NameDescription
Public methodEquals
Determines whether the specified Object is equal to the current MemberAccessor.
(Inherited from MemberAccessor.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGet
Gets the value of the property. For static properties the instance parameter is omitted (can be ). If the property is not an indexer, then indexerParameters parameter is omitted.
Public methodStatic memberGetAccessor
Gets a PropertyAccessor for the specified property.
Public methodGetHashCode
Gets a hash code for the current MemberAccessor instance.
(Inherited from MemberAccessor.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodSet
Sets the property. For static properties the instance parameter is omitted (can be ). If the property is not an indexer, then indexerParameters parameter is omitted.
Public methodToString
Returns a String that represents the current MemberAccessor.
(Inherited from MemberAccessor.)
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Top
Remarks

You can obtain a PropertyAccessor instance by the static GetAccessor method.

The Get and Set methods can be used to get and set the property, respectively. The first call of these methods are slow because the delegates are generated on the first access, but further calls are much faster.

The already obtained accessors are cached so subsequent GetAccessor calls return the already created accessors unless they were dropped out from the cache, which can store about 8000 elements.

Note Note
If you want to access a property by name rather then by a PropertyInfo, then you can use the SetProperty and GetProperty methods in the Reflector class, which have some overloads with a propertyName parameter.
Caution note Caution
The .NET Standard 2.0 version of the Set method throws a PlatformNotSupportedException if the property is an instance member of a value type (struct).
If you reference the .NET Standard 2.0 version of the KGySoft.CoreLibraries assembly, then use the Reflector.SetProperty methods to set value type instance properties.
Examples

C#
using System;
using System.Reflection;
using KGySoft.Diagnostics;
using KGySoft.Reflection;

class Example
{
    private class TestClass
    {
        public int TestProperty { get; set; }
    }

    static void Main(string[] args)
    {
        var instance = new TestClass();
        PropertyInfo property = instance.GetType().GetProperty(nameof(TestClass.TestProperty));
        PropertyAccessor accessor = PropertyAccessor.GetAccessor(property);

        new PerformanceTest { TestName = "Set Property", Iterations = 1000000 }
            .AddCase(() => instance.TestProperty = 1, "Direct set")
            .AddCase(() => property.SetValue(instance, 1), "PropertyInfo.SetValue")
            .AddCase(() => accessor.Set(instance, 1), "PropertyAccessor.Set")
            .DoTest()
            .DumpResults(Console.Out);

        new PerformanceTest<int> { TestName = "Get Property", Iterations = 1000000 }
            .AddCase(() => instance.TestProperty, "Direct get")
            .AddCase(() => (int)property.GetValue(instance), "PropertyInfo.GetValue")
            .AddCase(() => (int)accessor.Get(instance), "PropertyAccessor.Get")
            .DoTest()
            .DumpResults(Console.Out);
    }
}

// This code example produces a similar output to this one:
// ==[Set Property Results]================================================
// Iterations: 1,000,000
// Warming up: Yes
// Test cases: 3
// Calling GC.Collect: Yes
// Forced CPU Affinity: 2
// Cases are sorted by time (quickest first)
// --------------------------------------------------
// 1. Direct set: average time: 2.93 ms
// 2. PropertyAccessor.Set: average time: 24.22 ms (+21.29 ms / 825.79 %)
// 3. PropertyInfo.SetValue: average time: 214.56 ms (+211.63 ms / 7,314.78 %)
// 
// ==[Get Property Results]================================================
// Iterations: 1,000,000
// Warming up: Yes
// Test cases: 3
// Calling GC.Collect: Yes
// Forced CPU Affinity: 2
// Cases are sorted by time (quickest first)
// --------------------------------------------------
// 1. Direct get: average time: 2.58 ms
// 2. PropertyAccessor.Get: average time: 16.62 ms (+14.05 ms / 645.30 %)
// 3. PropertyInfo.GetValue: average time: 169.12 ms (+166.54 ms / 6,564.59 %)
See Also

Reference