. Net (C #): appdomain. docallback method and Lambda expression

Source: Internet
Author: User
Tags reflector

As we all know, the appdomain. docallback method will execute code in the specified application domain. If the input delegate is static, the Delegate will proceed normally. If the input delegate is a member function of the object, the object will be blocked (by value or by reference ).

When the input delegate is in the form of a Lambda expression, it is worth noting that.

If it is a Lambda expression that is passed in to capture external variables, the docallback method will always throw an exception and read the following code (this is an incorrect method to create a remoting object in another application domain)

Using system;

Using system. runtime. remoting;

 

Namespace mgen. TTC

{

Class A: marshalbyrefobject

{}

 

Class Program

{

Static void main ()

{

VaR appdom = appdomain. createdomain ("new ");

A obj = NULL;

Appdom. docallback () =>

{

OBJ = new ();

});

Console. writeline (remotingservices. istransparentproxy (OBJ ));

}

}

}

 

In fact, the program not only does not create an object A in the new application domain on the surface, but throws an exception:

Unhandled exception: system. runtime. serialization. serializationexception: type'

Mgen. TTC. Program + <> C _ displayclass1 'in assembly 'mgen, version = 1.0.0.0, culture

= Neutral, publickeytoken = NULL 'is not marked as serializable.

At system. appdomain. docallback (crossappdomaindelegate callbackdelegate)

 

Because lambda expressions are used to capture external variables, this Lambda will be compiled into a subclass nested in the program class, then, the local variable OBJ in the main function will be replaced with a field of the nested class. The execution of Lambda is a common method of the nested class.

The following is a nested class under reflector:

[Compilergenerated]

Private sealed class <> C _ displayclass1

{

// Fields

Public a obj;

 

// Methods

Public <> C _ displayclass1 ()

{

Base .. ctor ();

Return;

}

 

Public void <main> B _ 0 ()

{

This. OBJ = new ();

Return;

}

}

 

In this case, the object is still created in the local application domain. At the same time, the delegate passed in docallback is the method of the nested class generated by the compiler by Lambda. Therefore, the nested classes generated by this compiler cannot be enclosed by reference or sent by value, and an exception is thrown.

 

Of course, not all lambda expressions will be compiled into Nested classes, and Lambda without external variables will be directly compiled into static methods!

For example, the following code (to better describe Lambda in non-static member functions)

Class B

{

Public void doo ()

{

VaR appdom = appdomain. createdomain ("new ");

Appdom. docallback () => console. writeline ("hehe "));

}

}

 

Class Program

{

Static void main ()

{

New B (). Doo ();

}

}

The program runs smoothly (console. writeline is executed in another application domain ).

 

Source code of Class B under Reflector

Internal Class B

{

// Fields

[Compilergenerated]

Private Static crossappdomaindelegate CS $ <> 9 _ cachedanonymousmethoddelegate1;

 

// Methods

Public B ()

{

Base .. ctor ();

Return;

}

 

[Compilergenerated]

Private Static void <doo> B _ 0 ()

{

Console. writeline ("new reference ");

Return;

}

 

Public void doo ()

{

Appdomain appdom;

If (CS $ <> 9 _ cachedanonymousmethoddelegate1! = NULL)

{

Goto label_0024;

}

CS $ <> 9 _ cachedanonymousmethoddelegate1 = new crossappdomaindelegate (B. <doo> B _ 0 );

Label_0024:

Appdomain. createdomain ("new"). docallback (CS $ <> 9 _ cachedanonymousmethoddelegate1 );

Return;

}

}

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.