在c#中从字符串值创建类实例并赋值给相同的类类型

本文关键字:赋值 类型 实例 字符串 创建 | 更新日期: 2023-09-27 18:06:57

private void QueryDBSchema()
    {
        // Schema information for the current database connection.
        DataTable schema;
        // Loop counter.
        int loop = 0;
        // Clean up the menu so the menu item does not hang while this function  executes.
        this.Refresh();

            // Instantiate an OleDbConnection object.
        using (OleDbConnection oleDbConnection = new    OleDbConnection("Provider=SQLOLEDB;Password=sa123;User ID=sa;Data Source=mukesh;Initial Catalog=Medi;"))
            {
                try
                {
                    // Open the connection.
                    oleDbConnection.Open();
                    // Retrieve the Table objects.
                        schema =    oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "table" });
                    // Store the table names in the object collection.
                    for (loop = 0; loop < schema.Rows.Count; loop++)
                    {
                        objects.Add(schema.Rows[loop].ItemArray[2].ToString());
                    }
                }
                catch (Exception e)
                {
                   // Messages.BadConnection(e);
                }
            }
    }

在对象列表中获取表名。

        // Instantiate the objects collection.
        this.objects = new Collection<string>();
        QueryDBSchema();
        // Gather each of the selected items (if any) into a collection object.
        foreach (string item in this.objects)
          {
          //This should be like this
          // "item" obj = new "item";
          }

在foreach循环,我想创建类对象与字符串保存在项目值。这在c#中是如何可能的,同样的名字类是在不同的命名空间中存在的,我想把对象分配给另一个相同的类在不同的命名空间和调用函数命名为保存在obj1.

//"item" obj1 = new "item";//"Item" obj2 = new "Item";

其中obj1 = methoda;obj1.Save ();

在c#中从字符串值创建类实例并赋值给相同的类类型

使用

ObjectHandle句柄= Activator。调用CreateInstance("YourAssemblyName_String"、"项目");

Item object = (Item)handle.Unwrap();