C# Getmethod调用失败,出现异常

C# Getmethod调用失败,出现异常,c#,asp.net,ado.net,C#,Asp.net,Ado.net,我试图从静态函数调用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 =

我试图从静态函数调用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();
}
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
上面的代码应该在out.txt文件中写入连接字符串。但我有个例外

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


我怎样才能做到这一点?这里我做错了什么?

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

谢谢。。但是,有可能从静态函数对get_ConnectionString进行运行时访问吗?所以它应该返回刚刚从某个X函数赋值的字符串。你是什么意思?没有静态连接字符串。它根本不存在。指定一个实例。我正在尝试通过一个静态函数将此代码注入Connection.Open(),以便获得连接字符串。在运行时是否可以通过注入代码来知道连接字符串?你想实现什么?我需要在.NET应用程序中配置DB查询,并从我的探查器(C++中使用IrrRead开发)来执行它们的执行时间。