[英]How to recognize which methods are getters and setters?
I am trying to use reflection to get all class methods. 我正在尝试使用反射来获取所有类方法。 I want to prepare an algorithm that will recognize which methods are getters and setters.
我想准备一个算法来识别哪些方法是getter和setter。
So, as you see I am printing each getter in the format: {name} will return {Return Type}
. 因此,如您所见,我将以格式打印每个getter :
{name} will return {Return Type}
。 I am trying to print all of the setters in the format: {name} will set field of {Parameter Type}
, but I don't know how to get the Parameter Type
. 我试图以格式打印所有的setter :
{name} will set field of {Parameter Type}
,但我不知道如何获取Parameter Type
。
public string CollectGettersAndSetters(string className)
{
Type classType = Type.GetType(className);
MethodInfo[] getters = classType
.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(m => m.Name.StartsWith("get"))
.ToArray();
MethodInfo[] setters = classType
.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(m => m.Name.StartsWith("set"))
.ToArray();
StringBuilder sb = new StringBuilder();
foreach (MethodInfo getter in getters)
{
sb.AppendLine($"{getter.Name} will return {getter.ReturnType}");
}
foreach (MethodInfo setter in setters)
{
sb.AppendLine($"{setter.Name} will set field of {?}");
}
return sb.ToString().TrimEnd();
}
An example of class for which I will use the method: 我将使用该方法的类的示例:
public class Hacker
{
public string username = "securityGod82";
private string password = "mySuperSecretPassw0rd";
public string Password
{
get => this.password;
set => this.password = value;
}
private int Id { get; set; }
public double BankAccountBalance { get; private set; }
public void DownloadAllBankAccountsInTheWorld()
{
}
}
The expected output is: 预期的产出是:
get_Password will return System.String
get_Id will return System.Int32
get_BankAccountBalance will return System.Double
set_Password will set field of System.String
set_Id will set field of System.Int32
set_BankAccountBalance will set field of System.Double
Thank you in advance! 先感谢您!
You can get the first parameter, and then use ParameterType
: 您可以获取第一个参数,然后使用
ParameterType
:
var parameter = setter.GetParameters()[0];
sb.AppendLine($"{setter.Name} will set field of {parameter.ParameterType}");
I suggest you do some more checking, just to make sure it is actually a setter: 我建议你做一些检查,只是为了确保它实际上是一个二传手:
var parameters = setter.GetParameters();
if (parameters.Length != 1) { continue; }
sb.AppendLine($"{setter.Name} will set field of {parameters[0].ParameterType}");
EDIT: 编辑:
Actually, you can get the setters by first getting the properties, then call the GetSetMethod
method, which is a much safer approach than iterating through all the methods. 实际上,您可以通过首先获取属性来获取setter,然后调用
GetSetMethod
方法,这比迭代所有方法更安全。
MethodInfo[] setters = classType
.GetProperties()
.Select(x => x.GetSetMethod(true))
.Where(x => x != null).ToArray();
The same thing goes for getters - with GetGetMethod
. GetGetMethod
适用于getter。
You can get the list of parameters with: 您可以使用以下命令获取参数列表:
var parameters = setter.GetParameters();
Since it's a setter, it will have only one parameter, so you can do 由于它是一个setter,它只有一个参数,所以你可以这样做
var setterType = setter.GetParameters().First().ParameterType;
to access its Type
. 访问其
Type
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.