Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Reflection 2
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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); } } } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0.11 sec, absolute running time: 0.09 sec, cpu time: 0.08 sec, average memory usage: 16 Mb, average nr of threads: 4
fork mode
|
history
|
discussion
Invoking methods in MyClass sum is 30 14 is between x and y x: 12, y: 23