Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Reflection 2
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Reflection; namespace Rextester { class MyClass { int x; int y; public MyClass(int i, int j) { x = i; y = j; } public int Sum() { return x+y; } public bool IsBetween(int i) { if(x < i && i < y) return true; else return false; } public void Set(int a, int b) { x = a; y = b; } public void Set(double a, double b) { x = (int) a; y = (int) b; } public void Set(int a, double b) //Extra added method { x = (int) a; y = (int) b; } public void Set(double a, int b) //Extra added method { x = (int) a; y = (int) b; } public void Show() { Console.WriteLine(" x: {0}, y: {1}", x, y); } } public class Program { public static void Main(string[] arg) { Type t = typeof(MyClass); MyClass reflectOb = new MyClass(10, 20); int val; Console.WriteLine("Invoking methods in " + t.Name); Console.WriteLine(); MethodInfo[] mi = t.GetMethods(); // Invoke each method. foreach(MethodInfo m in mi) { // Get the parameters. ParameterInfo[] pi = m.GetParameters(); if(m.Name.Equals("Set", StringComparison.Ordinal) && (pi[0].ParameterType == typeof(int) && pi[1].ParameterType == typeof(int))) { object[] args = new object[2]; args[0] = 9; args[1] = 18; m.Invoke(reflectOb, args); } else if(m.Name.Equals("Set", StringComparison.Ordinal) && (pi[0].ParameterType == typeof(double) && pi[1].ParameterType == typeof(double))) { object[] args = new object[2]; args[0] = 1.12; args[1] = 23.4; m.Invoke(reflectOb, args); } else if(m.Name.Equals("Sum", StringComparison.Ordinal)) { val = (int) m.Invoke(reflectOb, null); Console.WriteLine("sum is " + val); } else if(m.Name.Equals("IsBetween", StringComparison.Ordinal)) { object[] args = new object[1]; args[0] = 14; if((bool) m.Invoke(reflectOb, args)) Console.WriteLine("14 is between x and y"); } else if(m.Name.Equals("Show", StringComparison.Ordinal)) { m.Invoke(reflectOb, null); } if(m.Name.Equals("Set", StringComparison.Ordinal) &&(pi[0].ParameterType == typeof(int) && pi[1].ParameterType == typeof(double))) { object[] args = new object[2]; args[0] = 12; args[1] = 23.4; m.Invoke(reflectOb, args); } if(m.Name.Equals("Set", StringComparison.Ordinal) && (pi[0].ParameterType == typeof(double) && pi[1].ParameterType == typeof(int) )) { object[] args = new object[2]; args[0] = 12.1; args[1] = 23; m.Invoke(reflectOb, args); } } } } }
run
|
edit
|
history
|
help
0
Ezaz
DateTime change clock
Url Encode
How to Count Occurrences of a Word in a String (LINQ) (C#)
test code
Math 10.93 added fracOp
Hello World!
waefueiure
sfe
Exception