未验证 提交 1306b036 编写于 作者: V Vitek Karas 提交者: GitHub

Add a test for Expression.Property and its handling of property accessors (#54279)

* Add a test for Expression.Property and its handling of property accessors

This mostly a linker test as this effectively validates linker feature from https://github.com/mono/linker/pull/1880.

But having a clean trimming tests here is better validation for this specific case.

* Add one more test case as per feedback

* Remove debugging leftover
上级 eb7b3db7
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
/// <summary>
/// Tests that the System.Linq.Expressions.Expression.Property will correctly preserve both
/// accessors even if only one is referenced by MethodInfo.
/// </summary>
internal class Program
{
static int Main(string[] args)
{
int result = ExplicitCreation.Test();
if (result != 100)
return result;
result = LambdaCreation.Test();
return result;
}
class ExplicitCreation
{
class TestType
{
public bool _testPropertyValue;
public bool TestProperty {
get => _testPropertyValue;
set { _testPropertyValue = value; }
}
}
public static int Test()
{
var obj = new TestType();
var param = Expression.Parameter(typeof(TestType));
var prop = Expression.Property(param, typeof(TestType).GetMethod("get_TestProperty"));
((PropertyInfo)prop.Member).SetValue(obj, true);
if (obj._testPropertyValue != true)
return -1;
return 100;
}
}
class LambdaCreation
{
class TestType
{
public bool _testPropertyValue;
public bool TestProperty {
get => _testPropertyValue;
set { _testPropertyValue = value; }
}
}
public static int Test()
{
var obj = new TestType ();
Expression<Func<TestType, bool>> expression = t => t.TestProperty;
var prop = ((MemberExpression)expression.Body);
((PropertyInfo)prop.Member).SetValue(obj, true);
if (obj._testPropertyValue != true)
return -2;
return 100;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册