Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Type type = typeof(Car);
- // var method = type.GetMethod("MethodName");
- var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
- foreach(var method in methods)
- {
- var parameters = method.GetParameters();
- foreach(var parameter in parameters)
- {
- Console.WriteLine($" - {paramter.ParameterType} {parameter.Name}"); // type and name
- }
- }
- //
- var ferrari = new Ferrari();
- Type type = typeof(Ferrari);
- var method = type.GetMethod("Move"); // without BindingFlags if method is public or this will return null
- var returnValue = method.Invoke(ferrari, new object[] {23, 54}); // invoke method "Move" to object Ferrari with params 23, 54
- //
- public string CollectGettersAndSetters(string className)
- {
- Type type = typeof(Hacker);
- var sb = new StringBuilder();
- var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- foreach (var method in methods.Where(m=>m.Name.StartsWith("get"))) // get
- {
- sb.AppendLine($"{method.Name} will return {method.ReturnType}"); // ReturnType
- }
- foreach (var method in methods.Where(m => m.Name.StartsWith("set"))) // set
- {
- sb.AppendLine($"{method.Name} will set field of {method.GetParameters().First().ParameterType}"); // ParameterType
- }
- string result = sb.ToString().TrimEnd();
- return result;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement