• c# 动态生成程序集dll


    动态生成程序集,指定版本号,如果需要引用程序集,可以添加一个类的代码,可以是个空类,也可以将一些文件嵌入到dll里。

    CSharpCodeProvider codeProvider = new CSharpCodeProvider();
                    System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
                    parameters.ReferencedAssemblies.Add(@"System.dll");
                    parameters.GenerateExecutable = false;
                    parameters.GenerateInMemory = false;
                    parameters.OutputAssembly = appStartupPath + "\\Task\\" + $"{projectAssembleName}.dll";
    
    
                    parameters.EmbeddedResources.Add(zipFileName);
    
                    CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, @"
    using System;
    using System.Reflection;
    [assembly: AssemblyVersion(""1.0.0.0"")] 
    [assembly: AssemblyFileVersion(""1.0.0.0"")]
    
    namespace " + projectAssembleName + @"
        {
            public class Referenced
            {
            }
        }");

    如果程序需要引用该程序集,或者需要获取该程序集可以先引用一个类,这样程序集就会加载到主程序域中,如果只是引用了程序集,代码里没有引用程序集的元数据,程序集不会加载到主程序域中

     Referenced project = new Referenced();//引用任务dll,加载任务程序集

    可以通过这种方式获取引用的程序集

     Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(m => m.FullName == $"{ProjectAssembleName}, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
  • 相关阅读:
    OpenLayers测量距离和面积
    BeanUtils接口和类
    深入理解Spring--动手实现一个简单的SpringIOC容器
    Spring:源码解读Spring IOC原理
    Spring AOP的底层实现原理
    ClassLoader工作机制
    Cglib及其基本使用
    InvocationHandler和Proxy(Class)的动态代理机制详解
    解密Redis的持久化和主从复制机制
    Redis之父九条编程忠告
  • 原文地址:https://www.cnblogs.com/njcxwz/p/15932570.html
Copyright © 2020-2023  润新知