是否可以使用new nameof运算符获取setter方法名称?
public object Foo { get; set; }
public void Test()
{
var myMethod = GetType().GetMethod("set_Foo");
}
Run Code Online (Sandbox Code Playgroud)
我想 GetType().GetMethod("set_" + nameof(Foo))可以工作,但还有更直接的东西吗?
您无法直接使用nameof获取setter方法名称.
您可以将它与反射结合使用PropertyInfo.SetMethod以获取属性并使用它来获取setter:
MethodInfo setterMethod = GetType().GetProperty(nameof(Foo)).SetMethod;
string setterName = setterMethod.Name;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |