すべて、戻り値の型の DLL を呼び出すために現在使用されているメソッドがありますbool
。これはうまく機能します。この方法は
public static bool InvokeDLL(string strDllName, string strNameSpace,
string strClassName, string strMethodName,
ref object[] parameters,
ref string strInformation,
bool bWarnings = false)
{
try
{
// Check if user has access to requested .dll.
if (!File.Exists(Path.GetFullPath(strDllName)))
{
strInformation = String.Format("Cannot locate file '{0}'!",
Path.GetFullPath(strDllName));
return false;
}
else
{
// Execute the method from the requested .dll using reflection.
Assembly DLL = Assembly.LoadFrom(Path.GetFullPath(strDllName));
Type classType = DLL.GetType(String.Format("{0}.{1}",
strNameSpace, strClassName));
if (classType != null)
{
object classInstance = Activator.CreateInstance(classType);
MethodInfo methodInfo = classType.GetMethod(strMethodName);
if (methodInfo != null)
{
object result = null;
result = methodInfo.Invoke(classInstance, new object[] { parameters });
return Convert.ToBoolean(result);
}
}
// Invocation failed fatally.
strInformation = String.Format("Could not invoke the requested DLL '{0}'! " +
"Please insure that you have specified the namespace, class name " +
"method and respective parameters correctly!",
Path.GetFullPath(strDllName));
return false;
}
}
catch (Exception eX)
{
strInformation = String.Format("DLL Error: {0}!", eX.Message);
if (bWarnings)
Utils.ErrMsg(eX.Message);
return false;
}
}
ここで、このメソッドを拡張して、任意のタイプの DLL から戻り値を取得できるようにします。私はジェネリックを使用してこれを行うことを計画していましたが、すぐに未知の領域に入りました。コンパイル時に不明な場合に T を返すにはどうすればよいですか。リフレクションを調べましたが、この場合にどのように使用されるかわかりません。上記のコードで最初のチェックを行います
public static T InvokeDLL<T>(string strDllName, string strNameSpace,
string strClassName, string strMethodName,
ref object[] parameters, ref string strInformation,
bool bWarnings = false)
{
try
{
// Check if user has access to requested .dll.
if (!File.Exists(Path.GetFullPath(strDllName)))
{
strInformation = String.Format("Cannot locate file '{0}'!",
Path.GetFullPath(strDllName));
return "WHAT/HOW??";
...
どうすれば欲しいものを達成できますか、それともメソッドをオーバーロードするだけですか?
どうもありがとうございました。