技术开发 频道

C#调用VS生成的DLL

 【IT168技术文档】

    使用Visual Studio 2005创建一个类库,并且生成一个dll文件,如A.dll,文件名存放到数据库

 在新建一个应用程序,后台代码里获取到这个文件名之后,有没有可以这样调用里面的方法和类。

 首先新建一个类库,将它生成ClassLibrary1.dll

 view plaincopy to clipboardprint?

 using System;

 using System.Collections.Generic;

 using System.Text;

 namespace WebTest

 {

 public class ReflectTest

 {

 private string word;

 public String Word

 {

 get

 {

 return word;

 }

 set

 {

 word = value;

 }

 }

 public ReflectTest(string w)

 {

 this.word = w;

 }

 public ReflectTest()

 {

 this.word = "word";

 }

 public string WriteString(String s)

 {

 return "Welcome + " + s;

 }

 public static string WriteStatic(String name)

 {

 return "Static : Welcome + " + name;

 }

 public string WriteNoPara()

 {

 return "NoPara : Welcome !";

 }

 private String WritePrivate()

 {

 return "Private ";

 }

 }

 public class TestClass

 {

 }

 }

 然后新建立一个项目以入该dll(代码里有e文注释)

 view plaincopy to clipboardprint?

 using System;

 using System.Collections.Generic;

 using System.Text;

 using System.Reflection;

 namespace TestReflect

 {

 class Program

 {

 static void Main(string[] args)

 {

 System.Reflection.Assembly ass;

 Type type;

 Object obj;

 try

 {

 ass = Assembly.Load("ClassLibrary1");

 Console.WriteLine("-----------All the class In this dll-----------");

 foreach (Type t in ass.GetTypes())

 {

 Console.WriteLine(t.Name);

 }

 Console.WriteLine("---------All the Module In this dll----------");

 Module[] miar = ass.GetModules();

 foreach (Module m in miar)

 {

 Console.WriteLine(m.Name);

 }

 type = ass.GetType("WebTest.ReflectTest");

 Console.WriteLine("-----------All the method In WebTest.ReflectTest-----------");

 MethodInfo[] miarr = type.GetMethods();

 foreach (MethodInfo m in miarr)

 {

 Console.WriteLine(m.Name);

 }

 Console.WriteLine("-----------All the Property In WebTest.ReflectTest-----------");

 PropertyInfo[] piarr = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

 foreach (PropertyInfo p in piarr)

 {

 Console.WriteLine(p.Name);

 }

 Console.WriteLine("----------Call some methods in WebTest.ReflectTest-----------");

 obj = ass.CreateInstance("WebTest.ReflectTest");

 MethodInfo method = type.GetMethod("WriteString");

 String s = (String)method.Invoke(obj, new string[] { "WriteString" });

 Console.WriteLine(s);

 method = type.GetMethod("WriteStatic");

 s = (String)method.Invoke(null, new string[] { "WriteName" });

 Console.WriteLine(s);

 method = type.GetMethod("WriteNoPara");

 s = (String)method.Invoke(obj, null);

 Console.WriteLine(s);

 method = type.GetMethod("WritePrivate", BindingFlags.Instance | BindingFlags.NonPublic);

 s = (String)method.Invoke(obj, null);

 Console.WriteLine(s);

 Console.WriteLine("---------Use the Property---------");

 PropertyInfo pi = type.GetProperty("Word");

 pi.SetValue(obj, "Word", null);

 Console.WriteLine(pi.GetValue(obj,null));

 Console.WriteLine("--------- Get All Constructor ----------");

 ConstructorInfo[] ci = type.GetConstructors();

 foreach (ConstructorInfo c in ci)

 {

 Console.WriteLine(c.ToString());

 }

 Console.WriteLine("-------- Construct --------");

 ConstructorInfo ci1 = type.GetConstructor(new Type[] { typeof(string) });

 WebTest.ReflectTest wr1 = (WebTest.ReflectTest)ci1.Invoke(new string[] { "XXX" });

 Console.WriteLine(wr1.Word);

 ConstructorInfo ci2 = type.GetConstructor(new Type[0] );

 WebTest.ReflectTest wr2 = (WebTest.ReflectTest)ci2.Invoke(null);

 Console.WriteLine(wr2.Word);

 }

 catch (Exception ex)

 {

 Console.WriteLine(ex);

 }

 finally

 {

 ass = null;

 type = null;

 obj = null;

 }

 }

 }

 }
 

 

0
相关文章