Differences between the Assembly. createinstance () and activator. createinstance () Methods

Source: Internet
Author: User
Tags mscorlib

Differences between the Assembly. createinstance () and activator. createinstance () Methods

Er ''recently used activator. createinstance () and activator. createinstance <t>
(), Very easy to use, but when looking at the source code of many others, most of them use
Assembly. Load ("assemblyname"). createinstance ("classname"); method, suddenly want to study the two
What is the difference? Open msdn and find 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
;

}




//
Note that this sentence is 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
Urlattribute )))



{


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.

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.