[You must know. NET] The 23rd time: taste details, in-depth. NET's type Builder

Source: Internet
Author: User
Tags class definition constructor

1 Introduction

Today, Artech on the question of type initializer and beforefieldinit, and see if you can give us the correct explanation, we know an interesting example of the execution of a type constructor call. In this paper, we also propose some discussion about the timing of beforefieldinit for type constructors, which is a good practical experience for us to understand the type builder.

As a supplement, this article hopes to start from the basis of further layers, the "questions about type initializer and beforefieldinit, see if we can give the correct explanation" in the article does not explain the concept and principle, to carry out the necessary supplements, such as a more comprehensive understanding of type Builder, Know BeforeFieldInit. On this basis, we discuss a bit about the practical application of the type constructor and expect to be able to answer the results of the example run.

Cut the crap, we start.

2 Understanding object Constructors and type constructors

In. NET, the initialization process for a class is done in the constructor. And depending on the type of the constructed member, is divided into type constructors (. cctor) and Object Constructors (. ctor), where. cctor and. ctor are represented by the two directives in the IL code. cctor can not be directly called, its calling rules is the focus of this article, see the analysis of the following text , and. ctor is invoked automatically when the type is instantiated.

Based on the discussion of type constructors, it is necessary to first implement a simple class definition, which includes ordinary constructors and static constructors, such as

// Release : code01, 2008/11/02

   // Author : Anytao, http://www.anytao.com

   public class User

   {

     static User()

     {

       message = "Initialize in static constructor.";

     }

     public User()

     {

       message = "Initialize in normal construcotr.";

     }

     public User(string name, int age)

     {

       Name = name;

       Age = age;

     }

     public string Name { get; set; }

     public int Age { get; set; }

     public static string message = "Initialize when defined.";

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.