Is it possible to get a setter method name using the new nameof
operator?
public object Foo { get; set; }
public void Test()
{
var myMethod = GetType().GetMethod("set_Foo");
}
I guess GetType().GetMethod("set_" + nameof(Foo))
could work but is there something more straightforward?
You can't use nameof
to get the setter method name directly.
You can combine it with reflection to get the property and use PropertyInfo.SetMethod
to get the setter:
MethodInfo setterMethod = GetType().GetProperty(nameof(Foo)).SetMethod;
string setterName = setterMethod.Name;
Something like -
var type = typeof(Test).GetProperties().FirstOrDefault().GetAccessors(false);
where Test
is the type
with a property
S3
of type string
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.