C# 0 The Complete Reference


PART I C h a p t e r   1 7



Download 4,07 Mb.
Pdf ko'rish
bet600/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   596   597   598   599   600   601   602   603   ...   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  

479


PART IPART I

      ParameterInfo[] pi =  ci[x].GetParameters();

      if(pi.Length == 2) break;

    }


    if(x == ci.Length) {

      Console.WriteLine("No matching constructor found.");

      return;

    }


    else

      Console.WriteLine("Two-parameter constructor found.\n");

    // Construct the object.

    object[] consargs = new object[2];

    consargs[0] = 10;

    consargs[1] = 20;

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

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

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

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

      }


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

        m.Invoke(reflectOb, null);

www.freepdf-books.com



480

 

P a r t   I :  



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

      }


    }

  }


}

The output from the program is shown here:

Found: MyClass

Found: AnotherClass

Found: Demo

Using: MyClass

Available constructors:

   MyClass(Int32 i)

   MyClass(Int32 i, Int32 j)

Two-parameter constructor found.

Constructing MyClass(int, int).

Values are x: 10, y: 20

Invoking methods on reflectOb.

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

As the output shows, all three classes contained within 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   596   597   598   599   600   601   602   603   ...   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