Parameter transfer of reference type in reflection technology dynamic call Method

Source: Internet
Author: User
The reference type parameter in the dynamic call method of reflection technology is transferred a few days ago. When programming, you need to use the reflection technology to dynamically execute the method call. After passing the reference type parameter during the call process, the value cannot be returned normally. For details, refer to the following url: Success. Here thanks to "Nan crazy", read the http://name-lh.cnblogs.com/archive/2006/02/08/327146.html to solve my problem, about the reflection call method to pass the knowledge of the application type parameters, Nan crazy that Article write a lot of introduction, now I want to write out my ideas in the solution process. First, we will analyze the code below (the code below has been compiled ):
Namespace app
{
Public Class
{
Public void setvalue (INT num, ref datatable _ TB)
{
Num = 5;
_ TB. Columns. Add ("temp1 ");
}
Public void setvalue1 (out string S, out datatable _ TB)
{
S = "temp2 ";
_ TB = new datatable ();
_ TB. Columns. Add ("temp2 ");
}
}
Class Test
{
[Stathread]
Static void main (string [] ARGs)
{
Int num = 0;
String S = "reflection ";
Datatable TB = new datatable ();

Type _ type = typeof ();
Object _ o = activator. createinstance (_ type );

/// Section 1
Object [] Param = new object [2];
Param [0] = s;
Param [1] = Tb;

Console. writeline (Num );
Console. writeline (s );
Console. writeline (Tb. Columns. Count + "/N ");

Parametermodifier [] parammod = new parametermodifier [1];
Parammod [0] = new parametermodifier (2 );
Parammod [0] [0] = true;
Parammod [0] [1] = true;

_ Type. invokemember ("setvalue", bindingflags. Default | bindingflags. invokemethod,
Null, _ o, Param, parammod, null, null );

// Section 2
Console. writeline (Num );
Console. writeline (Param [0]);
Console. writeline (datatable) Param [1]). Columns. Count );

// Section3
// Console. writeline (Tb. Columns. Count );
}
}
}

Analysis: 1. From the code above, we declared a Class A and declared the overload function setvalue in the class, mainly to demonstrate that reflection has functions with the same types of automatic matching parameters. 2. In the main function, we focus on Section1, section2, and section3. Section1 is mainly used to verify reflection when different parameters are passed. If the code Param [0] passes S, reflection will automatically match the second function. If num is input to Param [0, then the reflection will automatically call the first function in Class A, which may be. Net, but if you use invoke in methodinfo, it will not automatically match. Why? The code below section2 in the program is mainly to verify that parameters of the reference type need to be passed in for calling Using Reflection technology, and the changes before and after the value of the reference type parameter; section3 is a place that is easy to ignore, at first, I did not notice that when I pass in the ref type parameter, the reference object in Param [1] and TB is the same. If I pass in the out type parameter, param [1] is different from the object referenced in TB. Why is this difference? If you want to retrieve the value of an out-type parameter, you must use the value of para [1]. 3. When using reflection technology to dynamically call a parameter of the reference type, the preceding parameter Param [1] Must be initialized. Why? If we use invoke in methodinfo to call parameters of the reference type, if the overload function is included, especially when the data object (datatable or dataset) is passed ), it is especially complicated to find matching functions. When getting the returned value, you must also use the object in the input parameter array, instead of using the variable in the function. The above are some of my experiences. Now I come back and think about it. In the early days, I couldn't get the return value of the reference type parameter, but I mainly ignored section3.

 

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.