C # advanced Dev with system. reflection. emit. ilgenerator from Microsoft

Source: Internet
Author: User

/* ===================================================== ========================================

File: typeresolve. CS

Summary: This file implements "type resolution" sample. This sample
Demonstrates how hosts can participate in the type resolution
Process by supplying and event handler that returns an assembly
Containing the requested type.

---------------------------------------------------------------------
This file is part of the Microsoft. NET Framework SDK code samples.

Copyright (c) Microsoft Corporation. All rights reserved.

This source code is intended only as a supplement to Microsoft
Development tools and/or on-line documentation. See these other
Materials for detailed information regarding Microsoft code samples.

This code and information are provided "as is" without warranty of any
Kind, either expressed or implied, including but not limited to
Implied warranties of merchantability and/or fitness for
Particle purpose.
========================================================== ======================================= */

Using system;
Using system. reflection;
Using system. reflection. emit;
Using system. Threading;
Using system. runtime. remoting;

Class app {
Static Assembly typeresolvehandler (Object sender, resolveeventargs e ){
Console. writeline ("in typeresolvehandler ");

Assemblyname = new assemblyname ();
Assemblyname. Name = "dynamicassem ";

// Create a new assembly with one module
Assemblybuilder newassembly =
Thread. getdomain (). definedynamicassembly (assemblyname, assemblybuilderaccess. Run );
Modulebuilder newmodule = newassembly. definedynamicmodule ("dynamicmodule ");

// Define a public class named "anonexistenttype" in the Assembly.
Typebuilder mytype = newmodule. definetype ("anonexistenttype", typeattributes. Public );

// Define a method on the type to call
Methodbuilder simplemethod = mytype. definemethod ("simplemethod", methodattributes. Public, null, null );
Ilgenerator IL = simplemethod. getilgenerator ();
Il. emitwriteline ("method called in anonexistenttype ");
Il. emit (Opcodes. Ret );

// Bake the type
Mytype. createtype ();

Return newassembly;

}

Static void main (){
// Hook up the event handler
Thread. getdomain (). assemblyresolve + = new resolveeventhandler (App. typeresolvehandler );

// Find a type that shocould be in our Assembly but isn' t
Objecthandle Oh = activator. createinstance ("dynamicassem", "anonexistenttype ");

Type Mt = Oh. Unwrap (). GetType ();

// Construct an instance of a Type
Object objinstance = activator. createinstance (MT );

// Find a method in this type and call it on this object
Methodinfo MI = Mt. getmethod ("simplemethod ");
Mi. Invoke (objinstance, null );
Console. Read ();
}
}

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.