Reflection
このように(オブジェクトの初期化が呼び出される関数に依存しないと仮定して)クラスの関数を呼び出そうとしています
/// <summary>
/// Calls a static public method.
/// Assumes that the method returns a string
/// </summary>
/// <param name="assemblyName">name of the assembly containing the class in which the method lives.</param>
/// <param name="namespaceName">namespace of the class.</param>
/// <param name="typeName">name of the class in which the method lives.</param>
/// <param name="methodName">name of the method itself.</param>
/// <param name="stringParam">parameter passed to the method.</param>
/// <returns>the string returned by the called method.</returns>
///
public static string InvokeStringMethod5(string assemblyName, string namespaceName, string typeName, string methodName, string stringParam)
{
//This method was created incase Method has params with default values. If has params will return error and not find function
//This method will choice and is the preffered method for invoking
// Get the Type for the class
Type calledType = Type.GetType(String.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName));
String s = null;
// Invoke the method itself. The string returned by the method winds up in s.
// Note that stringParam is passed via the last parameter of InvokeMember, as an array of Objects.
if (MethodHasParams(assemblyName, namespaceName, typeName, methodName))
{
s = (String)calledType.InvokeMember(
methodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
null,
null,
new Object[] { stringParam });
}
else
{
s = (String)calledType.InvokeMember(
methodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
null,
null,
null);
}
// Return the string that was returned by the called method.
return s;
}
public static bool MethodHasParams(string assemblyName, string namespaceName, string typeName, string methodName)
{
bool HasParams;
string name = String.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName);
Type calledType = Type.GetType(name);
MethodInfo methodInfo = calledType.GetMethod(methodName);
ParameterInfo[] parameters = methodInfo.GetParameters();
if (parameters.Length > 0)
{
HasParams = true;
}
else
{
HasParams = false;
}
return HasParams;
}
これ はここで 撮影 さ れ た.
これを行う他の/より良い方法はありますか??
この活動は将軍になることができますか。戻り値の型が独立できるように、.Net 4.0を使用しDynamic
て呼び出すことができます。Non-Static methods
実際のシナリオでキーワードを使用したことはありませんdynamic
(いくつかの例を読むだけです)。実際の使用法はまだ不明です
この点に関するヘルプ/指示をいただければ幸いです ありがとう