从另一个可执行文件调用一个内部类内部的功能 [英] Calling an function inside an internal class from an another executable
本文介绍了从另一个可执行文件调用一个内部类内部的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我自己的code中调用.NET可执行文件的功能。 我用反射镜,看到这样的:
命名空间TheExe.Core
{
内部静态类集信息
内部静态类StringExtensionMethods
}
在命名空间TheExe.Core是我感兴趣的功能:
内部静态类StringExtensionMethods
{
//方法
公共静态字符串哈希(此字符串原来,字符串密码);
//更多方法...
}
使用这个code,我可以看到的哈希方法,但我怎么称呼呢?
大会驴= Assembly.LoadFile(TheExe);
键入asmType = ass.GetType(TheExe.Core.StringExtensionMethods);
MethodInfo的MI = asmType.GetMethod(哈希,BindingFlags.Public | BindingFlags.Static);
字符串[]参数= {布拉布拉,MyPassword输入};
//这行给出System.Reflection.TargetParameterCountException
//以及如何把结果转换字符串?
mi.Invoke(空,新的对象[] {参数});
解决方案
正在传递字符串数组作为与您当前的code的参数。
由于的String []
可强制转换为 [对象]
,你可以通过参数
数组调用
。
字符串结果=(字符串)mi.Invoke(NULL,参数);
I want to call a function from a .net executable from my own code. I used reflector and saw this:
namespace TheExe.Core
{
internal static class AssemblyInfo
internal static class StringExtensionMethods
}
In namespace TheExe.Core is the function I am interested in:
internal static class StringExtensionMethods
{
// Methods
public static string Hash(this string original, string password);
// More methods...
}
Using this code I can see the Hash Method but how do I call it?
Assembly ass = Assembly.LoadFile("TheExe");
Type asmType = ass.GetType("TheExe.Core.StringExtensionMethods");
MethodInfo mi = asmType.GetMethod("Hash", BindingFlags.Public | BindingFlags.Static);
string[] parameters = { "blabla", "MyPassword" };
// This line gives System.Reflection.TargetParameterCountException
// and how to cast the result to string ?
mi.Invoke(null, new Object[] {parameters});
解决方案
You are passing an array of strings as a single parameter with your current code.
Since string[]
can be coerced to object[]
, you can just pass the parameters
array into Invoke
.
string result = (string)mi.Invoke(null, parameters);
这篇关于从另一个可执行文件调用一个内部类内部的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文