c# 程序集

动态编译程序集

代码:

public class DynamicCode
{
    public const string SourceCode = 
        "using System;"+
        "public static class TestWrite"+
        "{"+
            "public static void Run()"+
            "{"+
                "Console.WriteLine(\"Holle World\");"+
            "}"+
        "}";

    /// <summary>
    /// 编译且运行
    /// </summary>
    public void CompilationAndRun()
    {
        CompilerResults compilerResults = null;

        using (CSharpCodeProvider csharpCodeProvider =  new CSharpCodeProvider())
        {
            CompilerParameters options = new CompilerParameters();
            options.GenerateInMemory = true;

            // 通过源码编译程序集
            compilerResults = csharpCodeProvider.CompileAssemblyFromSource(options, SourceCode);
        }

        if (compilerResults.Errors.HasErrors == false)
        {
            // 获取编译程序集的类
            Type testWriteType = compilerResults.CompiledAssembly.GetType("TestWrite");
            // 反射调用类
            testWriteType.InvokeMember("Run", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, null);
        }
    }
}

应用程序域

在这里插入图片描述

一个进程可以有多个程序域,一个程序域可以加载多个程序集

示例代码:

static void Main(string[] args)
{
    // 创建程序域
    AppDomain appDomain = AppDomain.CreateDomain("TestAppDomain");

    // 执行程序集,这里会执行Main()方法
    appDomain.ExecuteAssembly("AssemblyPart.exe");

    // 获取程序集AssemblyPart的DynamicCode类的实例
    DynamicCode dynamicCode = (DynamicCode) appDomain.CreateInstanceAndUnwrap("AssemblyPart", "AssemblyPart.DynamicCode");
    dynamicCode.CompilationAndRun();

    // 卸载程序域
    AppDomain.Unload(appDomain);
}

跨域执行类需要类继承MarshalByRefObject,如下:

public class DynamicCode:MarshalByRefObject

版本号

版本号格式:
<主版本>.<次版本>.<内部版本>.<修订版本>

获取版本信息:
Assembly.GetExecutingAssembly().FullName;

共享程序集

共享程序集
将程序集安装到windows共享程序集缓存空间中,客户端需要加载时从共享程序集缓存空间中加载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值