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.