Methods of Assembly. createinstance () and activator. createinstance ()

Source: Internet
Author: User
Tags mscorlib

Methods of Assembly. createinstance () and activator. createinstance ()

Dynamic Creation of class objects, mostly activator. createinstance () and activator. the createinstance <t> () method is very easy to use and generally uses assembly. load ("assemblyname "). createinstance ("classname"); method, to study the difference between the two, in msdn, found the introduction of the two methods:

Assembly. createinstance method (string)

Use case-sensitive searches to find the specified type from this program, 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.

After reading it, I suddenly felt like I did not say anything. I don't know whether I have a problem with text comprehension or its expression.

So there is no way, so we have to use reflector to check the source code.

System. reflection. Assembly is located in mscorlib. dll. The source code of the createinstance () method is as follows:

 

System. activator is also located in mscorlib. dll

Public object createinstance (string typename, bool ignorecase, bindingflags bindingattr, binder, object [] ARGs, cultureinfo culture, object []

Activationattributes)
{
Type type1 = This. gettypeinternal (typename, false, ignorecase, false );
If (type1 = NULL)
{
Return NULL;
}
// Pay attention to this sentence, dizzy .... The activator. createinstance method is called here.
Return activator. createinstance (type1, bindingattr, binder, argS, culture, activationattributes );
}


The source code is as follows:

Public static object createinstance (type, bindingflags bindingattr, binder, object [] ARGs, cultureinfo culture, object [] activationattributes)
{
Object obj1;
If (type = NULL)
{
Throw new argumentnullexception ("type ");
}
If (type is typebuilder)
{
Throw new notsupportedexception (environment. getresourcestring ("notsupported_createinstancewithtypebuilder "));
}
If (bindingattr & (bindingflags) 0xff) = bindingflags. Default)
{
Bindingattr | = bindingflags. createinstance | bindingflags. Public | bindingflags. instance;
}
If (activationattributes! = NULL) & (activationattributes. length> 0 ))
{
If (! Type. isw.albyref)
{
Throw new notsupportedexception (environment. getresourcestring ("notsupported_activattronnonmbr "));
}
If (! Type. iscontextful & (activationattributes. length> 1) |! (Activationattributes [0] Is urlattrites )))
{
Throw new notsupportedexception (environment. getresourcestring ("notsupported_nonurlattronmbr "));
}
}
Try
{
Obj1 = (runtimetype) type. underlyingsystemtype). createinstanceimpl (bindingattr, binder, argS, culture, activationattributes );
}
Catch (invalidcastexception)
{
Throw new argumentexception (environment. getresourcestring ("arg_mustbetype"), "type ");
}
Return obj1;
}



A facade mode solves the problem, and the code of the system. activator. createinstance () method will be studied again next time. I will take a look at the facade.
========================================================== ========================================================== ===

By default, dalfactory encapsulates each layer in 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:
// Use the constructor with the highest degree of matching with the specified parameter to create an instance of the specified type
String Path = system. configuration. configurationsettings. receivettings ["Dal"];
String typename = path + ". dbobject"
Type objtype = type. GetType (typename, true );
Return (idbobject) activator. createinstance (objtype );

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.