Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Reflection constructors
//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; int z; public MyClass(int i) { Console.WriteLine("Constructing MyClass(int, int). "); x = y = i; } public MyClass(int i, int j) { Console.WriteLine("Constructing MyClass(int, int). "); x = i; y = j; Show(); } public MyClass(int i, int j,int k) { Console.WriteLine("Constructing MyClass(int, int,int). "); x = i; y = j; z=k; Show(); } public int Sum() { return x+y+z; } public bool IsBetween(int i) { if((x < i) && (i < y)) return true; else return false; } public void Set(int a, int b) { Console.Write("Inside Set(int, int). "); x = a; y = b; Show(); } // Overload Set. public void Set(double a, double b) { Console.Write("Inside Set(double, double). "); x = (int) a; y = (int) b; Show(); } public void Show() { Console.WriteLine("Values are x: {0}, y: {1}, z:{2}", x, y, z); } } public class Program { public static void Main(string[] arg) { object reflectOb=null; ; Type t = typeof(MyClass); int val; // Get constructor info. ConstructorInfo[] ci = t.GetConstructors(); Console.WriteLine("Available constructors: "); foreach(ConstructorInfo c in ci) { // Display return type and name. Console.Write(" " + t.Name + "("); // Display parameters. ParameterInfo[] pi = c.GetParameters(); for(int i=0; i < pi.Length; i++) { Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name); if(i+1 < pi.Length) Console.Write(", "); } Console.WriteLine(")"); } Console.WriteLine(); // Find matching constructor. int x; int ch=0; do { Console.WriteLine("Enter number of pareameters:\n"); ch=Convert.ToInt32(Console.ReadLine()); for(x=0; x < ci.Length; x++) { ParameterInfo[] pi = ci[x].GetParameters(); if(pi.Length==ch) { break; } } if(ch==3) { Console.WriteLine("Two-parameter constructor found.\n"); object[] consargs = new object[3]; consargs[0] = 10; consargs[1] = 20; consargs[2] = 30; reflectOb = ci[x].Invoke(consargs); break; } if(ch== 2) { Console.WriteLine("Two-parameter constructor found.\n"); object[] consargs = new object[2]; consargs[0] = 10; consargs[1] = 20; // consargs[2] = 30; reflectOb = ci[x].Invoke(consargs); break; } /*if(x == ci.Length) { Console.WriteLine("No matching constructor found."); return; }*/ //} }while(ch!=0); Console.WriteLine("x=:"+x); // Construct the object. /*object[] consargs = new object[3]; consargs[0] = 10; consargs[1] = 20; consargs[2] = 30; object reflectOb = ci[x].Invoke(consargs);*/ Console.WriteLine("\nInvoking methods on reflectOb."); 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)) { // This is Set(int, 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)) { // This is Set(double, 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")) { m.Invoke(reflectOb, null); } } } } }
run
|
edit
|
history
|
help
0
Main5.3
DijkstraAlgo
4x4 ver 3
3
Enigme
page 2
SMS recipient filter testing
task 1
DateTime
Ty