[英]Using nameof to get a setter method name in c#
是否可以使用新的nameof
运算符获取setter方法名称?
public object Foo { get; set; }
public void Test()
{
var myMethod = GetType().GetMethod("set_Foo");
}
我猜GetType().GetMethod("set_" + nameof(Foo))
可以工作,但还有更直接的东西吗?
您不能使用nameof
直接获取setter方法名称。
您可以将它与反射结合以获取属性并使用PropertyInfo.SetMethod
来获取setter:
MethodInfo setterMethod = GetType().GetProperty(nameof(Foo)).SetMethod;
string setterName = setterMethod.Name;
就像是 -
var type = typeof(Test).GetProperties().FirstOrDefault().GetAccessors(false);
其中Test
是具有type
为string
的property
S3
的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.