当我尝试在 RegisterMethod 中传递 x.Bar 时出现以下错误。
无法使用实例引用访问成员“TestProject2.Foo.Bar.get”;改为使用类型名称来限定它
问:是否有可能让 Bar 属性保持静态?
namespace TestProject2
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
RegisterMethod<Foo, string>(x => x.Bar); <-- error here
}
static void RegisterMethod<TSelf, TProp>(Expression<Func<TSelf, TProp>> expression)
{
var member_expression = expression.Body as MemberExpression;
if (member_expression == null)
return;
var member = member_expression.Member;
if (member.MemberType != MemberTypes.Property)
return;
var property = member as PropertyInfo;
Console.WriteLine(property.Name);
Console.WriteLine(property.DeclaringType);
}
}
public class Foo
{
private string _bar;
public static string Bar <-- would very much want to keep this static
{
get { return _bar; }
set { _bar = value; }
}
}
}