| |

VerySource

 Forgot password?
 Register
Search
View: 738|Reply: 6

Questions about dynamically creating class instances

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-2 11:40:01
| Show all posts |Read mode
I wrote the code in the same namespace like this: (The main function is to create his instance based on the class name "String" that I wrote). The following code is valid:
namespace WindowsApplication1
{
    public partial class Form1: Form
    {
        public Form1 ()
        {
            InitializeComponent ();
            Type typ = Type.GetType ("WindowsApplication1.myCls");
            ConstructorInfo info = typ.GetConstructor (new Type [] {});
            info.Invoke (new object [] {});
            
        }
    }

    public class myCls
    {
        public myCls ()
        {
            MessageBox.Show ("");
        }
        public myCls (int a)
        {
            MessageBox.Show (a.ToString ());
        }
    }
}
If you move the above code, the code in the Form1 constructor to another class, and encapsulate it into a DLL, and then call the external class myCls again, you will be prompted to refer to the empty object. Why?
Reply

Use magic Report

0

Threads

26

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-3-22 18:00:01
| Show all posts
Did you add a DLL reference?
Is the namespace consistent after moving?
Reply

Use magic Report

0

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-26 13:30:01
| Show all posts
The description is not clear
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-10 15:30:01
| Show all posts
The dll has been added to the reference, the results of the consistent and inconsistent namespaces are the same, I hope the heroes help
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-25 09:45:01
| Show all posts
I also encountered this problem, and it seems that it can only be solved by reflection.
I don't know if it is feasible without reflection?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-8-25 10:00:01
| Show all posts
WindowsApplication1.myCls is just the name of the class, so if reflection is used, .NET will only find the definition of myCls in the current assembly (dll).

For example, if the Dll file name after Form1 and myCls are compiled is AAA.DLL, then use the following statement:

Type typ = Type.GetType("WindowsApplication1.myCls,AAA");
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-25 10:45:01
| Show all posts
If you call this overloaded method, you will see an error message

Type typ = Type.GetType("WindowsApplication1.myCls", true);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list