C #4.0 dynamic usage (1) -- use reflection

Source: Internet
Author: User

When designing the framework architecture, one of the headaches is that reflection is everywhere, but with C #4.0, dynamic can completely replace reflection, which makes me a little excited, immediately in vs2010, the first reflection in the log tracker frameworkCodeUpgrading to C #4.0 is not disappointing at all, and the code is much simpler.
Let's take a look at the code after replacing the reflection with dynamic:

1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5 Using System. reflection;
6 Using System. IO;
7 /* *******************************
8 * Updated by Lihua at 03/13/2009
9 *
10 * Update function:
11 * 1. Upgrade to C #4.0 and add dynamic to replace reflection.
12 * 2. If zivsoft. log. dll does not exist, logs are not output.
13 * 3. Project compilation does not depend on zivsoft. log. dll
14 *************************************** */
15 Namespace Zivsoft. Data
16 {
17 ///   <Summary>
18 /// Only classes in the persistent data layer framework are available.
19 ///   </Summary>
20 Internal   Class Logger
21 {
22 Private   Static Assembly _ assemblyfile;
23 Private   Static Dynamic _ logger;
24 Static Logger ()
25 {
26 VaR strdllfile = Appdomain. currentdomain. basedirectory +   " Zivsoft. log. dll " ;
27 If (File. exists (strdllfile ))
28 {
29 _ Assemblyfile = Assembly. LoadFile (strdllfile );
30 Try
31 {
32 _ Logger = _ Assemblyfile. createinstance ( " Zivsoft. log. Logger " , False , Bindingflags. createinstance, Null , Null , Null , Null );
33 }
34 Catch {
35 _ Logger =   Null ;
36 }
37 }
38 }
39
40 Public   Static   Void Loginfo ( String Message, Params   Object [] ARGs)
41 {
42 If ( Null   ! = _ Logger)
43 {
44 _ Logger. loginfo (message, argS );
45 }
46 }
47
48 Public   Static   Void Logwarning ( String Message, Params   Object [] ARGs)
49 {
50 If ( Null   ! = _ Logger)
51 {
52 _ Logger. logwarning (message, argS );
53 }
54 }
55
56 Public   Static   Void Logerror ( String Message, Params   Object [] ARGs)
57 {
58 If ( Null   ! = _ Logger)
59 {
60 _ Logger. logerror (message, argS );
61 }
62 }
63
64 Public   Static   Void Logdebug ( String Message, Params   Object [] ARGs)
65 {
66 If ( Null   ! = _ Logger)
67 {
68 _ Logger. logdebug (message, argS );
69 }
70 }
71
72 Public   Static   Void Logerror (exception E)
73 {
74 Logerror ( " {0} " , E );
75 }
76 }
77 }
78

 

The above is the entrance code for the persistent data layer to call the log tracker. I used reflection. I just changed it with dynamic. After debugging, there was no problem at all, so I was delighted, because many of my frameworks adopt reflection mechanisms that may be replaced by dynamic, such as the reflected sqlserver, MySQL, access and other databases in the persistent data layer framework.

I don't want to write much. Let's take a closer look.

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.