You can of course call cos in the normal course of

Info icon This preview shows pages 707–709. Sign up to view the full content.

View Full Document Right Arrow Icon
You can, of course, call Cos() in the normal course of your code, but reflection allows you to bind to that method at runtime. This is called late binding , and offers the flexibility of choosing at runtime which ob- ject to bind to and invoking it programmatically. The dynamic keyword added in C# 4.0, discussed in Chapter 18 , can do this for you, but you may sometimes want to control the underlying mechanisms for late binding yourself. This can be useful when creating a custom script to be run by the user or when working with objects that might not be available at compile time. Reflection | 683
Image of page 707

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
To invoke Cos() , first get the Type information for the System.Math class: Type theMathType = typeof(System.Math); Once we have type information, we could dynamically create an instance of the type using a static method of the Activator class. However, we don’t need to here because Cos() is static. In fact, all members of System.Math are static, and even if you wanted to create an instance, you can’t because System.Math has no public constructor. However, since you will come across types that need to be instantiated so that you can call their nonstatic members, it’s important to know how to create new objects with reflection. The Activator class contains three methods, all static, which you can use to create objects. The methods are as follows: CreateComInstanceFrom Creates instances of COM objects. CreateInstanceFrom Creates a reference to an object from a particular assembly and type name. CreateInstance Creates an instance of a particular type from a Type object. For example: Object theObj = Activator.CreateInstance(someType); Back to the Cos() example. Our theMathType variable now refers to a Type object which we obtained by calling GetType . Before we can invoke a method on the type, we must get the method we need from the Type object. To do so, we call GetMethod() , passing the method name: MethodInfo cosineInfo = theMathType.GetMethod("Cos"); There’s obviously a problem here if you need to deal with overloaded methods. That’s not an issue for this particular example—there’s only one Cos method. But if you need to deal with multiple methods of the same name, you can use an alternative overload of GetMethod that takes two arguments. After the method name you can pass an array of the argument types, which allows you to uniquely identify the overload you require. We could use that here if we wanted even though it’s not necessary—we could create a Type[] array containing one entry: the typeof(double) . This would tell GetMethod that we are looking specifi- cally for a method called Cos that takes a single argument of type double . You now have an object of type MethodInfo which provides an Invoke method that calls the method this MethodInfo represents. Normally, the first argument to Invoke would be the object on which we want to invoke the method. However, because this is a static method, there is no object, so we just pass null . And then we pass the arguments for the function.
Image of page 708
Image of page 709
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern