首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Getmethod调用异常失败

Getmethod调用异常失败
EN

Stack Overflow用户
提问于 2015-01-08 11:08:05
回答 1查看 129关注 0票数 1

我试图从静态函数调用get_ConnectionString方法。但我得到了例外。

我的代码如下所示

代码语言:javascript
复制
PropertyInfo[] myPropertyInfo;

var ss = Type.GetType("System.Data.SqlClient.SqlConnection,System.data, version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false, true);
myPropertyInfo = ss.GetProperties();
for (int i = 0; i < myPropertyInfo.Length; i++)
{
    System.IO.StreamWriter sw = System.IO.File.AppendText("C:\\Temp\\out.txt");
    if(myPropertyInfo[i].GetMethod.ToString().Contains("get_ConnectionString") == true)
    {
         var obj1 = myPropertyInfo[i].GetMethod.Invoke(null,null);
         sw.WriteLine(obj1);
    }
    sw.Close();
}

上面的代码应该在out.txt文件中写入连接字符串。但我得到了例外

System.Reflection.TargetException:对象与目标类型不匹配。在System.Reflection.RuntimeMethodInfo.CheckConsistency(Object目标)

我怎么才能完成这件事?我在这里做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-08 11:11:03

您需要传递对象实例,以便在:Invoke(sqlConnection, null)上调用该方法。错误信息具有误导性。

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

https://stackoverflow.com/questions/27838644

复制
相关文章

相似问题

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