Differences between the Assembly. createinstance () and activator. createinstance () methods in. Net reflection

Source: Internet
Author: User

Personal Understanding: in short, assembly. createinstance is an encapsulated activator. createinstance method. The former is more secure, and the latter is more widely used.

Assembly. createinstance method (string)

Use case-sensitive searches.ProgramSearch for the specified type, and then use the system activator to create its instance.

Activator. createinstance method (type)

Create an instance of the specified type using the constructor with the highest degree of matching with the specified parameter.

By default, dalfactory encapsulates each layer into an assembly (Independent Project

) Component. Creates an object instance through the reflection mechanism.

//Create an object instance from an assembly


String Path = system. configuration. configurationsettings. receivettings ["Dal"]; // The Assembly name at the data layer.
Return (idbobject) Assembly. Load (PATH). createinstance (path + ". dbobject ");

If your data layer is not a separate assembly, you can load it as follows:
//Create an instance of the specified type using the constructor with the highest degree of matching with the specified parameter


String Path = system. configuration. configurationsettings. receivettings ["Dal"];
String typename = path + ". dbobject"
Type objtype = type. GetType (typename, true );
Return (idbobject) activator. createinstance (objtype );

Reference: http://www.cnblogs.com/SAL2928/archive/2009/04/27/1444761.html

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.