Skip to content

Commit eadf2c7

Browse files
authored
Expression.Field should validate field name for null (#34301)
Small change to correctly check for null in one of the overloads of Expression.Field.
1 parent 11ffcdc commit eadf2c7

File tree

2 files changed

+2
-0
lines changed

2 files changed

+2
-0
lines changed

src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/MemberExpression.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public static MemberExpression Field(Expression expression, string fieldName)
177177
public static MemberExpression Field(Expression? expression, Type type, string fieldName)
178178
{
179179
ContractUtils.RequiresNotNull(type, nameof(type));
180+
ContractUtils.RequiresNotNull(fieldName, nameof(fieldName));
180181

181182
// bind to public names first
182183
FieldInfo? fi = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy)

src/libraries/System.Linq.Expressions/tests/Member/MemberAccessTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public static void Field_NullField_ThrowsArgumentNullException()
224224
{
225225
AssertExtensions.Throws<ArgumentNullException>("field", () => Expression.Field(null, (FieldInfo)null));
226226
AssertExtensions.Throws<ArgumentNullException>("fieldName", () => Expression.Field(Expression.Constant(new FC()), (string)null));
227+
AssertExtensions.Throws<ArgumentNullException>("fieldName", () => Expression.Field(Expression.Constant(new FC()), typeof(FC), (string)null));
227228
}
228229

229230
[Fact]

0 commit comments

Comments
 (0)