读取动态创建的dll中字符串的值

时间:2014-09-18 12:32:11

标签: c# dll .net-assembly

我正在尝试在运行时创建一个DLL文件,事实上我需要将编码数据保存到DLL。我的代码是这样的:

      class DllFile
    {
        public static void CreateDllFile(string source)
        {
            source = @"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LicensecodeDll
{
    class Check
    {
        public string returnValue()
        {
            return " + source + ";" + "}}}";



            var provider = new CSharpCodeProvider();
            var options = new CompilerParameters
            {
                OutputAssembly = "test.dll"
            };

            var results = provider.CompileAssemblyFromSource(options, new[] { source });

        }

    }
}

每件事都运行正常,我的ddl已创建,但我需要读取保存在dll文件中的值,我的意思是我需要returnValue。我该怎么做?

致以最诚挚的问候。任何想法都将受到赞赏。

1 个答案:

答案 0 :(得分:2)

您可以动态加载程序集并使用反射来调用该方法。代码应该是这样的。

    Assembly a = Assembly.Load("test.dll");        
    Type myType = a.GetType("LicensecodeDll.Check");        
    MethodInfo myMethod = myType.GetMethod("returnValue");        
    object obj = Activator.CreateInstance(myType);       
    myMethod.Invoke(obj, null);

有关详细信息,请访问MSDN:如何:Load Assemblies into an Application Domain