Dynamic call object attributes and methods-methods with both performance and flexibility

Source: Internet
Author: User

In dynamic programming, we often need to determine which attribute or method of the called object during runtime. This task can usually be solved by reflection. But as we all know, the reflection performance is much lower than the static method, because reflection needs to be completed through a complex mechanism at runtime. Can dynamic calling with both performance and flexibility be achieved? I have repeatedly considered this issue when developing the latest VBF functions. We usually use this method to dynamically call the attributes of an object. Assume that object a has an attribute called MyProp:

Type t = a. GetType ();
PropertyInfo pi = t. GetProperty ("MyProp ");

String value = (string) pi. GetValue (a, null );

Have you noticed any problems? We know that the type of this attribute is string and that it has no parameters. Of course, some people do not know the type and parameters of the property to be called, but we know it in this case, but it is not used, or we do not know anything about it. We also use a pure dynamic method to obtain it. In this way, we miss the opportunity to use the strong type feature to accelerate this process. There are also method calls. Sometimes we only do not know the method or attribute name during compilation (for example, we need to specify it), but we know the type and signature of the method or attribute, in this case, the generic and delegated technologies can be used for high-performance calling.

Generic technology provides convenience for processing types. In addition,. NET delegation also has some additional good features. A delegate can take on tasks similar to an interface, but the biggest difference is that a method does not have to declare that it meets a delegate. As long as the signature is correct, it can be assigned to the delegate variable. In this way, we can use a set of pre-declared delegates to process ever-changing types of attributes and methods.

C # attributes with parameters are not allowed, unless they are indexers. VB allows attributes with parameters, but few of them are used in large quantities. In the real world, the getter and setter formats of attributes are limited. The getter and setter attributes can be represented by the following two delegates:

Public delegate void PropertySetter <T> (T value );
Public delegate T PropertyGetter <T> ();

With these two delegates, we can perform high-speed, strong-type dynamic access to attributes with known types but whose names need dynamic. The method is to use reflection to get the MethodInfo of the Gettet or Setter attribute, and then use MethodInfo to create the delegate:

Type t = a. GetType ();
PropertyInfo pi = t. GetProperty ("MyProp ");
MethodInfo getter = pi. GetGetMethod ();

PropertyGetter <string> strPropGetter =
(PropertyGetter <string>) Delegate. CreateDelegate (
Typeof (PropertyGetter <string>), a, getter );

String value = strPropGetter ();

Note that this method performs more reflection operations before calling. Therefore, if you only want to obtain the attribute value once or twice, this method is not as fast as using radiation directly. However, when you need to perform thousands of accesses to the same attribute, the absolute value is much more than this Code. In a simple attribute of the string type, the speed can be up to 1000 times faster than that obtained by direct reflection, this is my actual result.

Next we will discuss how to call index attributes and methods. C # use Attribute parameters in the syntax of the indexer as much as possible. In VB's view, the indexer is only a special attribute with parameters in the class, he is granted the permission to access objects using array syntax. In any case, the getter and setter processes of the indexer and common attributes with parameters are not as simple as the typical attributes. There is also a call to the method, and the method signature is ever-changing. It seems that it is difficult for us to use a pre-defined delegate for unified calling. This is true, but compared with defining a delegate for the signature of each type of attribute accessor or method, generic offers a slightly more comfortable approach:

Public delegate R Func <R> ();
Public delegate R Func <T0, R> (T0 a0 );
Public delegate R Func <T0, T1, R> (T0 a0, T1 a1 );
Public delegate R Func <T0, T1, T2, R> (T0 a0, T1 a1, T2 a2 );

Such a set of generic delegation can cover all method signatures with the number of parameters ranging from 0 to 3, return values, and no parameters being out or ref. You can also define a group for no return value. With such a set of generic delegation, you can directly create a function signature without declaring a new type. Combined with the method just now, we can use a unified method to achieve most of the dynamic calls of attributes or methods with parameters-both the dynamic name and the strong type of performance.

You may have used a similar method and used it for tasks other than dynamically calling attributes or methods. I just thought of them when developing VBF, hoping to help some people who need it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.