.


:




:

































 

 

 

 


.




, MyClass

:

Int32 Sum ()

Boolean IsBetween(Int32 i)

Void Set(Int32 a, Int32 b)

Void Set(Double a, Double b)

Void Show()

, , MyClass.

, , , . Invoke (), Methodlnf . :

object Invoke(object obj, object[] parameters)

obj , . (static) obj (null). , , parameters. , parameters (null). , parameters . , , parameters , . , , Invoke (), .

Invoke () Methodlnf , GetMethods (). .

// .

Using System;

Using System.Reflection;

class MyClass { int x; int y;

public MyClass(int i, int j) { x = i;

= 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) {

Console.Write(" Set (int, int). "); x = a;

= b;

Show();

}

// Set.

public void Set(double a, double b) {

Console.Write(" Set(double, double). "); x = (int) a; = (int) b;

Show ();

}

public void Show() {

Console.WriteLine(" x: {0}, : {1}", x, );

}

}

class InvokeMethDemo { static void Main() {

Type t = typeof(MyClass);

MyClass reflectOb = new MyClass(10, 20); int val;

Console.WriteLine(" , " + t.Name); Console.WriteLine();

MethodInfo[] mi = t.GetMethods();

// , foreach(Methodlnfo m in mi) {

// .

Parameterlnfo[] pi = m.GetParameters();

if(m.Name.CompareTo("Set")==0 &&

pi[0].ParameterType == typeof(int)) { object[] args = new object[2]; args[0] = 9; args[l] = 18;

M. Invoke(reflectOb, args);

}

else if(m.Name.CompareTo("Set") ==0 &&

pi[0].ParameterType == typeof(double)) {

object[] args = new object[2]; args[0] = 1.12; args[1] = 23.4; m. Invoke(reflectOb, args);

}

else if(m.Name.CompareTo("Sum")==0) {

val = (int) m.Invoke(reflectOb, null);

Console.WriteLine(" " + val);

}

else if(m.Name.CompareTo("IsBetween")==0) {

object[] args = new object[1]; args[0] = 14;

If((bool) m.Invoke(reflectOb, args))

Console.WriteLine(" 14 x ");

}

else if(m.Name.CompareTo("Show")==0) {

M.Invoke(reflectOb, null);

}

}

}

}

.

, MyClass 30

14

Set (int, int). : 9, : 18 Set(double, double). : 1, : 23 : 1, : 23

. . foreach . if/else. Set () .

if(m.Name.CompareTo("Set")==0 &&

pi(0].ParameterType == typeof(int)) {

object[] args = new object[2]; args[0] = 9; args[l] = 18;





:


: 2016-12-03; !; : 350 |


:

:

,
==> ...

961 - | 874 -


© 2015-2024 lektsii.org - -

: 0.013 .