It is found that the Assembly DLL is in temp, And the appdomain cannot dynamically load the assembly.

Source: Internet
Author: User

When I use code for dynamic generation, the code to be compiled is:

 

1 using system;
2 using system. Windows. forms;
3 using system. Threading;
4 using system. reflection;
5 using system. IO;
6
7 using system. Security;
8 using system. Security. permissions;
9
10
11/*** // <summary>
12 /// Summary of class1.
13 /// </Summary>
14 ///
15
16 public class test: financialbyrefobject
17 {
18 public void testmethod (string srcappdomain)
19 {
20 FileStream fs = File. Create ("11.txt ");
21 fs. Close ();
22
23}
24 public void Click (object sender, System. EventArgs e)
25 {
26 MessageBox. Show ("hi ");
27}
28}

The compilation attributes used are:

1 compilerparams. generateexecutable = false;
2 compilerparams. generateinmemory = false;
3 compilerparams. includedebuginformation = false;

In this way, compiler will be in the system temp (that is, c: \ Documents ents and Settings \ *** \ Local Settings \ Temp) directory when you execute the code. but when you create an application domain. 1 appdomain domain = appdomain. createdomain ("mynewdomain", null, null );

Then, when the generated assembly is loaded using this domain: 1 object test = domain. createinstancefromandunwrap (assembly. codebase, "test ");

Note: assembly is obtained after compilation.
However, the following code finds that: 1 methodinfo [] MIS = test. GetType (). getmethods ();
2 foreach (MethodInfo min in mis)
3 {
4 MessageBox. Show (min. Name );
5}

The output is not the result I want. Only the method of externalbyrefobject has no Click
TestMethod.
However, if I use the following code to load: 1 object test = Activator. CreateInstanceFrom (assembly. CodeBase, "Test"). Unwrap ();

You can get the correct result. This is a problem. We need to study 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.