Excerpt from-it Enterprise must read 200. NET face questions-06. The advanced features in net

Source: Internet
Author: User
Tags emit

Commissioned

Q: Please explain the rationale of the delegation

A delegate is a class of types that inherit from System.Delegate, and each delegate object contains at least one pointer to a method, and the delegate implements the mechanism of the callback method.

Q: The difference between a delegate callback static method and an instance method

When a delegate binds a static method, the internal object member variable _target is set to null, and when the delegate binds an instance method, _target sets the exponentially to an instance object of the type to which the instance method belongs, which is used to invoke the instance method when the delegate is executed.

Q: What is a chained delegate

A chained delegate is a linked list that is strung by a delegate, and subsequent delegates to that delegate on all linked lists are executed sequentially when a delegate on the list is called back.

Q: What is the order of execution of chained delegates

Chained delegates are executed sequentially from the current delegate, starting with the order on the delegate chain. If necessary, the Getinvocationlist () method can be used to obtain the delegates needed to execute on the delegate chain and execute them in the desired order.

Q: Can I define a delegate chain for a method that has a return value

A delegate can be a method with a return value, but when more than one method with a return value is added to the delegate chain, the programmer needs to manually invoke each method on the delegate chain, or the delegate consumer can only get the return value of the last executed method on the delegate chain.

Q: Where can a delegate usually be applied?

The delegate's application is usually the task's executor to redistribute the details, and the performer knows exactly what work is going to be executed, but delegates execution details to other components, methods, or assemblies.

Event

Q: Please explain the basic use of the event

An event is a member that enables an object or class to provide notification. The client can add executable code for the corresponding event by providing an event handler. An event is a special kind of delegate.

Q: What is the connection between the event and the delegate

An event is a delegate type that has no return value for a method of the delegate type and has two parameters: Object and Teventarg. The subscription and cancellation of events are based on the list of delegates.

Q: How to design a type with a lot of events

When a type contains more events, you can consider centralizing the delegate list of all events in a collection, which effectively reduces the size of the object because there are few cases in which an object uses all events. NET built-in type System.ComponentModel.EventHandlerList provides the encapsulation of such a collection of storage events.

Q: The following scenario is represented in code: Cat Barking, Mouse escape, master awakened

A simple example is given below:

Definition of CAT:

 Public classCat {Private string_name;  Public EventEventhandler<catcryeventargs>catcryevent;  PublicCat (stringname) {_name=name; }         Public voidcatcry () {Catcryeventargs args=NewCatcryeventargs (_name);            Console.WriteLine (args); Catcryevent ( This, args); }    }

The event parameter definition:

 Public classCatcryeventargs:eventargs {Private string_catname;  PublicCatcryeventargs (stringcatname):Base() {_catname=catname; }         Public Override stringToString () {return_catname +"called the"; }    }

Definition of the mouse:

 Public classMouse {Private string_name;  PublicMouse (stringname, Cat cat) {_name=name; Cat. Catcryevent+=cat_catcryevent; }        voidCat_catcryevent (Objectsender, Catcryeventargs e)        {Run (); }        Private voidRun () {Console.WriteLine ("Mouse"+ _name +"run away."); }    }

The definition of the master:

 Public classMaster {Private string_mastername;  PublicMaster (stringname, Cat cat) {_mastername=name; Cat. Catcryevent+=cat_catcryevent; }        voidCat_catcryevent (Objectsender, Catcryeventargs e)        {WakeUp (); }        Private voidWakeUp () {Console.WriteLine ("Master"+ _mastername +"Wake up"); }    }

Code to run:

Console.WriteLine ("Start Simulation"); Cat Cat=NewCat ("Tom Cat"); Mouse M1=NewMouse ("Mouse No. 1th", cat); Mouse m2=NewMouse ("Mouse No. 2nd", cat); Master Master=NewMaster ("Jack", cat); Cat.            Catcry (); Console.ReadLine ();
Reflection

Q: Please explain the fundamentals of reflection and the cornerstone of its implementation

Reflection is a mechanism for dynamically analyzing target objects such as assemblies, modules, types, and fields, and its implementation is based on meta-data. Metadata is a block of data stored in a PE file that details the structure, reference types, assemblies, and manifests within an assembly or module.

Q:. NET what types are available to implement reflection

Under the System.Reflection namespace,. NET provides a rich type of implementation reflection mechanism.

Q: How to implement a dynamically launched Assembly

Under the System.Reflection.Emit namespace, you define a series of types that are used to dynamically emit intermediate code that can be used to implement the ability to dynamically create assemblies. The generated intermediate code is written directly into memory, not in the form of code residing on the hard disk.

Q: How to use reflection to implement Factory mode

Using reflection allows for a more flexible factory model, the key is to dynamically find all the parts contained in the product, instead of having to analyze the user's needs through code. The reflective factory model is characterized by high flexibility and low operating efficiency.

Q: How to save the type, field, and method information at a small memory cost

The System.runtimetypehandle, System.runtimemethodhandle, and system.runtimefieldhandle three types contain a pointer to the description of the type, method, and field, respectively. Saving pointers is a useful way to reduce memory consumption by replacing information-describing objects that hold the entire type, method, and field.

Characteristics

Q: What is an attribute and how to customize an attribute

A feature is a special mechanism for declarative programming, which is a family of types that inherit from System.Attribute, and at compile time, the use of the attribute is written to the metadata for use by the runtime or reflection in the program.

Q:. What elements can be used by attributes in net

The AttributeUsage feature allows you to limit the scope of your custom attributes

Q: What are some ways to know if an element declares an attribute

Excerpt from-it Enterprise must read 200. NET face questions-06. The advanced features in net

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.