Skip to content

Commit fe6f12f

Browse files
authored
CSHARP-5797: Add C# 14 testing (#1834)
1 parent 4c9b66e commit fe6f12f

File tree

24 files changed

+364
-616
lines changed

24 files changed

+364
-616
lines changed

CSharpDriver.sln

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,72 +64,105 @@ Global
6464
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6565
Debug|Any CPU = Debug|Any CPU
6666
Release|Any CPU = Release|Any CPU
67+
Release_CSharp_14|Any CPU = Release_CSharp_14|Any CPU
6768
EndGlobalSection
6869
GlobalSection(ProjectConfigurationPlatforms) = postSolution
6970
{9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7071
{9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
7172
{9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
7273
{9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
75+
{9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
7376
{3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7477
{3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
7578
{3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
7679
{3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Release|Any CPU.Build.0 = Release|Any CPU
80+
{3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
81+
{3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
7782
{4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7883
{4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Debug|Any CPU.Build.0 = Debug|Any CPU
7984
{4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Release|Any CPU.ActiveCfg = Release|Any CPU
8085
{4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Release|Any CPU.Build.0 = Release|Any CPU
86+
{4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
87+
{4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
8188
{756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
8289
{756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
8390
{756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
8491
{756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
93+
{756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
8594
{253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
8695
{253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
8796
{253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
8897
{253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Release|Any CPU.Build.0 = Release|Any CPU
98+
{253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
99+
{253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
89100
{FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
90101
{FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Debug|Any CPU.Build.0 = Debug|Any CPU
91102
{FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Release|Any CPU.ActiveCfg = Release|Any CPU
92103
{FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Release|Any CPU.Build.0 = Release|Any CPU
104+
{FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
105+
{FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
93106
{F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
94107
{F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
95108
{F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
96109
{F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Release|Any CPU.Build.0 = Release|Any CPU
110+
{F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
111+
{F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
97112
{C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
98113
{C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Debug|Any CPU.Build.0 = Debug|Any CPU
99114
{C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Release|Any CPU.ActiveCfg = Release|Any CPU
100115
{C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Release|Any CPU.Build.0 = Release|Any CPU
116+
{C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
117+
{C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
101118
{B90F025F-89D3-436A-AD78-6AA304A6E240}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
102119
{B90F025F-89D3-436A-AD78-6AA304A6E240}.Debug|Any CPU.Build.0 = Debug|Any CPU
103120
{B90F025F-89D3-436A-AD78-6AA304A6E240}.Release|Any CPU.ActiveCfg = Release|Any CPU
104121
{B90F025F-89D3-436A-AD78-6AA304A6E240}.Release|Any CPU.Build.0 = Release|Any CPU
122+
{B90F025F-89D3-436A-AD78-6AA304A6E240}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
123+
{B90F025F-89D3-436A-AD78-6AA304A6E240}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
105124
{B711A69F-A337-452C-95E1-A6B15C727CBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
106125
{B711A69F-A337-452C-95E1-A6B15C727CBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
107126
{B711A69F-A337-452C-95E1-A6B15C727CBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
108127
{B711A69F-A337-452C-95E1-A6B15C727CBA}.Release|Any CPU.Build.0 = Release|Any CPU
128+
{B711A69F-A337-452C-95E1-A6B15C727CBA}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
129+
{B711A69F-A337-452C-95E1-A6B15C727CBA}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
109130
{DF888021-744F-4A8B-9324-831DEFC48AB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
110131
{DF888021-744F-4A8B-9324-831DEFC48AB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
111132
{DF888021-744F-4A8B-9324-831DEFC48AB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
112133
{DF888021-744F-4A8B-9324-831DEFC48AB8}.Release|Any CPU.Build.0 = Release|Any CPU
134+
{DF888021-744F-4A8B-9324-831DEFC48AB8}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
135+
{DF888021-744F-4A8B-9324-831DEFC48AB8}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
113136
{CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
114137
{CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Debug|Any CPU.Build.0 = Debug|Any CPU
115138
{CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Release|Any CPU.ActiveCfg = Release|Any CPU
116139
{CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Release|Any CPU.Build.0 = Release|Any CPU
140+
{CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
141+
{CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
117142
{33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
118143
{33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Debug|Any CPU.Build.0 = Debug|Any CPU
119144
{33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Release|Any CPU.ActiveCfg = Release|Any CPU
120145
{33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Release|Any CPU.Build.0 = Release|Any CPU
146+
{33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
147+
{33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
121148
{642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
122149
{642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
123150
{642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
124151
{642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Release|Any CPU.Build.0 = Release|Any CPU
152+
{642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
153+
{642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
125154
{AB4AD0CB-756C-444F-9F37-26DA35148934}.Release|Any CPU.ActiveCfg = Release|Any CPU
126155
{AB4AD0CB-756C-444F-9F37-26DA35148934}.Release|Any CPU.Build.0 = Release|Any CPU
127156
{AB4AD0CB-756C-444F-9F37-26DA35148934}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
128157
{AB4AD0CB-756C-444F-9F37-26DA35148934}.Debug|Any CPU.Build.0 = Debug|Any CPU
158+
{AB4AD0CB-756C-444F-9F37-26DA35148934}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
159+
{AB4AD0CB-756C-444F-9F37-26DA35148934}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
129160
{A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
130161
{A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
131162
{A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
132163
{A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Release|Any CPU.Build.0 = Release|Any CPU
164+
{A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU
165+
{A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU
133166
EndGlobalSection
134167
GlobalSection(SolutionProperties) = preSolution
135168
HideSolutionNode = FALSE

evergreen/compile-sources.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
set -o errexit # Exit the script with error if any of the commands fail
33

44
SOURCE_PROJECT=${1:-CSharpDriver.sln}
5+
CONFIGURATION=${CONFIGURATION:-'Release'}
6+
57
if [ -z "$PACKAGE_VERSION" ]; then
68
PACKAGE_VERSION=$(bash ./evergreen/get-version.sh)
79
echo Calculated PACKAGE_VERSION value: "$PACKAGE_VERSION"
@@ -30,4 +32,4 @@ do
3032
sleep $DELAY
3133
done
3234

33-
dotnet build "${SOURCE_PROJECT}" -c Release --no-restore -p:Version="$PACKAGE_VERSION"
35+
dotnet build "${SOURCE_PROJECT}" -c "${CONFIGURATION}" --no-restore -p:Version="$PACKAGE_VERSION"

evergreen/evergreen.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ functions:
332332
shell: "bash"
333333
include_expansions_in_env:
334334
- "FRAMEWORK"
335+
- "CONFIGURATION"
335336
script: |
336337
${PREPARE_SHELL}
337338
bash ./evergreen/run-unit-tests.sh
@@ -1123,6 +1124,16 @@ tasks:
11231124
FRAMEWORK: net10.0
11241125
- func: run-unit-tests
11251126

1127+
- name: unit-tests-net100-csharp14
1128+
commands:
1129+
- func: configure-framework
1130+
vars:
1131+
FRAMEWORK: net10.0
1132+
DOTNET_SDK_VERSION: 10.0
1133+
- func: run-unit-tests
1134+
vars:
1135+
CONFIGURATION: "Release_CSharp_14"
1136+
11261137
- name: test-net472
11271138
commands:
11281139
- func: setup-csfle-secrets
@@ -2390,6 +2401,7 @@ buildvariants:
23902401
- name: unit-tests-netstandard21
23912402
- name: unit-tests-net60
23922403
- name: unit-tests-net100
2404+
- name: unit-tests-net100-csharp14
23932405

23942406
- name: unit-tests-ubuntu
23952407
display_name: Unit Tests on Ubuntu
@@ -2400,6 +2412,7 @@ buildvariants:
24002412
- name: unit-tests-netstandard21
24012413
- name: unit-tests-net60
24022414
- name: unit-tests-net100
2415+
- name: unit-tests-net100-csharp14
24032416

24042417
- name: unit-tests-macos
24052418
display_name: Unit Tests on MacOs
@@ -2410,6 +2423,7 @@ buildvariants:
24102423
- name: unit-tests-netstandard21
24112424
- name: unit-tests-net60
24122425
- name: unit-tests-net100
2426+
- name: unit-tests-net100-csharp14
24132427

24142428
- name: unit-tests-macos-arm
24152429
display_name: Unit Tests on MacOs Arm
@@ -2419,6 +2433,7 @@ buildvariants:
24192433
tasks:
24202434
- name: unit-tests-net60
24212435
- name: unit-tests-net100
2436+
- name: unit-tests-net100-csharp14
24222437

24232438
- matrix_name: stable-api-tests
24242439
matrix_spec: { version: ["5.0", "6.0", "7.0", "8.0", "rapid", "latest"], topology: ["standalone", "sharded-cluster"], auth: "auth", ssl: "nossl", os: "windows-64" }

evergreen/execute-tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set -o errexit # Exit the script with error if any of the commands fail
1010
FRAMEWORK=${FRAMEWORK:-}
1111
TEST_CATEGORY=${TEST_CATEGORY:-Integration}
1212
TEST_PROJECT_PATH=${TEST_PROJECT_PATH:-./tests/**/[!AtlasConnectivity]*.Tests.csproj}
13+
CONFIGURATION=${CONFIGURATION:-'Release'}
1314

1415
if [ "$FRAMEWORK" = "netstandard2.1" ]; then
1516
FRAMEWORK="netcoreapp3.1"
@@ -31,5 +32,5 @@ if [[ -n "${FRAMEWORK}" ]]; then
3132
fi
3233

3334
for file in $TEST_PROJECT_PATH; do
34-
dotnet test "${file}" -c Release --no-build ${FILTER_PARAMETER} ${FRAMEWORK_PARAMETER} --results-directory ./build/test-results --logger "junit;verbosity=detailed;LogFileName=TEST-{assembly}.xml;FailureBodyFormat=Verbose" --logger "console;verbosity=detailed"
35+
dotnet test "${file}" -c ${CONFIGURATION} --no-build ${FILTER_PARAMETER} ${FRAMEWORK_PARAMETER} --results-directory ./build/test-results --logger "junit;verbosity=detailed;LogFileName=TEST-{assembly}.xml;FailureBodyFormat=Verbose" --logger "console;verbosity=detailed"
3536
done

evergreen/run-unit-tests.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#!/usr/bin/env bash
1+
#!/usr/bin/env bash
22
set -o errexit # Exit the script with error if any of the commands fail
33

44
FRAMEWORK=${FRAMEWORK:-net6.0}
5+
CONFIGURATION=${CONFIGURATION:-'Release'}
56

6-
./evergreen/compile-sources.sh
7-
TEST_CATEGORY="!Integration" ./evergreen/execute-tests.sh
7+
CONFIGURATION=${CONFIGURATION} ./evergreen/compile-sources.sh
8+
CONFIGURATION=${CONFIGURATION} TEST_CATEGORY="!Integration" ./evergreen/execute-tests.sh

src/Directory.Build.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
<CodeAnalysisRuleSet>..\..\MongoDB.ruleset</CodeAnalysisRuleSet>
1111
</PropertyGroup>
1212

13+
<PropertyGroup Condition=" '$(Configuration)' == 'Release_CSharp_14' ">
14+
<!--Do not update LangVersion to 14 in Driver-->
15+
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
16+
</PropertyGroup>
17+
1318
<PropertyGroup>
1419
<Company>MongoDB Inc.</Company>
1520
<Copyright>Copyright © 2010-present MongoDB Inc.</Copyright>

tests/BuildProps/Tests.Build.props

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
<IsOSX Condition="$([MSBuild]::IsOSPlatform('OSX'))">true</IsOSX>
66
</PropertyGroup>
77

8+
<PropertyGroup>
9+
<Configurations>Debug;Release;Release_CSharp_14</Configurations>
10+
<Platforms>AnyCPU</Platforms>
11+
</PropertyGroup>
12+
13+
<PropertyGroup Condition=" '$(Configuration)' == 'Release_CSharp_14' ">
14+
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
15+
<DefineConstants>$(DefineConstants);CSHARP_14</DefineConstants>
16+
<LangVersion>14</LangVersion>
17+
</PropertyGroup>
18+
19+
<PropertyGroup Condition=" '$(Configuration)' != 'Release_CSharp_14' ">
20+
<LangVersion>12</LangVersion>
21+
</PropertyGroup>
22+
823
<PropertyGroup>
924
<TargetFrameworks>netcoreapp3.1;net6.0;net10.0</TargetFrameworks>
1025
<TargetFrameworks Condition="'$(IsWindows)'=='true'">$(TargetFrameworks);net472</TargetFrameworks>
@@ -14,7 +29,6 @@
1429
</PropertyGroup>
1530

1631
<PropertyGroup>
17-
<LangVersion>12</LangVersion>
1832
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1933
</PropertyGroup>
2034

@@ -28,7 +42,7 @@
2842
</PropertyGroup>
2943

3044
<PropertyGroup>
31-
<DefineConstants>TRACE</DefineConstants>
45+
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
3246
</PropertyGroup>
3347
<PropertyGroup Condition="'$(IsWindows)'=='true'">
3448
<DefineConstants>$(DefineConstants);WINDOWS</DefineConstants>

tests/MongoDB.Bson.TestHelpers/Reflector.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
/* Copyright 2018-present MongoDB Inc.
2-
*
3-
* Licensed under the Apache License, Version 2.0 (the "License");
4-
* you may not use this file except in compliance with the License.
5-
* You may obtain a copy of the License at
6-
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
9-
* Unless required by applicable law or agreed to in writing, software
10-
* distributed under the License is distributed on an "AS IS" BASIS,
11-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
* See the License for the specific language governing permissions and
13-
* limitations under the License.
14-
*/
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1515

1616
using System;
1717
using System.Collections.Generic;
@@ -40,13 +40,19 @@ public static object GetStaticFieldValue(Type type, string name, BindingFlags fl
4040
return fieldInfo.GetValue(null);
4141
}
4242

43+
public static TFieldType GetStaticFieldValue<TObjectType, TFieldType>(string name, BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Static)
44+
{
45+
var fieldInfo = GetDeclaredOrInheritedField(typeof(TObjectType), name, flags);
46+
return (TFieldType)fieldInfo.GetValue(null);
47+
}
48+
4349
public static object Invoke(object obj, string name, BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance)
4450
{
4551
try
4652
{
47-
var methodInfo = obj.GetType().GetMethods(flags)
48-
.Where(m => m.Name == name && m.GetParameters().Length == 0)
49-
.Single();
53+
var methodInfo = obj.GetType()
54+
.GetMethods(flags)
55+
.Single(m => m.Name == name && m.GetParameters().Length == 0);
5056
return methodInfo.Invoke(obj, new object[] { });
5157
}
5258
catch (TargetInvocationException exception)
@@ -58,9 +64,9 @@ public static object Invoke(object obj, string name, BindingFlags flags = Bindin
5864
public static object Invoke<T1>(object obj, string name, T1 arg1)
5965
{
6066
var parameterTypes = new[] { typeof(T1) };
61-
var methodInfo = obj.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
62-
.Where(m => m.Name == name && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes))
63-
.Single();
67+
var methodInfo = obj.GetType()
68+
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
69+
.Single(m => m.Name == name && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes));
6470
try
6571
{
6672
return methodInfo.Invoke(obj, new object[] { arg1 });

0 commit comments

Comments
 (0)