C# dll作为文件资源嵌入到Resource

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace WindowsFormCaller
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            AppDomain.CurrentDomain.AssemblyResolve += 
            delegate(object sender, ResolveEventArgs senderArgs)
            {
                //Console.WriteLine("AssemblyResolve...!!!");
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                string name = new AssemblyName(executingAssembly.FullName).Name;
                string str2 = new AssemblyName(senderArgs.Name).Name;
                string str3 = name + "." + str2 + ".dll";
                //Console.WriteLine("!executingAssembly.FullName={0},{1}", executingAssembly.FullName, str3);
                using (Stream stream = executingAssembly.GetManifestResourceStream(str3))
                {
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    return Assembly.Load(buffer);
                }
            };

            //InnerMath.Math.SayHi();
            Application.Run(new Form1());
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InnerMath;

namespace WindowsFormCaller
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InnerMath.Math.SayHi();
        }
    }
}

 

posted @ 2017-03-29 10:30  sky20080101  阅读(840)  评论(0)    收藏  举报