C# 反射 - 加载程序集并调用存在的方法 [英] C# reflection - load assembly and invoke a method if it exists

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

问题描述

我想加载一个程序集(它的名字存储在一个字符串中),使用反射来检查它是否有一个名为CustomType MyMethod(byte[] a, int b)"的方法并调用它或者抛出异常.我想我应该做这样的事情,但如果有人能就如何最好地做到这一点提供相同的建议,我将不胜感激:

Assembly asm = Assembly.Load("myAssembly");/* 1. 写 myAssembly 还是 myAssembly.dll 有关系吗?*/Type t = asm.GetType("myAssembly.ClassName");//指定参数byte[] a = GetParamA();int b = GetParamB();对象[] params = 新对象[2];参数[0] = 一个;参数[1] = b;/* 2. 调用返回对象CustomType"的方法 MyMethod() - 如何检查它是否存在?*//* 3.第四个参数是什么意思(在这种情况下是t);MSDN 说这是在其上调用指定成员的对象",但这不是已经通过使用 t.InvokeMember() 来解决的吗?*/CustomType result = t.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, t, params);

这是否足够好,或者有更好/更快/更短的方法吗?考虑到这些方法不是静态的,那么构造函数呢?可以简单地忽略它们吗?

在调用 void Methods() 时,是直接写 t.InvokeMember(...) 还是应该一直写 Object obj = t.InvokeMember(...)?

提前致谢.

<小时>

编辑我在下面提供了一个工作示例作为单独的答案.

解决方案

使用反射来检查它是否有一个名为CustomType MyMethod(byte[] a, int b)"的方法,否则调用它或抛出异常

您当前的代码不满足该要求.但是你可以很容易地使用这样的东西:

var methodInfo = t.GetMethod("MyMethod", new Type[] { typeof(byte[]), typeof(int) });if (methodInfo == null)//该方法不存在{//抛出一些异常}var o = Activator.CreateInstance(t);var 结果 = methodInfo.Invoke(o, params);

<块引用>

这是否足够好,或者有更好/更快/更短的方法吗?

就我而言,这是最好的方法,而且没有比这更快的方法了.

<块引用>

鉴于这些方法不是静态的,那么构造函数呢?可以简单地忽略它们吗?

您仍然需要创建一个 t 的实例,如我的示例所示.这将使用没有参数的默认构造函数.如果您需要传递参数,只需查看 MSDN 文档 并对其进行修改.

I want to load an assembly (its name is stored in a string), use reflection to check if it has a method called "CustomType MyMethod(byte[] a, int b)" and call it or throw an exception otherwise. I guess I should do something like this, but would appreciate if someone could offer same advice on how best to do it:

Assembly asm = Assembly.Load("myAssembly"); /* 1. does it matter if write myAssembly or myAssembly.dll? */

Type t = asm.GetType("myAssembly.ClassName");

// specify parameters
byte[] a = GetParamA();
int b = GetParamB();

object[] params = new object[2];
params[0] = a;
params[1] = b;

/* 2. invoke method MyMethod() which returns object "CustomType" - how do I check if it exists? */
/* 3. what's the meaning of 4th parameter (t in this case); MSDN says this is "the Object on which to invoke the specified member", but isn't this already accounted for by using t.InvokeMember()? */
CustomType result = t.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, t, params);

Is this good enough, or are there better/faster/shorter ways? What about constructors, given that these methods are not static - can they simply be ignored?

When invoking void Methods(), is it ok to just write t.InvokeMember(...) or should you always do Object obj = t.InvokeMember(...)?

Thanks in advance.


EDIT I have provided a working example as a separate answer below.

解决方案

use reflection to check if it has a method called "CustomType MyMethod(byte[] a, int b)" and call it or throw an exception otherwise

Your current code isn't fulfilling that requirement. But you can pretty easily with something like this:

var methodInfo = t.GetMethod("MyMethod", new Type[] { typeof(byte[]), typeof(int) });
if (methodInfo == null) // the method doesn't exist
{
    // throw some exception
}

var o = Activator.CreateInstance(t);

var result = methodInfo.Invoke(o, params);

Is this good enough, or are there better/faster/shorter ways?

As far as I'm concerned this is the best way and there isn't really anything faster per say.

What about constructors, given that these methods are not static - can they simply be ignored?

You are still going to have to create an instance of t as shown in my example. This will use the default constructor with no arguments. If you need to pass arguments you can, just see the MSDN documentation and modify it as such.

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

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