Blues Zhu 2023-10-10 11:42 采纳率: 100%
浏览 8
已结题

C#透过rflection使用MySql.Data

C#+MySQL的问题:由于特殊原因,无法在C#中直接引用MySQL.Data,因此不得已只能只用Reflection来做,从国外的平台找到的代码如下,稍加修改发现还是有报错

Visual Studio 2022开发,然后报错位置在下面43行,

methodMysqlConnection_Open.Invoke(oMySqlConnection, null);

报错内容

内部异常 1:
FileNotFoundException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. 系统找不到指定的文件。

完整代码如下:

using System.Reflection;
using System.Windows;

internal class Program
{
    private static void Main(string[] args)
    {
        Assembly assMySqlData = Assembly.LoadFrom(@"c:\FAConfig\MySql.Data.dll");

        Type typeMySqlConnection = assMySqlData.GetType("MySql.Data.MySqlClient.MySqlConnection");
        Type typeMySqlCommand = assMySqlData.GetType("MySql.Data.MySqlClient.MySqlCommand");
        Type typeMySqlDataReader = assMySqlData.GetType("MySql.Data.MySqlClient.MySqlDataReader");


        object oMySqlConnection = Activator.CreateInstance(typeMySqlConnection);

        if (oMySqlConnection != null)
        {
            MethodInfo methodMysqlConnection_State = typeMySqlConnection.GetMethod("get_State");
            MethodInfo methodMysqlConnection_Open = typeMySqlConnection.GetMethod("Open");
            MethodInfo methodMysqlConnection_ConnectionString = typeMySqlConnection.GetMethod("set_ConnectionString");
            MethodInfo methodMysqlConnection_Close = typeMySqlConnection.GetMethod("Close");
            MethodInfo methodMysqlConnection_Dispose = typeMySqlConnection.GetMethod("Dispose");

            Console.WriteLine(typeMySqlConnection.ToString());
            Console.WriteLine(oMySqlConnection.ToString());

            MethodInfo methodMysqlCommand_ExecuteReader = typeMySqlCommand.GetMethod("ExecuteReader", new Type[] { });
            MethodInfo methodMysqlCommand_Connection = typeMySqlCommand.GetMethod("set_Connection", new Type[] { typeMySqlConnection });
            MethodInfo methodMysqlCommand_CommandText = typeMySqlCommand.GetMethod("set_CommandText");
            MethodInfo methodMysqlCommand_Dispose = typeMySqlCommand.GetMethod("Dispose");

            MethodInfo methodMysqlDataReader_Read = typeMySqlDataReader.GetMethod("Read");
            MethodInfo methodMysqlDataReader_HasRows = typeMySqlDataReader.GetMethod("get_HasRows");
            MethodInfo methodMysqlDataReader_FieldCount = typeMySqlDataReader.GetMethod("get_FieldCount");
            MethodInfo methodMysqlDataReader_GetValue = typeMySqlDataReader.GetMethod("GetValue");
            MethodInfo methodMysqlDataReader_Close = typeMySqlDataReader.GetMethod("Close");
            MethodInfo methodMysqlDataReader_Dispose = typeMySqlDataReader.GetMethod("Dispose", new Type[] { });


            object[] arg = new object[] { (string)"Server=localhost;Database=download;User=root;Password=root;" };
            methodMysqlConnection_ConnectionString.Invoke(oMySqlConnection, arg);
            methodMysqlConnection_Open.Invoke(oMySqlConnection, null);
            object mysqlState = methodMysqlConnection_State.Invoke(oMySqlConnection, null);
            Console.WriteLine(mysqlState.ToString());

            object oMysqlCommand = Activator.CreateInstance(typeMySqlCommand);
            if (oMysqlCommand != null)
            {
                object[] paramCommandConnection = new object[] { oMySqlConnection }; //oMysqlConnection is not null
                object[] paramCommandText = new object[] { "SELECT * FROM `filelist`" };
                methodMysqlCommand_CommandText.Invoke(oMysqlCommand, paramCommandText);  //works fine
                methodMysqlCommand_Connection.Invoke(oMysqlCommand, paramCommandConnection); // error no object
                Console.WriteLine("5");
                object oMysqlDataReader = methodMysqlCommand_ExecuteReader.Invoke(oMysqlCommand, null);
                Console.WriteLine("6");
                object retRows = methodMysqlDataReader_HasRows.Invoke(oMysqlDataReader, null);
                Console.WriteLine("7");
                Console.WriteLine(retRows.ToString());
                Console.WriteLine("8");
            }

        }

        Console.ReadKey();
    }
}

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-10-10 11:45
    关注

    引自强大而免费的微信小程序:皆我百晓生。如有帮助,还望采纳。


    这个问题是由于缺少System.Diagnostics.DiagnosticSource.dll引起的。这个DLL文件是.NET Core的一部分,可能需要手动添加它作为依赖项。

    你可以尝试以下解决方案:

    1. 在Visual Studio中,右键单击项目并选择"管理NuGet程序包"。

    2. 在NuGet程序包管理器中,搜索"System.Diagnostics.DiagnosticSource"。

    3. 安装最新版本的 "System.Diagnostics.DiagnosticSource"。

    如果以上方法无效,你可以尝试手动添加引用:

    1. 在Visual Studio中,右键单击项目,选择"添加引用"。

    2. 在左侧的"浏览"选项卡下,浏览到"System.Diagnostics.DiagnosticSource.dll"的位置。通常它位于C:\Program Files\dotnet\shared\Microsoft.NETCore.App\<版本号>目录下。

    3. 添加这个DLL文件作为引用。

    希望这个解决方案能够帮助你解决问题。如果还有其他问题,请随时提问。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 10月18日
  • 已采纳回答 10月10日
  • 创建了问题 10月10日

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数