C# 0 The Complete Reference


PART I C h a p t e r   1 7



Download 4,07 Mb.
Pdf ko'rish
bet590/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   586   587   588   589   590   591   592   593   ...   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  

471


PART IPART I

          Console.WriteLine("14 is between x and y");

      }

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

        m.Invoke(reflectOb, null);

      }


    }

  }


}

The output is shown here:

Invoking methods in MyClass

sum is 30

14 is between x and y

Inside Set(int, int). Values are x: 9, y: 18

Inside Set(double, double). Values are x: 1, y: 23

Values are x: 1, y: 23

Look closely at how the methods are invoked. First, a list of methods is obtained. Then, 

inside the 



foreach

 loop, parameter information is retrieved. Next, using a series of if/else 

statements, each method is executed with the proper type and number of arguments. Pay 

special attention to the way that the overloaded 



Set( )

 method is executed by the following 

code:

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

}

If the name of the method is 



Set

, then the type of the first parameter is tested to determine 

which version of the method was found. If it was 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   586   587   588   589   590   591   592   593   ...   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