问题是这样的, 一个人想在发布自己项目的时候 不用发布它所引用的程序集.
我想 办法是这样的: 把程序集embed到自己的项目中再通过reflection机制来调用改程序集的方法啊 类啊 数据啊 什么的.
下面是一个例子.
-----------------ClassLibrary1.dll---------------------
namespace ClassLibrary1
{<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
public class Class1
{
public void Show()
{
Console.WriteLine("Class1.Show is called...");
}
}
}
把上面的dll加到下面的项目中来并且把Build Action 设成 Embeded Resources
------------------Console app------------------
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Assembly dll;
Assembly a = Assembly.GetExecutingAssembly();
byte[] buffer=new byte[40480];//大小因程序集而异
Stream stream = a.GetManifestResourceStream("ConsoleApplication1.ClassLibrary1.dll");
int j = stream.Read(buffer, 0, buffer.Length);
if (j > 0)
{
dll = Assembly.Load(buffer);
Type[] t = dll.GetTypes();
foreach (Type tt in t)
{
if (tt.Name == "Class1")
{
object class1= Activator.CreateInstance(tt);
MethodInfo[] ms = tt.GetMethods();
foreach (MethodInfo m in ms)
{
if (m.Name == "Show")
m.Invoke(class1, null);
}
}
} }
Console.Read();
}
}
}
此方法因为要把整个assembly导入内存,所以 如果程序集太大显然就不行了...
转载于:https://blog.51cto.com/feishunji/45034