C#使用程序集调用DLL中的方法 [英] C# Using Assembly to call a method within a DLL

查看:146
本文介绍了C#使用程序集调用DLL中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读了很多关于这个 - 我觉得我非常接近答案。我只是想从我创建的dll文件中调用一个方法。



例如:





我的DLL文件:



 code> using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;

命名空间ExampleDLL
{
类程序
{
static void Main(string [] args)
{
系统。 Windows.Forms.MessageBox.Show(参数[0]);
}

public void myVoid(string foo)
{
System.Windows.Forms.MessageBox.Show(foo);
}
}
}


>


我的应用程序:



  string filename = @ C:\Test.dll; 
装配SampleAssembly;
SampleAssembly = Assembly.LoadFrom(filename);
//获取对程序集中已知存在的方法的引用。
MethodInfo Method = SampleAssembly.GetTypes()[0] .GetMethod(myVoid);
//获取对MethodInfo实例的参数集合的引用。

所有信用转到上述代码段的用户woohoo em>
如何调用托管DLL文件C#?



尽管如此,我希望能够引用我的Dll(和它里面的方法)调用其中的方法(在这种情况下,我想调用方法'myVoid')。



任何人都可以有任何建议吗?



谢谢,



Evan

解决方案

p>您提到的问题和答案是使用反射来调用受管DLL中的方法。这不是必需的,如果你说你想做的,你只需要引用你的DLL。添加引用(通过Visual Studio中的添加参考选项),您可以直接调用方法:

  ExampleDLL。程序p = new ExampleDLL.Program(); //获取一个Program
p.myVoid()的实例; //调用方法`myVoid`

如果你想去反射路由(由 woohoo ),您仍然需要一个程序类的实例。



汇编SampleAssembly = Assembly.LoadFrom(filename);

  
键入myType = SampleAssembly.GetTypes()[0];
MethodInfo Method = myType.GetMethod(myVoid);
对象myInstance = Activator.CreateInstance(myType);
Method.Invoke(myInstance,null);

现在您有一个程序的实例,可以调用 myVoid


I have been reading a lot about this - I feel like I'm very close to the answer. I am simply looking to call a method from within a dll file that I have created.

For example purposes:

My DLL File:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExampleDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Windows.Forms.MessageBox.Show(args[0]);
        }

        public void myVoid(string foo)
        {
            System.Windows.Forms.MessageBox.Show(foo);
        }
    }
}


My Application:

string filename = @"C:\Test.dll";
    Assembly SampleAssembly;
    SampleAssembly = Assembly.LoadFrom(filename);
    // Obtain a reference to a method known to exist in assembly.
    MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("myVoid");
    // Obtain a reference to the parameters collection of the MethodInfo instance.

All credits go to SO user 'woohoo' for the above snippet How to call a Managed DLL File in C#?

Now, though, I would like to be able to not only reference my Dll (and the methods inside it) but properly call the methods inside it (in this case I would like to call method 'myVoid').

Might anyone have any suggestions for me?

Thank you,

Evan

解决方案

The question and answer you reference is using reflection to call the method in the managed DLL. This isn't necessary if, as you say you want to do, you simply reference your DLL. Add the reference (via the Add Reference option in Visual Studio), and you can call your method directly like so:

ExampleDLL.Program p = new ExampleDLL.Program(); // get an instance of `Program`
p.myVoid(); // call the method `myVoid`

If you want to go the reflection route (as given by woohoo), you still need an instance of your Program class.

Assembly SampleAssembly = Assembly.LoadFrom(filename);
Type myType = SampleAssembly.GetTypes()[0];
MethodInfo Method = myType.GetMethod("myVoid");
object myInstance = Activator.CreateInstance(myType);
Method.Invoke(myInstance, null);

Now you have an instance of Program and can call myVoid.

这篇关于C#使用程序集调用DLL中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆