我在
中有一类查询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
查询的返回字符串?
非常感谢任何和所有帮助。
答案 0 :(得分:0)
我将我的属性声明转换为方法声明(谢谢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语句的第一个参数传递。