Dynamic usage of c#4.0 (i.)--Using reflection skillfully

Source: Internet
Author: User
Tags reflection

In peacetime do frame architecture design, one of the headaches is everywhere to adopt reflection, but with c#4.0, found dynamic can replace reflection, this feature makes me a little excited, immediately in the VS2010 log tracker frame of the first reflection of the code to upgrade to C # 4.0, the results are not disappointing at all, the code simplifies a lot.

Let's take a look at the code that replaces 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 in 03/13/2009


9 *


10 * Update function:


11 * 1. Upgrade to c#4.0, add dynamic instead of reflection


12 * 2. If Zivsoft.Log.dll does not exist, the log does not output


13 * 3. Project compilation does not depend on Zivsoft.Log.dll


14 ****************************************/


namespace Zivsoft.data


16 {


///<summary>


18///only provides class usage in a persistent data-tier framework


///</summary>


Internal class Logger


21 {


private static Assembly _assemblyfile;


private static dynamic _logger;


static Logger ()


25 {


var strdllfile = AppDomain.CurrentDomain.BaseDirectory + "Zivsoft.Log.dll";


if (file.exists (strdllfile))


28 {


_assemblyfile = Assembly.loadfile (strdllfile);


Try


31 {


_logger = _assemblyfile.createinstance ("Zivsoft.Log.Logger", False, Bindingflags.createinstan CE, NULL, NULL, NULL, NULL);


33}


Catch {


_logger = null;


36}


37}


38}


39


public static void Loginfo (String message, params object[] args)


41 {


-if (null!= _logger)


43 {


_logger. Loginfo (message, args);


45}


46}


47


public static void Logwarning (String message, params object[] args)


49 {


if (null!= _logger)


51 {


_logger. logwarning (message, args);


53}


54}


55


public static void Logerror (String message, params object[] args)


57 {


(null!= _logger)


59 {


_logger. Logerror (message, args);


61}


62}


63


public static void Logdebug (String message, params object[] args)


65 {


if (null!= _logger)


67 {


_logger. Logdebug (message, args);


69}


70}


71


, public static void Logerror (Exception e)


73 {


Logerror ("{0}", e);


75}


76}


77}


78

The above is the entry code for the persistent data layer call log tracker, used to reflect, just I changed with the dynamic over, debugging a little problem, so it makes me happy, because the next many of my frameworks using reflection mechanism will be able to be replaced by dynamic, such as "persistent data layer framework" A database of reflected SQL Server, MySQL, access, and so on.

Do not write more, everyone carefully understand it.

Related Article

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.