【问题标题】:Read the value of a string in the dll that is created dynamicly读取动态创建的dll中字符串的值
【发布时间】:2014-09-18 12:32:11
【问题描述】:

我正在尝试在运行时创建一个 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。我该怎么做?

最好的问候。任何想法将不胜感激。

【问题讨论】:

  • 加载程序集并使用反射?
  • 哇,我该怎么做?!!!!!!
  • 反射是您可以询问对象的地方,例如获取属性和方法。调用没有对已构建程序集的引用的东西的唯一方法。

标签: c# dll .net-assembly


【解决方案1】:

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

    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

【讨论】:

  • 谢谢,但是我怎样才能访问返回的值呢?
  • return myMethod.Invoke(obj, null);?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 2022-01-19
  • 2013-11-20
  • 1970-01-01
  • 2019-02-01
  • 2012-02-15
相关资源
最近更新 更多