C# 0 The Complete Reference


PART I C h a p t e r   1 7



Download 4,07 Mb.
Pdf ko'rish
bet589/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   585   586   587   588   589   590   591   592   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 7 :  

R u n t i m e   T y p e   I D ,   R e f l e c t i o n ,   a n d   A t t r i b u t e s  

469


PART IPART I

   Boolean IsBetween(Int32 i)

   Void Set(Int32 a, Int32 b)

   Void Set(Double a, Double b)

   Void Show()

As you can see, only those methods explicitly defined by 



MyClass

 are displayed.



Calling Methods Using Reflection

Once you know what methods a type supports, you can call one or more of them. To do 

this, you will use the 

Invoke( )

 method that is contained in 



MethodInfo

. One of its forms 

is shown here:

object Invoke(object 



ob

, object[ ] 



args

)

Here, 



ob

 is a reference to the object on which the method is invoked. For 



static

 methods, pass 



null

 to 


ob.

 Any arguments that need to be passed to the method are specified in the array 



args.

 If no arguments are needed



args

 must be 



null

. Also, 


args

 must contain exactly the same 

number of elements as there are arguments. Therefore, if two arguments are needed, then 

args

 must be two elements long. It can’t, for example, be three or four elements long. The 

value returned by the invoked method is returned by 

Invoke( )

.

To call a method, simply call 



Invoke( )

 on an instance of 



MethodInfo

 that was obtained 

by calling 

GetMethods( )

. The following program demonstrates the procedure:

// Invoke methods using reflection.

using System;

using System.Reflection;

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



    Console.Write("Inside Set(int, int). ");

    x = a;

www.freepdf-books.com



470

 

P a r t   I :  



T h e   C #   L a n g u a g e

    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}", x, y);

  }


}

class InvokeMethDemo {

  static void Main() {

    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.CompareTo("Set")==0 &&

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

        object[] args = new object[2];

        args[0] = 9;

        args[1] = 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("sum is " + val);

      }

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

        object[] args = new object[1];

        args[0] = 14;

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

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   585   586   587   588   589   590   591   592   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish