C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet603/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   599   600   601   602   603   604   605   606   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Set

 and 


Sum

. However, using reflection 

it is possible to utilize a type about which you have no prior knowledge. To do this, you 

must discover all information necessary to construct an object and to generate method calls. 

Such an approach would be useful to a visual design tool, for example, because it could 

utilize the types available on the system.

To see how the full dynamic discovery of a type can be accomplished, consider the 

following example, which loads the 



MyClasses.exe

 assembly, constructs a 



MyClass

 object, 

and then calls all of the methods declared by 

MyClass

, all without assuming any prior 

knowledge:

// Utilize MyClass without assuming any prior knowledge.

using System;

using System.Reflection;

class ReflectAssemblyDemo {

  static void Main() {

    int val;

    Assembly asm = Assembly.LoadFrom("MyClasses.exe");

    Type[] alltypes = asm.GetTypes();

    Type t = alltypes[0]; // use first class found

    Console.WriteLine("Using: " + t.Name);

    ConstructorInfo[] ci = t.GetConstructors();

    // Use first constructor found.

    ParameterInfo[] cpi = ci[0].GetParameters();

    object reflectOb;

    if(cpi.Length > 0) {

      object[] consargs = new object[cpi.Length];

      // Initialize args.

      for(int n=0; n < cpi.Length; n++)

        consargs[n] = 10 + n * 20;

      // Construct the object.

      reflectOb = ci[0].Invoke(consargs);

    } else

www.freepdf-books.com




482

 

P a r t   I :  



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

      reflectOb = ci[0].Invoke(null);

    Console.WriteLine("\nInvoking methods on reflectOb.");

    Console.WriteLine();

    // Ignore inherited methods.

    MethodInfo[] mi = t.GetMethods(BindingFlags.DeclaredOnly |

                                   BindingFlags.Instance |

                                   BindingFlags.Public) ;

    // Invoke each method.

    foreach(MethodInfo m in mi) {

      Console.WriteLine("Calling {0} ", m.Name);

      // Get the parameters.

      ParameterInfo[] pi = m.GetParameters();

      // Execute methods.

      switch(pi.Length) {

        case 0: // no args

          if(m.ReturnType == typeof(int)) {

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

            Console.WriteLine("Result is " + val);

          }

          else if(m.ReturnType == typeof(void)) {

            m.Invoke(reflectOb, null);

          }

          break;

        case 1: // one arg

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

            object[] args = new object[1];

            args[0] = 14;

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

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

            else

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

          }

          break;

        case 2: // two args

          if((pi[0].ParameterType == typeof(int)) &&

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

            object[] args = new object[2];

            args[0] = 9;

            args[1] = 18;

            m.Invoke(reflectOb, args);

          }

          else if((pi[0].ParameterType == typeof(double)) &&

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

            object[] args = new object[2];

            args[0] = 1.12;

            args[1] = 23.4;

            m.Invoke(reflectOb, args);

          }

          break;

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   599   600   601   602   603   604   605   606   ...   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