| |

VerySource

 Forgot password?
 Register
Search
View: 656|Reply: 4

How to add a special type of Property to a dynamically generated class

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-9 15:00:01
| Show all posts |Read mode
I used the methods in the System.Reflection namespace to dynamically generate a class in my code. Now I want to add a special Property as shown in the code below. What should I do?

The property information to be added is as follows:
private string filename;
[Editor (typeof (System.Windows.Forms.Design.FileNameEditor), typeof (System.Drawing.Design.UITypeEditor))]
        public string FileName
        {
            get
            {
                return filename;
            }

            set
            {
                this.filename = value;
            }
        }

This Property is when the property is displayed in the PropertyGrid, a small "..." button will appear. When clicked, a dialog box for selecting a file will appear. After selection, the path of the selected file will be returned to this Property.
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-4-6 11:00:01
| Show all posts
Not a good solution:
Add a HashTable to the class. Among them save data members. The keyword is the member name.
Reply

Use magic Report

0

Threads

32

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-11 16:15:01
| Show all posts
I always thought that reflection is to read the type information at runtime, can it be changed? Now that it has been compiled, this type is a fait accompli.
If it is to dynamically generate a new type and compile it is OK.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-5-1 21:00:01
| Show all posts
The type information is not changed at runtime. The information of get and set is still of type String, but this line is added:
[Editor (typeof (System.Windows.Forms.Design.FileNameEditor), typeof (System.Drawing.Design.UITypeEditor))]]
It means to change the editing method of this attribute. I do n’t know exactly what it means. The optical cable is broken, Msdn ca n’t open it, I ca n’t find the information, I am depressed.
Reply

Use magic Report

0

Threads

32

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-5-2 13:30:02
| Show all posts
Adding an attribute statement is of course a change.
ps: This means that FileNameEditor is used as the Editor during the design period, which is dragged to the design interface seen on the Form.
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