首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GetMethod()返回null

GetMethod()返回null
EN

Stack Overflow用户
提问于 2017-06-16 10:44:05
回答 1查看 433关注 0票数 0

我在中有一类查询

代码语言:javascript
复制
public class gridQueries
{
    ....

    public string propertiesCombinedNamesQuery { get; set; } =
        "SELECT [NameId], [CombinedName] AS Display FROM [Names] ORDER BY [CombinedName]";
    ....
}  // end class gridQueries 

在另一个方法中,我获得这个查询名称的字符串,然后尝试调用它,但GetMethod()总是返回null

代码语言:javascript
复制
// 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“的字符串。

代码语言:javascript
复制
// 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查询的返回字符串?

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2017-07-26 11:49:38

我将属性语句转换为方法语句(在上面的注释中感谢John Doe )

发自:

代码语言:javascript
复制
public class gridQueries
{
    ....
    public string propertiesCombinedNamesQuery { get; set; } =
        "SELECT [NameId], [CombinedName] AS Display FROM [Names] ORDER BY [CombinedName]";
    ....
}

至:

代码语言:javascript
复制
public class gridQueries
{
    ....
    public string propertiesCombinedNamesQuery() 
    {
        return "SELECT [NameId], [CombinedName] AS Display FROM [Names] ORDER BY [CombinedName]";
    }
    ....
} 

从我的程序中,我调用该方法

代码语言:javascript
复制
// 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语句的第一个参数传递。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44579976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档