1.在写一个记录日志到文件中的类库(生成dll文件copy到一个目录中去,然后在主函数的appconfig中去配置。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WriteToTxtLib
{
public class WriteLog
{
public void Write(string logMessage)
{
using (StreamWriter sw = new StreamWriter("e:\\test\\writelog.txt",true))
{
sw.WriteLine(DateTime.Now.ToLongDateString() + logMessage);
}
}
}
}

2.新建一个测试的主函数,其中appconfig进行如下配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="dllNamespaceName" value="WriteToWordLib"/>
<add key="dllClassName" value="WriteLog"/>
<add key="dllMethodName" value="Write"/>
<add key="dllPath" value="e:\test\WriteToWordLib.dll"/> </appSettings>
</configuration>

3.给主函数添加如下代码

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Management.Instrumentation;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Collections;
using System.Configuration; namespace 我的日志功能函数
{
class Program
{
static void Main(string[] args)
{
WriteLog("主函数开始");
Console.ReadKey();
} private static void WriteLog(string logMessage)
{
/*
* 这是appconfig文件中的指定的内容
<add key="dllNamespaceName" value="WriteToTxtLib"/>
<add key="dllClassName" value="WriteLog"/>
<add key="dllMethodName" value="Write"/>
<add key="dllPath" value="e:\\test\WriteToTxtLib.dll"/>
*/
//获取到命名空间,类名,方法名,dll路径
string dllNamespaceName = System.Configuration.ConfigurationSettings.AppSettings["dllNamespaceName"].ToString();
string dllClassName = System.Configuration.ConfigurationSettings.AppSettings["dllClassName"].ToString();
string dllMethodName = System.Configuration.ConfigurationSettings.AppSettings["dllMethodName"].ToString();
string dllPath = System.Configuration.ConfigurationSettings.AppSettings["dllPath"].ToString(); Assembly asm = Assembly.LoadFile(dllPath);//加载dll
Type classType = asm.GetType(dllNamespaceName + "." + dllClassName);//获取类型
object obj = Activator.CreateInstance(classType);//根据类型创建对象
MethodInfo method = classType.GetMethod(dllMethodName);//找到指定的方法
object o = method.Invoke(obj, new object[] { logMessage });//调用方法
}
}
}

c#动态加载dll文件的更多相关文章

  1. c# 如何进行动态加载dll

    最近遇到了在c#中如何进行动态加载dll的话,搞定了,下面介绍一下自己的步骤. 1,新建dll. 打开vs,新建project->Class Library->项目名为testdll1.在 ...

  2. Delphi静态加载DLL和动态加载DLL示例

    下面以Delphi调用触摸屏动态库xtkutility.dll为例子,说明如何静态加载DLL和动态加载DLL. 直接上代码. 1.静态加载示例 unit Unit1; interface uses W ...

  3. C# 利用反射动态加载dll

    笔者遇到的一个问题,dll文件在客户端可以加载成功,在web端引用程序报错.解决方法:利用反射动态加载dll 头部引用加: using System.Reflection; 主要代码: Assembl ...

  4. c#实现动态加载Dll(转)

    c#实现动态加载Dll 分类: .net2009-12-28 13:54 3652人阅读 评论(1) 收藏 举报 dllc#assemblynullexceptionclass 原理如下: 1.利用反 ...

  5. unity3d动态加载dll的API以及限制

    Unity3D的坑系列:动态加载dll 一.使用限制 现在参与的项目是做MMO手游,目标平台是Android和iOS,iOS平台不能动态加载dll(什么原因找乔布斯去),可以直接忽略,而在Androi ...

  6. C#,动态加载DLL,通过反射,调用参数,方法,窗体

    .net中常会用到动态加载DLL,而DLL中可能包含各种参数.方法.窗体,如何来调用动态加载这些参数.方法.窗体呢? 在C#中,我们要使用反射,首先要搞清楚以下命名空间中几个类的关系: System. ...

  7. c#实现动态加载Dll

    原文:c#实现动态加载Dll 原理如下: 1.利用反射进行动态加载和调用. Assembly assembly=Assembly.LoadFrom(DllPath); //利用dll的路径加载,同时将 ...

  8. 动态加载dll的实现+远线程注入

    1.在目标进程中申请内存 2.向目标进程内存中写入shellcode(没有特征,编码比较麻烦) 3.创建远线程执行shellcode 之前可以看到shellcode很难编写还要去依赖库,去字符串区等等 ...

  9. Java_Java中动态加载jar文件和class文件

    转自:http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...

随机推荐

  1. 最全的PHP常用函数大全

    PHP的一些常用函数 quotemeta() 函数在字符串中某些预定义的字符前添加反斜杠. quoted_printable_decode() 函数对经过 quoted-printable 编码后的字 ...

  2. js如何判断一个对象是不是Array

    typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 var arr=new Array(&quo ...

  3. Kafka源码中的Producer Record定义

    1.ProducerRecord 含义: 发送给Kafka Broker的key/value 值对 2.内部数据结构: -- Topic (名字) -- PartitionID ( 可选) -- Ke ...

  4. 基于Flume的美团日志收集系统(二)改进和优化

    在<基于Flume的美团日志收集系统(一)架构和设计>中,我们详述了基于Flume的美团日志收集系统的架构设计,以及为什么做这样的设计.在本节中,我们将会讲述在实际部署和使用过程中遇到的问 ...

  5. [Warning] TIMESTAMP with implicit DEFAULT value is deprecated

    As indicated by the warning, to turn off the nonstandard behaviors, enable the new explicit_defaults ...

  6. unity3d 破解安装

    1.下载破解程序,执行生成unity_v4.x.ulf文件 2.断网 3.执行unity客户端,load该lisence文件即可 注意:安装unity客户端完成后,未破解,切记别打开unity客户端 

  7. init: sys_prop: permission denied uid:1003 name:service.bootanim.exit

    /************************************************************************* * init: sys_prop: permiss ...

  8. php 换行 PHP_EOL变量

    一个小小的换行,其实在不同的平台有着不同的实现,为什么要这样,可以是世界是多样的. 本来在unix世界换行就用/n来代替,但是windows为了体现他的不同,就用/r/n,更有意思的是在mac中用/r ...

  9. kdtree备份

    库在这里 这个很好用. 例子: /*! gcc -Wall -g -o test test.c libkdtree.a */ #include <stdio.h> #include < ...

  10. UVa10603 Fill

    解题思路:这是神奇的一题,一定要好好体会.见代码: #include<cstdio> #include<cstring> #include<algorithm> # ...