我在中有一类查询
public class gridQueries
{
....
public string propertiesCombinedNamesQuery { get; set; } =
"SELECT [NameId], [CombinedName] AS Display FROM [Names] ORDER BY [CombinedName]";
....
} // end class gridQueries 在另一个方法中,我获得这个查询名称的字符串,然后尝试调用它,但GetMethod()总是返回null。
// Get the short name of the dependent field from the dictionary value[2] string
string _1DF_Name = addEditFieldControl.FirstOrDefault(p => p.Value[1].Contains(fieldShortName)).Key;
// Find the long name of the dependent field
string Value1Text = addEditFieldControl.FirstOrDefault(p => p.Key.Contains(_1DF_Name)).Value[1];这给了我一个类似于"_Q_propertiesCombinedNamesQuery_OwnerName“的字符串。
// Couldn't get invoke to work.
// because MethodInfo info is null after the GetMethod() call
string queryMethod = "";
queryMethod = "queries."+strIsBtwTags(Value1Text, "_Q_", "_");
Type queriesType = queryMethod.GetType();
MethodInfo info = queriesType.GetMethod(queryMethod);
string query = info.Invoke(null, null).ToString();有人能发现我做错了什么吗?或者建议一种将此字符串作为方法调用的方法,以便获得包含SQL查询的返回字符串?
任何帮助都是非常感谢的。
发布于 2017-07-26 11:49:38
我将属性语句转换为方法语句(在上面的注释中感谢John Doe )
发自:
public class gridQueries
{
....
public string propertiesCombinedNamesQuery { get; set; } =
"SELECT [NameId], [CombinedName] AS Display FROM [Names] ORDER BY [CombinedName]";
....
}至:
public class gridQueries
{
....
public string propertiesCombinedNamesQuery()
{
return "SELECT [NameId], [CombinedName] AS Display FROM [Names] ORDER BY [CombinedName]";
}
....
} 从我的程序中,我调用该方法
// Top of program
using System.Reflection;
....
....
string qstring = "propertiesCombinedNamesQuery";
string query = ""
gridQueries q2 = new gridQueries();
MethodInfo methodInfo = q2.GetType().GetMethod(qstring);
query = (string)methodInfo.Invoke(q2, null);
....查询现在包含SELECT [NameId], [CombinedName] AS Display FROM [Names] ORDER BY [CombinedName]
这是可行的。
我遗漏的是将类名作为Invoke语句的第一个参数传递。
https://stackoverflow.com/questions/44579976
复制相似问题