C# GetMethod()返回null
我有一类查询C# GetMethod()返回null,c#,invoke,getmethod,C#,Invoke,Getmethod,我有一类查询 public class gridQueries { .... public string propertiesCombinedNamesQuery { get; set; } = "SELECT [NameId], [CombinedName] AS Display FROM [Names] ORDER BY [CombinedName]"; .... } // end class gridQueries 在另一个方法中,我获取这个
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];
这给了我一个像这样的字符串
“\u Q\u属性组合名称查询\u所有者名称”
谁能看出我做错了什么?
或者建议一种方法来调用这个字符串作为方法,这样我就可以得到包含SQL
查询的返回字符串了
非常感谢您的帮助。我已将我的财产声明转换为施工方案(感谢John Doe在上面的评论) 发件人: 致: 从我的程序中,我调用该方法
// 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);
....
查询现在包含选择[NameId],[CombinedName]作为[Names]订单中显示的[CombinedName]
这很有效
我缺少的是将类名作为Invoke语句的第一个参数传递。
propertiesCombinedNamesQuery
不是一个方法。这是一笔财产。此外,这里的大部分代码都是无关的。要么,queryMethod
包含错误的字符串(因此问题与GetMethod
无关),要么它包含正确的字符串,但GetMethod
返回null;在这种情况下,您可以简单地将其硬编码为一个简化的示例,并删除不相关的代码。
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);
....