.


:




:

































 

 

 

 


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);

}

Set, , . , Set (int, int), args. double.

, MyClass, , MyClass . . , .

, , . , .

GetConstructors () . .

Constructorlnfo[] GetConstructors()

GetConstructors () Constructorlnfo, .

Constructorlnfo MethodBase, Memberlnf . . GetConstructors (), , . , GetParameters (), Methodlnf .

, Invoke (), Constructorlnfo. .

object Invoke(object[] parameters)

, , parameters. , parameters (null). parameters , . Invoke () .

MyClass.

// .

Using System;

Using System.Reflection;

class MyClass { int x; int y;

l

public MyClass(int i) {

Console.WriteLine(" MyClass(int, int). "); x = = i;

}

public MyClass(int i, int j) {

Console.WriteLine(" MyClass(int, int). "); x = i;

= j;

Show ();

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(" (double, double). "); &#9632;

x = (int) a; = (int) b;

Show();

}

public void Show() {

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

}

}

class InvokeConsDemo { static void Main() {

Type t = typeof(MyClass); int val;

// .

Constructorlnfo[] ci = t.GetConstructors();

Console.WriteLine(" : "); foreach(Constructorlnfo in ci) {

// .

Console.Write(" " + t.Name + "(");

// .

Parameterlnfo[] pi = .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 ();

// , int ;

for(x=0; < ci.Length; ++) {

Parametgrlnfo[] pi = ci[x].GetParameters(); if(pi.Length == 2) break;

}

if (x == ci.Length) {

Console.WriteLine(" ."); return;

}

Else

Console.WriteLine(" .\n");

// , object[] consargs = new object[2]; consargs[0] = 10; consargs[1] = 20;

object reflectOb = ci[x].Invoke(consargs);

Console.WriteLine(" reflectOb."); Console.WriteLine();

Methodlnfo[] mi = t.GetMethods();

// , foreach(Methodlnfo m in mi) {

// .

Parameterlnfo[] pi = m.GetParameters(); if(m.Name.CompareTo("Set")==0 &&

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

// Set (int, int). object[] args = new object[2]; args[0] = 9; args[l] = 18;





:


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


:

:

, , . , .
==> ...

1532 - | 1371 -


© 2015-2024 lektsii.org - -

: 0.015 .