├── .gitattributes
├── FastAccessors.Tests
├── TestClasses.cs
├── FastAccessors.Tests.NET.csproj
├── FastAccessors.Tests.Net5.csproj
├── Properties
│ └── AssemblyInfo.cs
├── FastAccessors.Tests.csproj
├── Field.Monads.cs
└── Field.cs
├── LICENSE.TXT
├── FastAccessors
├── AccessorKey.cs
├── FastAccessors.Net5.csproj
├── Properties
│ └── AssemblyInfo.cs
├── FastAccessors.NET.csproj
├── FastAccessors.csproj
├── Monads
│ └── Accessor.cs
├── API
│ └── Field.cs
└── Accessors
│ └── Field.cs
├── FastAccessors.Benchmarks
├── FastAccessors.Benchmarks.NET.csproj
├── FastAccessors.Benchmarks.Net5.csproj
├── TestClasses.cs
├── Properties
│ └── AssemblyInfo.cs
├── Benchmark.cs
├── FastAccessors.cs
└── Reflection.cs
├── FastAccessors.sln
├── FastAccessors.NET.sln
├── FastAccessors.Net5.sln
├── README.md
└── .gitignore
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/FastAccessors.Tests/TestClasses.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors.Tests {
2 | class Foo {
3 | static string static_Name = "Foo(Static)";
4 | static string GetName() {
5 | return static_Name;
6 | }
7 | string private_Name;
8 | readonly string readonly_Name;
9 | public string public_Name;
10 | public Foo(string name) {
11 | this.readonly_Name = name + "(Readonly)";
12 | this.private_Name = name + "(Private)";
13 | this.public_Name = name + "(Public)";
14 | }
15 | }
16 | class Bar {
17 | static int static_Size = "Bar(Static)".Length;
18 | static int GetSize() {
19 | return static_Size;
20 | }
21 | int private_Size;
22 | readonly int readonly_Size;
23 | public int public_Size;
24 | public Bar(int size) {
25 | this.readonly_Size = size + "(Readonly)".Length;
26 | this.private_Size = size + "(Private)".Length;
27 | this.public_Size = size + "(Public)".Length;
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/LICENSE.TXT:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) Dmitry Garavsky
4 |
5 | All rights reserved.
6 |
7 | Permission is hereby granted, free of charge, to any person obtaining a copy
8 | of this software and associated documentation files (the "Software"), to deal
9 | in the Software without restriction, including without limitation the rights
10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | copies of the Software, and to permit persons to whom the Software is
12 | furnished to do so, subject to the following conditions:
13 |
14 | The above copyright notice and this permission notice shall be included in all
15 | copies or substantial portions of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 | SOFTWARE.
24 |
--------------------------------------------------------------------------------
/FastAccessors/AccessorKey.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors {
2 | using System;
3 |
4 | static class AccessorKey {
5 | const int prime = 16777619;
6 | internal static int Make(Type type, string fieldName) {
7 | unchecked {
8 | return type.GetHashCode() * prime + fieldName.GetHashCode();
9 | }
10 | }
11 | internal static string GetKey(Type type, string fieldName) {
12 | return GetTypeName(type) + "." + fieldName;
13 | }
14 | static string GetTypeName(Type type) {
15 | if(!type.IsGenericType)
16 | return type.Name;
17 | var sb = new System.Text.StringBuilder(type.Name);
18 | int argumentsPos = type.Name.IndexOf('`');
19 | sb.Remove(argumentsPos, type.Name.Length - argumentsPos);
20 | sb.Append('<');
21 | var genericArgs = type.GetGenericArguments();
22 | for(int i = 0; i < genericArgs.Length; i++) {
23 | sb.Append(GetTypeName(genericArgs[i]));
24 | if(i > 0) sb.Append(',');
25 | }
26 | sb.Append('>');
27 | return sb.ToString();
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/FastAccessors/FastAccessors.Net5.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}
4 | False
5 | Library
6 | net5.0
7 |
8 | FastAccessors
9 | FastAccessors
10 | ..\bin\
11 | false
12 | False
13 | False
14 | 4
15 | true
16 | True
17 | Fast Accessors Library for .Net.
18 | Dmitry Garavsky
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/FastAccessors.Benchmarks/FastAccessors.Benchmarks.NET.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}
4 | False
5 | Exe
6 | net6.0
7 |
8 | FastAccessors.Benchmarks
9 | FastAccessors.Benchmarks.NET
10 | ..\bin\
11 | false
12 | False
13 | False
14 | 4
15 | true
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/FastAccessors.Benchmarks/FastAccessors.Benchmarks.Net5.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}
4 | False
5 | Exe
6 | net5.0
7 |
8 | FastAccessors.Benchmarks
9 | FastAccessors.Benchmarks.Net5
10 | ..\bin\
11 | false
12 | False
13 | False
14 | 4
15 | true
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/FastAccessors.Benchmarks/TestClasses.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.CompilerServices;
2 |
3 | namespace FastAccessors.Benchmarks {
4 | class Foo {
5 | static string static_Name = "Foo(Static)";
6 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
7 | public static string GetName() {
8 | return static_Name;
9 | }
10 | string private_Name;
11 | readonly string readonly_Name;
12 | public string public_Name;
13 | public Foo(string name) {
14 | this.readonly_Name = name + "(Readonly)";
15 | this.private_Name = name + "(Private)";
16 | this.public_Name = name + "(Public)";
17 | }
18 | }
19 | class Bar {
20 | static int static_Size = "Bar(Static)".Length;
21 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
22 | public static int GetSize() {
23 | return static_Size;
24 | }
25 | int private_Size;
26 | readonly int readonly_Size;
27 | public int public_Size;
28 | public Bar(int size) {
29 | this.readonly_Size = size + "(Readonly)".Length;
30 | this.private_Size = size + "(Private)".Length;
31 | this.public_Size = size + "(Public)".Length;
32 | }
33 | }
34 | static class FooBar {
35 | static string DefaultName = nameof(FooBar);
36 | static string GetDefaultName() {
37 | return DefaultName;
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/FastAccessors.Tests/FastAccessors.Tests.NET.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}
4 | False
5 | Library
6 | net6.0
7 |
8 | FastAccessors.Tests
9 | FastAccessors.Tests
10 | true
11 | ..\bin\
12 | false
13 | False
14 | False
15 | 4
16 | true
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/FastAccessors.Tests/FastAccessors.Tests.Net5.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}
4 | False
5 | Library
6 | net5.0
7 |
8 | FastAccessors.Tests
9 | FastAccessors.Tests
10 | true
11 | ..\bin\
12 | false
13 | False
14 | False
15 | 4
16 | true
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/FastAccessors/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FastAccessors")]
9 | [assembly: AssemblyDescription("Fast Accessors Library for .Net")]
10 | [assembly: AssemblyCompany("Dmitry Garavsky(https://github.com/DmitryGaravsky)")]
11 | [assembly: AssemblyProduct("FastAccessors")]
12 | [assembly: AssemblyCopyright("Dmitry Garavsky, ©2021")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("97ab9619-c263-4ff8-a92f-89da0bfa8ccc")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
36 |
--------------------------------------------------------------------------------
/FastAccessors.Tests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FastAccessors.Tests")]
9 | [assembly: AssemblyDescription("Fast Accessors Library for .Net")]
10 | [assembly: AssemblyCompany("Dmitry Garavsky(https://github.com/DmitryGaravsky)")]
11 | [assembly: AssemblyProduct("FastAccessors.Tests")]
12 | [assembly: AssemblyCopyright("Dmitry Garavsky, ©2021")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("dbaaf003-d410-483a-a9a1-5e83c13548f5")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
36 |
--------------------------------------------------------------------------------
/FastAccessors.Benchmarks/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FastAccessors.Benchmarks")]
9 | [assembly: AssemblyDescription("Fast Accessors Library for .Net")]
10 | [assembly: AssemblyCompany("Dmitry Garavsky(https://github.com/DmitryGaravsky)")]
11 | [assembly: AssemblyProduct("FastAccessors.Benchmarks")]
12 | [assembly: AssemblyCopyright("Dmitry Garavsky, ©2021")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("CA7E802F-9B68-40C5-92CC-AC6D71F83176")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.1")]
35 | [assembly: AssemblyFileVersion("1.0.0.1")]
36 |
--------------------------------------------------------------------------------
/FastAccessors.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.40629.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastAccessors", "FastAccessors\FastAccessors.csproj", "{2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastAccessors.Tests", "FastAccessors.Tests\FastAccessors.Tests.csproj", "{268CA85D-0867-4571-8BA9-3F8FAC05896D}"
9 | ProjectSection(ProjectDependencies) = postProject
10 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D} = {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}
11 | EndProjectSection
12 | EndProject
13 | Global
14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
15 | Debug|Any CPU = Debug|Any CPU
16 | Release|Any CPU = Release|Any CPU
17 | EndGlobalSection
18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
19 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Debug|Any CPU.Build.0 = Debug|Any CPU
21 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Release|Any CPU.ActiveCfg = Release|Any CPU
22 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Release|Any CPU.Build.0 = Release|Any CPU
23 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Debug|Any CPU.Build.0 = Debug|Any CPU
25 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Release|Any CPU.ActiveCfg = Release|Any CPU
26 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Release|Any CPU.Build.0 = Release|Any CPU
27 | EndGlobalSection
28 | GlobalSection(SolutionProperties) = preSolution
29 | HideSolutionNode = FALSE
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/FastAccessors.NET.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30320.27
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastAccessors.NET", "FastAccessors\FastAccessors.NET.csproj", "{2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastAccessors.Tests.NET", "FastAccessors.Tests\FastAccessors.Tests.NET.csproj", "{268CA85D-0867-4571-8BA9-3F8FAC05896D}"
9 | ProjectSection(ProjectDependencies) = postProject
10 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D} = {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}
11 | EndProjectSection
12 | EndProject
13 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastAccessors.Benchmarks.NET", "FastAccessors.Benchmarks\FastAccessors.Benchmarks.NET.csproj", "{6BB148CA-D784-4915-B11E-B42FC53CAA9A}"
14 | EndProject
15 | Global
16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
17 | Debug|Any CPU = Debug|Any CPU
18 | Release|Any CPU = Release|Any CPU
19 | EndGlobalSection
20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
21 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Debug|Any CPU.Build.0 = Debug|Any CPU
23 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Release|Any CPU.Build.0 = Release|Any CPU
25 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Debug|Any CPU.Build.0 = Debug|Any CPU
27 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Release|Any CPU.ActiveCfg = Release|Any CPU
28 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Release|Any CPU.Build.0 = Release|Any CPU
29 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
31 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}.Release|Any CPU.Build.0 = Release|Any CPU
33 | EndGlobalSection
34 | GlobalSection(SolutionProperties) = preSolution
35 | HideSolutionNode = FALSE
36 | EndGlobalSection
37 | GlobalSection(ExtensibilityGlobals) = postSolution
38 | SolutionGuid = {5A037153-7299-4904-ABD6-9A51A5A03D09}
39 | EndGlobalSection
40 | EndGlobal
41 |
--------------------------------------------------------------------------------
/FastAccessors.Net5.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30320.27
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastAccessors.Net5", "FastAccessors\FastAccessors.Net5.csproj", "{2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastAccessors.Tests.Net5", "FastAccessors.Tests\FastAccessors.Tests.Net5.csproj", "{268CA85D-0867-4571-8BA9-3F8FAC05896D}"
9 | ProjectSection(ProjectDependencies) = postProject
10 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D} = {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}
11 | EndProjectSection
12 | EndProject
13 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastAccessors.Benchmarks.Net5", "FastAccessors.Benchmarks\FastAccessors.Benchmarks.Net5.csproj", "{6BB148CA-D784-4915-B11E-B42FC53CAA9A}"
14 | EndProject
15 | Global
16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
17 | Debug|Any CPU = Debug|Any CPU
18 | Release|Any CPU = Release|Any CPU
19 | EndGlobalSection
20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
21 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Debug|Any CPU.Build.0 = Debug|Any CPU
23 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}.Release|Any CPU.Build.0 = Release|Any CPU
25 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Debug|Any CPU.Build.0 = Debug|Any CPU
27 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Release|Any CPU.ActiveCfg = Release|Any CPU
28 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}.Release|Any CPU.Build.0 = Release|Any CPU
29 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
31 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 | {6BB148CA-D784-4915-B11E-B42FC53CAA9A}.Release|Any CPU.Build.0 = Release|Any CPU
33 | EndGlobalSection
34 | GlobalSection(SolutionProperties) = preSolution
35 | HideSolutionNode = FALSE
36 | EndGlobalSection
37 | GlobalSection(ExtensibilityGlobals) = postSolution
38 | SolutionGuid = {5A037153-7299-4904-ABD6-9A51A5A03D09}
39 | EndGlobalSection
40 | EndGlobal
41 |
--------------------------------------------------------------------------------
/FastAccessors.Benchmarks/Benchmark.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors.Benchmarks {
2 | using BenchmarkDotNet.Attributes;
3 | using BF = System.Reflection.BindingFlags;
4 |
5 | [MemoryDiagnoser]
6 | public partial class Benchmark {
7 | public static void Main() {
8 | BenchmarkDotNet.Running.BenchmarkRunner.Run();
9 | }
10 | //
11 | [GlobalSetup]
12 | public void SetUp() {
13 | foo = new Foo("Foo");
14 | fi_public_Name = typeof(Foo).GetField("public_Name", BF.Public | BF.Instance);
15 | fi_static_Name = typeof(Foo).GetField("static_Name", BF.NonPublic | BF.Static);
16 | fi_private_Name = typeof(Foo).GetField("private_Name", BF.NonPublic | BF.Instance);
17 | fi_readonly_Name = typeof(Foo).GetField("readonly_Name", BF.NonPublic | BF.Instance);
18 | //
19 | bar = new Bar("Bar".Length);
20 | fi_public_Size = typeof(Bar).GetField("public_Size", BF.Public | BF.Instance);
21 | fi_static_Size = typeof(Bar).GetField("static_Size", BF.NonPublic | BF.Static);
22 | fi_private_Size = typeof(Bar).GetField("private_Size", BF.NonPublic | BF.Instance);
23 | fi_readonly_Size = typeof(Bar).GetField("readonly_Size", BF.NonPublic | BF.Instance);
24 | //
25 | Field.RegisterDefault("readonly_Name");
26 | fldKey_private_Name_Field = Field.Register("private_Name", typeof(Foo));
27 | fldKey_static_Name_Field = Field.RegisterStatic("static_Name", typeof(Foo));
28 | //
29 | Field.RegisterDefault("readonly_Size");
30 | fldKey_private_Size_Field = Field.Register("private_Size", typeof(Bar));
31 | fldKey_static_Size_Field = Field.RegisterStatic("static_Size", typeof(Bar));
32 | //
33 | Field.RegisterDefaultStatic("DefaultName", typeof(FooBar));
34 | }
35 | Foo foo;
36 | System.Reflection.FieldInfo fi_public_Name;
37 | System.Reflection.FieldInfo fi_static_Name;
38 | System.Reflection.FieldInfo fi_private_Name;
39 | System.Reflection.FieldInfo fi_readonly_Name;
40 | Bar bar;
41 | System.Reflection.FieldInfo fi_public_Size;
42 | System.Reflection.FieldInfo fi_static_Size;
43 | System.Reflection.FieldInfo fi_private_Size;
44 | System.Reflection.FieldInfo fi_readonly_Size;
45 | int fldKey_private_Name_Field;
46 | int fldKey_static_Name_Field;
47 | int fldKey_private_Size_Field;
48 | int fldKey_static_Size_Field;
49 | }
50 | }
--------------------------------------------------------------------------------
/FastAccessors/FastAccessors.NET.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}
4 | False
5 | Library
6 | net472;net6.0
7 | FastAccessors
8 | FastAccessors
9 | false
10 | False
11 | False
12 | 4
13 | true
14 | True
15 | Fast Accessors Library for .NET
16 | Dmitry Garavsky
17 |
18 | Fast Accessors Library for .NET
19 | Allows you to access any fields of your .NET classes as fast as you want.
20 |
21 | Dmitry Garavsky, ©2022
22 | https://github.com/DmitryGaravsky/FastAccessors
23 | https://github.com/DmitryGaravsky/FastAccessors
24 | NET;Reflection;Accessor;Field
25 | LICENSE.TXT
26 | README.MD
27 | ..\bin
28 | 1.0.0.1
29 |
30 | v1.0.0.1:
31 | - multitarget nuget package for .NET6/.Net 4.7.2
32 |
33 |
34 |
35 |
36 | ..\bin.NetFW\
37 |
38 |
39 | ..\bin.NET\
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | True
52 | \
53 |
54 |
55 | True
56 | \
57 |
58 |
59 |
--------------------------------------------------------------------------------
/FastAccessors/FastAccessors.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {2FDE2DC0-F1DC-4090-84CD-58C0204CC63D}
8 | Library
9 | Properties
10 | FastAccessors
11 | FastAccessors
12 | v4.5.2
13 | 512
14 |
15 |
16 |
17 |
18 | true
19 | full
20 | false
21 | ..\bin\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 | true
26 | false
27 |
28 |
29 | pdbonly
30 | true
31 | ..\bin\
32 | TRACE
33 | prompt
34 | 4
35 | true
36 | false
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
58 |
--------------------------------------------------------------------------------
/FastAccessors/Monads/Accessor.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors.Monads {
2 | using System;
3 | using System.Diagnostics;
4 |
5 | public static partial class @Accessor {
6 | /// @Accessor(object): Get Field Value
7 | [DebuggerStepThrough]
8 | public static object @ƒ(this object instance, Type type, string fieldName) {
9 | return FieldAccessor.GetFieldValue(instance, type, fieldName);
10 | }
11 | /// @Accessor(object): Get Field Value
12 | [DebuggerStepThrough]
13 | public static object @ƒ(this object instance, int key) {
14 | return FieldAccessor.GetFieldValue(instance, key);
15 | }
16 | /// @Accessor(Type): Get Static Field Value
17 | [DebuggerStepThrough]
18 | public static object @ƒs(this Type type, string fieldName) {
19 | return FieldAccessorStatic.GetFieldValue(type, fieldName);
20 | }
21 | /// @Accessor(Type): Get Static Field Value
22 | [DebuggerStepThrough]
23 | public static object @ƒs(int key) {
24 | return FieldAccessorStatic.GetFieldValue(key);
25 | }
26 | /// @Accessor(T): Get Default Static Field Value
27 | [DebuggerStepThrough]
28 | public static object @ƒsDefault() {
29 | return FieldAccessorStatic.GetDefaultFieldValue();
30 | }
31 | /// @Accessor(T): Get Field Value
32 | [DebuggerStepThrough]
33 | public static object @ƒ(this T instance, string fieldName) {
34 | return FieldAccessor.GetFieldValue(instance, fieldName);
35 | }
36 | /// @Accessor(T): Get Field Value
37 | [DebuggerStepThrough]
38 | public static object @ƒ(this T instance, int fieldKey) {
39 | return FieldAccessor.GetFieldValue(instance, fieldKey);
40 | }
41 | /// @Accessor(T): Get Default Field Value
42 | [DebuggerStepThrough]
43 | public static object ƒDefault(this T instance) {
44 | return FieldAccessor.GetDefaultFieldValue(instance);
45 | }
46 | //
47 | [DebuggerStepThrough]
48 | public static int @ƒRegister(this string fieldName, Type type) {
49 | return FieldAccessor.RegisterField(type, fieldName);
50 | }
51 | [DebuggerStepThrough]
52 | public static int @ƒsRegister(this string fieldName, Type type, bool defaultField = false) {
53 | return FieldAccessorStatic.RegisterField(type, fieldName, defaultField);
54 | }
55 | [DebuggerStepThrough]
56 | public static int @ƒRegister(this string fieldName, bool defaultField = false) {
57 | return FieldAccessor.RegisterField(fieldName, defaultField);
58 | }
59 | }
60 | }
--------------------------------------------------------------------------------
/FastAccessors/API/Field.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors {
2 | using System;
3 | using System.Runtime.CompilerServices;
4 |
5 | public static class Field {
6 | #region Instance-level
7 | public static int Register(string fieldName, Type type) {
8 | return FieldAccessor.RegisterField(type, fieldName);
9 | }
10 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
11 | public static object GetValue(object instance, Type type, string fieldName) {
12 | return FieldAccessor.GetFieldValue(instance, type, fieldName);
13 | }
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static object GetValue(object instance, int key) {
16 | return FieldAccessor.GetFieldValue(instance, key);
17 | }
18 | #endregion
19 | #region Type-level
20 | public static int RegisterStatic(string fieldName, Type type) {
21 | return FieldAccessorStatic.RegisterField(type, fieldName, false);
22 | }
23 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
24 | public static object GetStaticValue(Type type, string fieldName) {
25 | return FieldAccessorStatic.GetFieldValue(type, fieldName);
26 | }
27 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
28 | public static object GetStaticValue(int key) {
29 | return FieldAccessorStatic.GetFieldValue(key);
30 | }
31 | public static int RegisterDefaultStatic(string fieldName, Type type) {
32 | return FieldAccessorStatic.RegisterField(type, fieldName, true);
33 | }
34 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
35 | public static object GetDefaultStaticValue() {
36 | return FieldAccessorStatic.GetDefaultFieldValue();
37 | }
38 | #endregion
39 | #region Instance-level(GenericWay)
40 | public static int Register(string fieldName) {
41 | return FieldAccessor.RegisterField(fieldName, false);
42 | }
43 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
44 | public static object GetValue(this T instance, string fieldName) {
45 | return FieldAccessor.GetFieldValue(instance, fieldName);
46 | }
47 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
48 | public static object GetValue(this T instance, int fieldKey) {
49 | return FieldAccessor.GetFieldValue(instance, fieldKey);
50 | }
51 | public static int RegisterDefault(string fieldName) {
52 | return FieldAccessor.RegisterField(fieldName, true);
53 | }
54 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
55 | public static object GetDefaultValue(this T instance) {
56 | return FieldAccessor.GetDefaultFieldValue(instance);
57 | }
58 | #endregion
59 | }
60 | }
--------------------------------------------------------------------------------
/FastAccessors.Tests/FastAccessors.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {268CA85D-0867-4571-8BA9-3F8FAC05896D}
8 | Library
9 | Properties
10 | FastAccessors.Tests
11 | FastAccessors.Tests
12 | v4.5.2
13 | 512
14 |
15 |
16 |
17 | true
18 | full
19 | false
20 | ..\bin\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 | true
25 | false
26 |
27 |
28 | pdbonly
29 | true
30 | ..\bin\
31 | TRACE
32 | prompt
33 | 4
34 | true
35 | false
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | {2fde2dc0-f1dc-4090-84cd-58c0204cc63d}
51 | FastAccessors
52 |
53 |
54 |
55 |
62 |
--------------------------------------------------------------------------------
/FastAccessors.Tests/Field.Monads.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors.Monads.Tests {
2 | using System;
3 | using FastAccessors.Tests;
4 | using NUnit.Framework;
5 |
6 | [TestFixture]
7 | public class FieldAccessor_Monads_Tests {
8 | #region SetUp
9 | object obj;
10 | Foo instance;
11 | Type instanceType;
12 | static int fld_private_Name_T, fld_private_Name, fld_static_Name;
13 | [SetUp]
14 | public void SetUp() {
15 | obj = instance = new Foo("Foo");
16 | instanceType = typeof(Foo);
17 | fld_private_Name = "private_Name".ƒRegister(instanceType);
18 | fld_static_Name = "static_Name".ƒsRegister(instanceType, true);
19 | fld_private_Name_T = "private_Name".ƒRegister(true);
20 | }
21 | #endregion
22 | [Test(Description = "1.1 DynamicMethod(Instance)")]
23 | public void Accessor01_DynamicMethod() {
24 | Assert.AreEqual("Foo(Private)", obj.@ƒ(instanceType, "private_Name"));
25 | }
26 | [Test(Description = "1.2 DynamicMethod(Public)")]
27 | public void Accessor01_DynamicMethod_Public() {
28 | Assert.AreEqual("Foo(Public)", obj.@ƒ(instanceType, "public_Name"));
29 | }
30 | [Test(Description = "1.3 DynamicMethod(Readonly)")]
31 | public void Accessor01_DynamicMethod_Readonly() {
32 | Assert.AreEqual("Foo(Readonly)", obj.@ƒ(instanceType, "readonly_Name"));
33 | }
34 | [Test(Description = "1.4 DynamicMethod(Static)")]
35 | public void Accessor01_DynamicMethod_Static() {
36 | Assert.AreEqual("Foo(Static)", instanceType.@ƒs("static_Name"));
37 | }
38 | [Test(Description = "1.5 DynamicMethod(Generic)")]
39 | public void Accessor01_DynamicMethod_Generic() {
40 | Assert.AreEqual("Foo(Private)", instance.@ƒ("private_Name"));
41 | }
42 | [Test(Description = "2.1 DynamicMethod(Instance,Key)")]
43 | public void Accessor02_DynamicMethod_Key() {
44 | Assert.AreEqual("Foo(Private)", obj.@ƒ(fld_private_Name));
45 | }
46 | [Test(Description = "2.2 DynamicMethod(Static,Key)")]
47 | public void Accessor02_DynamicMethod_Static_Key() {
48 | Assert.AreEqual("Foo(Static)", Accessor.@ƒs(fld_static_Name));
49 | }
50 | [Test(Description = "2.3 DynamicMethod(Generic,Key)")]
51 | public void Accessor02_DynamicMethod_Generic_Key() {
52 | Assert.AreEqual("Foo(Private)", instance.@ƒ(fld_private_Name_T));
53 | }
54 | [Test(Description = "3.1 DynamicMethod(Static,DefaultField)")]
55 | public void Accessor02_DynamicMethod_Static_DefaultField() {
56 | Assert.AreEqual("Foo(Static)", Accessor.@ƒsDefault());
57 | }
58 | [Test(Description = "3.2 DynamicMethod(Generic,DefaultField)")]
59 | public void Accessor02_DynamicMethod_Generic_DefaultField() {
60 | Assert.AreEqual("Foo(Private)", instance.@ƒDefault());
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/FastAccessors.Benchmarks/FastAccessors.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors.Benchmarks {
2 | using BenchmarkDotNet.Attributes;
3 |
4 | partial class Benchmark {
5 | [Benchmark(Description = "2.1 FastAccessors(Instance)")]
6 | public int FastAccessors_Instance() {
7 | var name = (string)Field.GetValue(foo, typeof(Foo), "private_Name");
8 | var size = (int)Field.GetValue(bar, typeof(Bar), "private_Size");
9 | return name.Length + size;
10 | }
11 | [Benchmark(Description = "2.2. FastAccessors(Static)")]
12 | public int FastAccessors_Static() {
13 | var name = (string)Field.GetStaticValue(typeof(Foo), "static_Name");
14 | var size = (int)Field.GetStaticValue(typeof(Bar), "static_Size");
15 | return name.Length + size;
16 | }
17 | [Benchmark(Description = "2.3. FastAccessors(Public)")]
18 | public int FastAccessors_Public() {
19 | var name = (string)Field.GetValue(foo, typeof(Foo), "public_Name");
20 | var size = (int)Field.GetValue(bar, typeof(Bar), "public_Size");
21 | return name.Length + size;
22 | }
23 | [Benchmark(Description = "2.4. FastAccessors(Readonly)")]
24 | public int FastAccessors_Readonly() {
25 | var name = (string)Field.GetValue(foo, typeof(Foo), "readonly_Name");
26 | var size = (int)Field.GetValue(bar, typeof(Bar), "readonly_Size");
27 | return name.Length + size;
28 | }
29 | [Benchmark(Description = "2.5. FastAccessors(Generic)")]
30 | public int FastAccessors_Generic() {
31 | var name = (string)Field.GetValue(foo, "private_Name");
32 | var size = (int)Field.GetValue(bar, "private_Size");
33 | return name.Length + size;
34 | }
35 | //
36 | [Benchmark(Description = "3.1 FastAccessors(Instance,Key)")]
37 | public int FastAccessors_Instance_Key() {
38 | var name = (string)Field.GetValue((object)foo, fldKey_private_Name_Field);
39 | var size = (int)Field.GetValue((object)bar, fldKey_private_Size_Field);
40 | return name.Length + size;
41 | }
42 | [Benchmark(Description = "3.2 FastAccessors(Static,Key)")]
43 | public int FastAccessors_Static_Key() {
44 | var name = (string)Field.GetStaticValue(fldKey_static_Name_Field);
45 | var size = (int)Field.GetStaticValue(fldKey_static_Size_Field);
46 | return name.Length + size;
47 | }
48 | [Benchmark(Description = "3.3 FastAccessors(Generic,Key)")]
49 | public int FastAccessors_Generic_Key() {
50 | var name = (string)Field.GetValue(foo, fldKey_private_Name_Field);
51 | var size = (int)Field.GetValue(bar, fldKey_private_Size_Field);
52 | return name.Length + size;
53 | }
54 | //
55 | [Benchmark(Description = "4.1 FastAccessors(Generic,DefaultField)")]
56 | public int FastAccessors_Generic_DefaultField() {
57 | var name = (string)Field.GetDefaultValue(foo);
58 | var size = (int)Field.GetDefaultValue(bar);
59 | return name.Length + size;
60 | }
61 | [Benchmark(Description = "4.2 FastAccessors(Static,DefaultField)")]
62 | public int FastAccessors_Static_DefaultField() {
63 | var name1 = (string)Field.GetDefaultStaticValue();
64 | var name2 = (string)Field.GetDefaultStaticValue();
65 | return name1.Length + name2.Length;
66 | }
67 | }
68 | }
--------------------------------------------------------------------------------
/FastAccessors.Benchmarks/Reflection.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors.Benchmarks {
2 | using BenchmarkDotNet.Attributes;
3 | using BF = System.Reflection.BindingFlags;
4 |
5 | partial class Benchmark {
6 | [Benchmark(Description = "Direct Access", Baseline = true)]
7 | public int DirectAccess() {
8 | return foo.public_Name.Length + bar.public_Size;
9 | }
10 | [Benchmark(Description = "Direct Access(Static)")]
11 | public int DirectAccess_Static() {
12 | return Foo.GetName().Length + Bar.GetSize();
13 | }
14 | //
15 | [Benchmark(Description = "1.1. Reflection(Instance)")]
16 | public int Reflection_Instance() {
17 | var fi_pname = typeof(Foo).GetField("private_Name", BF.NonPublic | BF.Instance);
18 | var name = (string)fi_pname.GetValue(foo);
19 | var fi_psize = typeof(Foo).GetField("private_Size", BF.NonPublic | BF.Instance);
20 | var size = (int)fi_psize.GetValue(bar);
21 | return name.Length + size;
22 | }
23 | [Benchmark(Description = "1.2. Reflection(Static)")]
24 | public int Reflection_Static() {
25 | var fi_psname = typeof(Foo).GetField("static_Name", BF.NonPublic | BF.Static);
26 | var name = (string)fi_psname.GetValue(foo);
27 | var fi_pssize = typeof(Foo).GetField("static_Size", BF.NonPublic | BF.Static);
28 | var size = (int)fi_pssize.GetValue(bar);
29 | return name.Length + size;
30 | }
31 | [Benchmark(Description = "1.3. Reflection(Public)")]
32 | public int Reflection_Public() {
33 | var fi_pname = typeof(Foo).GetField("public_Name", BF.Public | BF.Instance);
34 | var name = (string)fi_pname.GetValue(foo);
35 | var fi_psize = typeof(Foo).GetField("public_Size", BF.Public | BF.Instance);
36 | var size = (int)fi_psize.GetValue(bar);
37 | return name.Length + size;
38 | }
39 | [Benchmark(Description = "1.4. Reflection(Readonly)")]
40 | public int Reflection_Readonly() {
41 | var fi_prname = typeof(Foo).GetField("readonly_Name", BF.NonPublic | BF.Instance);
42 | var name = (string)fi_prname.GetValue(foo);
43 | var fi_prsize = typeof(Foo).GetField("readonly_Size", BF.NonPublic | BF.Instance);
44 | var size = (int)fi_prsize.GetValue(bar);
45 | return name.Length + size;
46 | }
47 | //
48 | [Benchmark(Description = "1.1. Reflection(Instance, Cached)")]
49 | public int Reflection_Instance_Cached() {
50 | var name = (string)fi_private_Name.GetValue(foo);
51 | var size = (int)fi_private_Size.GetValue(bar);
52 | return name.Length + size;
53 | }
54 | [Benchmark(Description = "1.2. Reflection(Static, Cached)")]
55 | public int Reflection_Static_Cached() {
56 | var name = (string)fi_static_Name.GetValue(foo);
57 | var size = (int)fi_static_Size.GetValue(bar);
58 | return name.Length + size;
59 | }
60 | [Benchmark(Description = "1.3. Reflection(Public, Cached)")]
61 | public int Reflection_Public_Cached() {
62 | var name = (string)fi_public_Name.GetValue(foo);
63 | var size = (int)fi_public_Size.GetValue(bar);
64 | return name.Length + size;
65 | }
66 | [Benchmark(Description = "1.4. Reflection(Readonly, Cached)")]
67 | public int Reflection_Readonly_Cached() {
68 | var name = (string)fi_readonly_Name.GetValue(foo);
69 | var size = (int)fi_readonly_Size.GetValue(bar);
70 | return name.Length + size;
71 | }
72 | }
73 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FastAccessors
2 | Fast Accessors Library for .NET.
3 | Allows you to access any fields of your .NET classes as fast as you want.
4 |
5 |
6 |
7 | ## Usage
8 |
9 | Let the `Foo` class is defined as follows:
10 | ```cs
11 | class Foo {
12 | string private_Name;
13 | int private_Field;
14 | static int static_Field;
15 | }
16 | ```
17 | If you want to get access to it's fields you can use the "by-name" accessors:
18 |
19 | ```cs
20 | using FastAccessors;
21 | //...
22 | Foo foo = new Foo()
23 | // access to the specific private field by field-name
24 | string _name = foo.@ƒ("private_Name") as string;
25 | ```
26 | It's quite fast. But, if you want to be faster, you can use "field-keys":
27 |
28 | ```cs
29 | using FastAccessors;
30 | // Field-Keys initialization
31 | int fldKey_private_Field = "private_Field".@ƒRegister(typeof(Foo));
32 | int fldKey_static_Field = "static_Field".@ƒsRegister(typeof(Foo));
33 | //...
34 | Foo foo = new Foo()
35 | // access to the specific private field by field-key
36 | int _field = (int)foo.@ƒ(fldKey_private_Field);
37 | // access to the specific static field by field-key
38 | int _s_field = (int)typeof(Foo).@ƒs(fldKey_Static_Field);
39 | ```
40 | To be **fastest** use the "default-fields":
41 |
42 | ```cs
43 | using FastAccessors;
44 | // default-fields initialization
45 | "private_Name".@ƒRegister(typeof(Foo), true); // default instance-field
46 | "static_Field".@ƒsRegister(typeof(Foo), true); // default static field
47 | //...
48 | Foo foo = new Foo()
49 | // access to the default-fields
50 | string _name = foo.@ƒDefault() as string;
51 | int _s_Field = (int)foo.@ƒsDefault();
52 | ```
53 |
54 | ### NuGet
55 |
56 | To install [FastAccessors](https://www.nuget.org/packages/FastAccessors), run the following command in the Package Manager Console:
57 |
58 | Install-Package FastAccessors
59 |
60 |
61 | ### License
62 |
63 | The FastAccessors library is licensed under the [MIT](https://github.com/DmitryGaravsky/FastAccessors/blob/master/LICENSE.TXT) license.
64 |
65 |
66 | ### Benchmarks
67 |
68 | ```ini
69 | BenchmarkDotNet=v0.9.4.0
70 | OS=Microsoft Windows NT 6.2.9200.0
71 | Processor=Intel(R) Core(TM) i7-4702HQ CPU @ 2.20GHz, ProcessorCount=8
72 | Frequency=2143485 ticks, Resolution=466.5300 ns, Timer=TSC
73 | HostCLR=MS.NET 4.0.30319.42000, Arch=64-bit RELEASE [RyuJIT]
74 | JitModules=clrjit-v4.6.1055.0
75 |
76 | Type=Benchmarks Mode=Throughput
77 |
78 | Method | Median | StdDev |
79 | ---------------------------------------- |------------ |---------- |
80 | 1.1. Reflection(Instance) | 171.1189 ns | 4.6809 ns |
81 | 1.2. Reflection(Static) | 162.7281 ns | 4.2497 ns |
82 | 1.3. Reflection(Public) | 171.0062 ns | 2.4226 ns |
83 | 1.4. Reflection(Readonly) | 169.5342 ns | 4.0005 ns |
84 | 2.1 FastAccessors(Instance) | 89.8821 ns | 1.4286 ns |
85 | 2.2 FastAccessors(Static) | 87.9811 ns | 0.9167 ns |
86 | 2.3 FastAccessors(Public) | 88.2568 ns | 0.7475 ns |
87 | 2.4 FastAccessors(Readonly) | 91.0912 ns | 0.9119 ns |
88 | 2.5 FastAccessors(Generic) | 52.1873 ns | 1.1102 ns |
89 | 3.1 FastAccessors(Instance,Key) | 18.1572 ns | 0.1741 ns |
90 | 3.2 FastAccessors(Static,Key) | 16.7747 ns | 0.1925 ns |
91 | 3.3 FastAccessors(Generic,Key) | 32.3943 ns | 0.2694 ns |
92 | 4.1 FastAccessors(Generic,DefaultField) | 6.8535 ns | 0.1283 ns |
93 | 4.2 FastAccessors(Static,DefaultField) | 3.6290 ns | 0.0805 ns |
94 | ```
95 |
--------------------------------------------------------------------------------
/FastAccessors.Tests/Field.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors.Tests {
2 | using System;
3 | using NUnit.Framework;
4 |
5 | [TestFixture]
6 | public class FieldAccessor_Tests_Ref {
7 | #region SetUp
8 | object obj;
9 | Foo instance;
10 | Type instanceType;
11 | static int fld_private_Name_T, fld_private_Name, fld_static_Name;
12 | [SetUp]
13 | public void SetUp() {
14 | obj = instance = new Foo("Foo");
15 | instanceType = typeof(Foo);
16 | fld_static_Name = Field.RegisterDefaultStatic("static_Name", instanceType);
17 | fld_private_Name = Field.Register("private_Name", instanceType);
18 | fld_private_Name_T = Field.RegisterDefault("private_Name");
19 | }
20 | #endregion
21 | [Test(Description = "1.1 DynamicMethod(Instance)")]
22 | public void Accessor01_DynamicMethod() {
23 | Assert.AreEqual("Foo(Private)", Field.GetValue(obj, instanceType, "private_Name"));
24 | }
25 | [Test(Description = "1.2 DynamicMethod(Public)")]
26 | public void Accessor01_DynamicMethod_Public() {
27 | Assert.AreEqual("Foo(Public)", Field.GetValue(obj, instanceType, "public_Name"));
28 | }
29 | [Test(Description = "1.3 DynamicMethod(Readonly)")]
30 | public void Accessor01_DynamicMethod_Readonly() {
31 | Assert.AreEqual("Foo(Readonly)", Field.GetValue(obj, instanceType, "readonly_Name"));
32 | }
33 | [Test(Description = "1.4 DynamicMethod(Static)")]
34 | public void Accessor01_DynamicMethod_Static() {
35 | Assert.AreEqual("Foo(Static)", Field.GetStaticValue(instanceType, "static_Name"));
36 | }
37 | [Test(Description = "1.5 DynamicMethod(Generic)")]
38 | public void Accessor01_DynamicMethod_Generic() {
39 | Assert.AreEqual("Foo(Private)", Field.GetValue(instance, "private_Name"));
40 | }
41 | [Test(Description = "2.1 DynamicMethod(Instance,Key)")]
42 | public void Accessor02_DynamicMethod_Key() {
43 | Assert.AreEqual("Foo(Private)", Field.GetValue(obj, fld_private_Name));
44 | }
45 | [Test(Description = "2.2 DynamicMethod(Static,Key)")]
46 | public void Accessor02_DynamicMethod_Static_Key() {
47 | Assert.AreEqual("Foo(Static)", Field.GetStaticValue(fld_static_Name));
48 | }
49 | [Test(Description = "2.3 DynamicMethod(Generic,Key)")]
50 | public void Accessor02_DynamicMethod_Generic_Key() {
51 | Assert.AreEqual("Foo(Private)", Field.GetValue(instance, fld_private_Name_T));
52 | }
53 | [Test(Description = "3.1 DynamicMethod(Static,DefaultField)")]
54 | public void Accessor02_DynamicMethod_Static_DefaultField() {
55 | Assert.AreEqual("Foo(Static)", Field.GetDefaultStaticValue());
56 | }
57 | [Test(Description = "3.2 DynamicMethod(Generic,DefaultField)")]
58 | public void Accessor02_DynamicMethod_Generic_DefaultField() {
59 | Assert.AreEqual("Foo(Private)", Field.GetDefaultValue(instance));
60 | }
61 | }
62 |
63 | [TestFixture]
64 | public class FieldAccessor_Tests_Val {
65 | #region SetUp
66 | object obj;
67 | Bar instance;
68 | Type instanceType;
69 | static int fld_private_Size_T, fld_private_Size, fld_static_Size;
70 | [SetUp]
71 | public void SetUp() {
72 | obj = instance = new Bar("Bar".Length);
73 | instanceType = typeof(Bar);
74 | fld_static_Size = Field.RegisterDefaultStatic("static_Size", instanceType);
75 | fld_private_Size = Field.Register("private_Size", instanceType);
76 | fld_private_Size_T = Field.RegisterDefault("private_Size");
77 | }
78 | #endregion
79 | [Test(Description = "1.1 DynamicMethod(Instance)")]
80 | public void Accessor01_DynamicMethod() {
81 | Assert.AreEqual("Bar(Private)".Length, Field.GetValue(obj, instanceType, "private_Size"));
82 | }
83 | [Test(Description = "1.2 DynamicMethod(Public)")]
84 | public void Accessor01_DynamicMethod_Public() {
85 | Assert.AreEqual("Bar(Public)".Length, Field.GetValue(obj, instanceType, "public_Size"));
86 | }
87 | [Test(Description = "1.3 DynamicMethod(Readonly)")]
88 | public void Accessor01_DynamicMethod_Readonly() {
89 | Assert.AreEqual("Bar(Readonly)".Length, Field.GetValue(obj, instanceType, "readonly_Size"));
90 | }
91 | [Test(Description = "1.4 DynamicMethod(Static)")]
92 | public void Accessor01_DynamicMethod_Static() {
93 | Assert.AreEqual("Bar(Static)".Length, Field.GetStaticValue(instanceType, "static_Size"));
94 | }
95 | [Test(Description = "1.5 DynamicMethod(Generic)")]
96 | public void Accessor01_DynamicMethod_Generic() {
97 | Assert.AreEqual("Bar(Private)".Length, Field.GetValue(instance, "private_Size"));
98 | }
99 | [Test(Description = "2.1 DynamicMethod(Instance,Key)")]
100 | public void Accessor02_DynamicMethod_Key() {
101 | Assert.AreEqual("Bar(Private)".Length, Field.GetValue(obj, fld_private_Size));
102 | }
103 | [Test(Description = "2.2 DynamicMethod(Static,Key)")]
104 | public void Accessor02_DynamicMethod_Static_Key() {
105 | Assert.AreEqual("Bar(Static)".Length, Field.GetStaticValue(fld_static_Size));
106 | }
107 | [Test(Description = "2.3 DynamicMethod(Generic,Key)")]
108 | public void Accessor02_DynamicMethod_Generic_Key() {
109 | Assert.AreEqual("Bar(Private)".Length, Field.GetValue(instance, fld_private_Size_T));
110 | }
111 | [Test(Description = "3.1 DynamicMethod(Static,DefaultField)")]
112 | public void Accessor02_DynamicMethod_Static_DefaultField() {
113 | Assert.AreEqual("Bar(Static)".Length, Field.GetDefaultStaticValue());
114 | }
115 | [Test(Description = "3.2 DynamicMethod(Generic,DefaultField)")]
116 | public void Accessor02_DynamicMethod_Generic_DefaultField() {
117 | Assert.AreEqual("Bar(Private)".Length, Field.GetDefaultValue(instance));
118 | }
119 | }
120 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 | [Bb]in/
25 | [Bb]in.NET/
26 | [Bb]in.NetFW/
27 |
28 | # Visual Studio 2015 cache/options directory
29 | .vs/
30 | # Uncomment if you have tasks that create the project's static files in wwwroot
31 | #wwwroot/
32 |
33 | # MSTest test Results
34 | [Tt]est[Rr]esult*/
35 | [Bb]uild[Ll]og.*
36 |
37 | # NUNIT
38 | *.VisualState.xml
39 | TestResult.xml
40 |
41 | # Build Results of an ATL Project
42 | [Dd]ebugPS/
43 | [Rr]eleasePS/
44 | dlldata.c
45 |
46 | # DNX
47 | project.lock.json
48 | artifacts/
49 |
50 | *_i.c
51 | *_p.c
52 | *_i.h
53 | *.ilk
54 | *.meta
55 | *.obj
56 | *.pch
57 | *.pdb
58 | *.pgc
59 | *.pgd
60 | *.rsp
61 | *.sbr
62 | *.tlb
63 | *.tli
64 | *.tlh
65 | *.tmp
66 | *.tmp_proj
67 | *.log
68 | *.vspscc
69 | *.vssscc
70 | .builds
71 | *.pidb
72 | *.svclog
73 | *.scc
74 |
75 | # Chutzpah Test files
76 | _Chutzpah*
77 |
78 | # Visual C++ cache files
79 | ipch/
80 | *.aps
81 | *.ncb
82 | *.opendb
83 | *.opensdf
84 | *.sdf
85 | *.cachefile
86 | *.VC.db
87 | *.VC.VC.opendb
88 |
89 | # Visual Studio profiler
90 | *.psess
91 | *.vsp
92 | *.vspx
93 | *.sap
94 |
95 | # TFS 2012 Local Workspace
96 | $tf/
97 |
98 | # Guidance Automation Toolkit
99 | *.gpState
100 |
101 | # ReSharper is a .NET coding add-in
102 | _ReSharper*/
103 | *.[Rr]e[Ss]harper
104 | *.DotSettings.user
105 |
106 | # JustCode is a .NET coding add-in
107 | .JustCode
108 |
109 | # TeamCity is a build add-in
110 | _TeamCity*
111 |
112 | # DotCover is a Code Coverage Tool
113 | *.dotCover
114 |
115 | # NCrunch
116 | _NCrunch_*
117 | .*crunch*.local.xml
118 | nCrunchTemp_*
119 |
120 | # MightyMoose
121 | *.mm.*
122 | AutoTest.Net/
123 |
124 | # Web workbench (sass)
125 | .sass-cache/
126 |
127 | # Installshield output folder
128 | [Ee]xpress/
129 |
130 | # DocProject is a documentation generator add-in
131 | DocProject/buildhelp/
132 | DocProject/Help/*.HxT
133 | DocProject/Help/*.HxC
134 | DocProject/Help/*.hhc
135 | DocProject/Help/*.hhk
136 | DocProject/Help/*.hhp
137 | DocProject/Help/Html2
138 | DocProject/Help/html
139 |
140 | # Click-Once directory
141 | publish/
142 |
143 | # Publish Web Output
144 | *.[Pp]ublish.xml
145 | *.azurePubxml
146 | # TODO: Comment the next line if you want to checkin your web deploy settings
147 | # but database connection strings (with potential passwords) will be unencrypted
148 | *.pubxml
149 | *.publishproj
150 |
151 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
152 | # checkin your Azure Web App publish settings, but sensitive information contained
153 | # in these scripts will be unencrypted
154 | PublishScripts/
155 |
156 | # NuGet Packages
157 | *.nupkg
158 | # The packages folder can be ignored because of Package Restore
159 | **/packages/*
160 | # except build/, which is used as an MSBuild target.
161 | !**/packages/build/
162 | # Uncomment if necessary however generally it will be regenerated when needed
163 | #!**/packages/repositories.config
164 | # NuGet v3's project.json files produces more ignoreable files
165 | *.nuget.props
166 | *.nuget.targets
167 |
168 | # Microsoft Azure Build Output
169 | csx/
170 | *.build.csdef
171 |
172 | # Microsoft Azure Emulator
173 | ecf/
174 | rcf/
175 |
176 | # Windows Store app package directories and files
177 | AppPackages/
178 | BundleArtifacts/
179 | Package.StoreAssociation.xml
180 | _pkginfo.txt
181 |
182 | # Visual Studio cache files
183 | # files ending in .cache can be ignored
184 | *.[Cc]ache
185 | # but keep track of directories ending in .cache
186 | !*.[Cc]ache/
187 |
188 | # Others
189 | ClientBin/
190 | ~$*
191 | *~
192 | *.dbmdl
193 | *.dbproj.schemaview
194 | *.pfx
195 | *.publishsettings
196 | node_modules/
197 | orleans.codegen.cs
198 |
199 | # Since there are multiple workflows, uncomment next line to ignore bower_components
200 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
201 | #bower_components/
202 |
203 | # RIA/Silverlight projects
204 | Generated_Code/
205 |
206 | # Backup & report files from converting an old project file
207 | # to a newer Visual Studio version. Backup files are not needed,
208 | # because we have git ;-)
209 | _UpgradeReport_Files/
210 | Backup*/
211 | UpgradeLog*.XML
212 | UpgradeLog*.htm
213 |
214 | # SQL Server files
215 | *.mdf
216 | *.ldf
217 |
218 | # Business Intelligence projects
219 | *.rdl.data
220 | *.bim.layout
221 | *.bim_*.settings
222 |
223 | # Microsoft Fakes
224 | FakesAssemblies/
225 |
226 | # GhostDoc plugin setting file
227 | *.GhostDoc.xml
228 |
229 | # Node.js Tools for Visual Studio
230 | .ntvs_analysis.dat
231 |
232 | # Visual Studio 6 build log
233 | *.plg
234 |
235 | # Visual Studio 6 workspace options file
236 | *.opt
237 |
238 | # Visual Studio LightSwitch build output
239 | **/*.HTMLClient/GeneratedArtifacts
240 | **/*.DesktopClient/GeneratedArtifacts
241 | **/*.DesktopClient/ModelManifest.xml
242 | **/*.Server/GeneratedArtifacts
243 | **/*.Server/ModelManifest.xml
244 | _Pvt_Extensions
245 |
246 | # Paket dependency manager
247 | .paket/paket.exe
248 | paket-files/
249 |
250 | # FAKE - F# Make
251 | .fake/
252 |
253 | # JetBrains Rider
254 | .idea/
255 | *.sln.iml
256 |
257 | # =========================
258 | # Operating System Files
259 | # =========================
260 |
261 | # OSX
262 | # =========================
263 |
264 | .DS_Store
265 | .AppleDouble
266 | .LSOverride
267 |
268 | # Thumbnails
269 | ._*
270 |
271 | # Files that might appear in the root of a volume
272 | .DocumentRevisions-V100
273 | .fseventsd
274 | .Spotlight-V100
275 | .TemporaryItems
276 | .Trashes
277 | .VolumeIcon.icns
278 |
279 | # Directories potentially created on remote AFP share
280 | .AppleDB
281 | .AppleDesktop
282 | Network Trash Folder
283 | Temporary Items
284 | .apdisk
285 |
286 | # Windows
287 | # =========================
288 |
289 | # Windows image file caches
290 | Thumbs.db
291 | ehthumbs.db
292 |
293 | # Folder config file
294 | Desktop.ini
295 |
296 | # Recycle Bin used on file shares
297 | $RECYCLE.BIN/
298 |
299 | # Windows Installer files
300 | *.cab
301 | *.msi
302 | *.msm
303 | *.msp
304 |
305 | # Windows shortcuts
306 | *.lnk
307 |
--------------------------------------------------------------------------------
/FastAccessors/Accessors/Field.cs:
--------------------------------------------------------------------------------
1 | namespace FastAccessors {
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Reflection;
5 | using System.Reflection.Emit;
6 | using BF = System.Reflection.BindingFlags;
7 | using MA = System.Reflection.MethodAttributes;
8 |
9 | static class FieldAccessor {
10 | readonly static Dictionary> accessors = new Dictionary>(StringComparer.Ordinal);
11 | readonly static Func