C#反射 - 装载组件,并调用一个方法,如果它存在 [英] C# reflection - load assembly and invoke a method if it exists

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

问题描述

我要加载程序集(其名称都存储在一个字符串),使用反射来检查它是否有一个名为CustomType的MyMethod(字节[] A,INT B)的方法,并调用它或抛出一个异常。我想我应该做这样的事情,但将AP preciate如果有人可以提供关于如何最好地做同样的建议:

 大会ASM =的Assembly.Load(MyAssembly程序); / *如果写MyAssembly程序或MyAssembly.dll程序1.什么关系呢? * /类型t = asm.GetType(myAssembly.ClassName);//指定参数
字节[] A = GetParamA();
INT B = GetParamB();[对象] PARAMS =新对象[2];
PARAMS [0] =一个;
PARAMS [1] = B;/ * 2 invoke方法的MyMethod()返回对象CustomType - 我怎么检查它是否存在? * /
/ * 3.什么是第四个参数的含义(在这种情况下,T); MSDN说,这是的对象上调用指定的成员,而不是这个已经t.InvokeMember()占用? * /
CustomType结果= t.InvokeMember(的MyMethod,BindingFlags.InvokeMethod,空,T,则params);

这是不够好,或者有其他更好/更快/更短的方法呢?有关构造函数是什么,因为这些方法不是静态的 - 他们可以简单地忽略

当调用无效()的方法,它是确定只写t.InvokeMember(...)或者你应该总是做obj对象= t.InvokeMember(...)?

先谢谢了。


修改
我提供了一个工作示例如下一个单独的答案。


解决方案

  

使用反射来检查它是否有一个名为CustomType的MyMethod(字节[] A,INT B)的方法,并调用它或抛出一个异常


您当前code未满足这一要求。但是你可以像这样的东西很容易pretty:

  VAR的MethodInfo = t.GetMethod(的MyMethod,新类型[] {typeof运算(字节[])的typeof(INT)});
如果(MethodInfo的== NULL)//该方法不存在
{
    //抛出一些异常
}变种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天全站免登陆