Cross-assembly reflection (continued)

Source: Internet
Author: User
I have previously written a blog titled "cross-assembly reflection". The method used for cross-assembly loading is to first load the assembly of the target type, use Assembly again. the GetType method obtains the type. Later, Luna's comment said that type can be used directly. getType ("<fully qualified Class Name>, <Assembly>") to load the type. I checked msdn and mentioned this in msdn, I remember this solution. However, when this method is used for type loading across assemblies, null is always returned. Is msdn incorrect? I don't know. (Hope you can tell me what you know ).

In spring.net, configuration files usually contain configuration items similar to the following: <object name = "datastreamhelper" type = "datacenterbase. Common. datastreamhelper, datacenterbase"/>

The value of "type" in the configuration is exactly the same as the parameter format of the type. GetType method. So I guess spring.net uses the type. GetType method. I checked the source code of spring.net. Unexpectedly, spring.net did not use the type. GetType shortcut, but still used the Assembly. GetType method. I don't know why.

For future convenience, I encapsulated a static method to support any type of loading. 1 # region GetType
2 // assemblyname does not require an extension. If the target type is in the current Assembly, assemblyname is null.
3 Public static type GetType (string typefullname, string assemblyname)
4 {
5 If (assemblyname = NULL)
6 {
7 return type. GetType (typefullname );
8}
9
10 // search for the Assembly loaded in the current domain
11 Assembly [] asses = appdomain. currentdomain. getassemblies ();
12 foreach (Assembly ass in asses)
13 {
14 string [] names = ass. fullname. Split (',');
15 if (Names [0]. Trim () = assemblyname. Trim ())
16 {
17 return ass. GetType (typefullname );
18}
19}
20
21 // load the Target Assembly
22 Assembly tarassem = assembly. loadwithpartialname (assemblyname );
23 if (tarassem! = NULL)
24 {
25 return tarassem. GetType (typefullname );
26}
27
28 return NULL;
29}
30 # endregion

Who has successful experience in loading non-current Assembly types using type. GetType? Thanks for sharing.

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.