gpt4 book ai didi

c# - 使用类名和方法名调用类的成员

转载 作者:行者123 更新时间:2023-11-30 22:24:17 25 4
gpt4 key购买 nike

我正在尝试使用 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;

}

这取自here .

有没有其他/更好的方法来做到这一点?

这个事件可以是将军。喜欢使用Dynamic,在.Net 4.0中可以调用Non-Static methods,这样返回类型就可以独立了。

我从来没有在实际场景中使用过dynamic关键字(只看了一些例子),实际用法我还不知道

在这方面的任何帮助/指导将不胜感激谢谢

最佳答案

回答你关于dynamic的查询;不,那不适合这里。 dynamic 在编译时已知成员名称(或操作)但无法证明其存在时很有用 - 基本上是鸭子类型。例如:

dynamic foo = GetSomeRandomObject();
foo.ThisShouldExist("abc");

这做类似的事情,但用法不同。所以是的,你留下了反射(reflection)。你在做什么是非常正确的。我唯一可能更改的是获取 MethodInfo,并从那里开始工作 - 尽管如果您可以更改 API 以接受单个 string assemblyQualifiedName 会更方便灵活。但也许:

public static string InvokeStringMethod5(string assemblyName,
string namespaceName, string typeName, string methodName, string stringParam)
{
string assemblyQualifiedName = string.Format("{0}.{1},{2}",
namespaceName, typeName, assemblyName);
Type calledType = Type.GetType(assemblyQualifiedName);
if(calledType == null) throw new InvalidOperationException(
assemblyQualifiedName + " not found");
MethodInfo method = calledType.GetMethod(methodName,
BindingFlags.Public | BindingFlags.Static);
switch (method.GetParameters().Length)
{
case 0:
return (string)method.Invoke(null, null);
case 1:
return (string)method.Invoke(null, new object[] { stringParam });
default:
throw new NotSupportedException(methodName
+ " must have 0 or 1 parameter only");
}
}

关于c# - 使用类名和方法名调用类的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com