我试图从静态函数调用get_ConnectionString方法。但我得到了例外。
我的代码如下所示
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目标)
我怎么才能完成这件事?我在这里做错了什么?
发布于 2015-01-08 11:11:03
您需要传递对象实例,以便在:Invoke(sqlConnection, null)上调用该方法。错误信息具有误导性。
https://stackoverflow.com/questions/27838644
复制相似问题