Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I have a form in which there are various labels and a button..on the button click event there is a code written which generates a cs file in which i want the text of the label to be displayed..

I am trying to get the values with the help of the following function in the code dom but m not able to extract the values of the label i.e. i am just getting label1.text, label2.text, etc. instead i want the values that are there in the labels and the combobox..

can anyone please help..

C#
start.Statements.Add(new CodeVariableReferenceExpression("Info.Valid("\"combobox1.SelectedValue.ToString()\"", "\"label1.Text\"", "\"label2.Text\"", "\"label3.Text\"", "\"numericupdown.Value.ToString()\"")");


here start is the CodeMemberMethod to which all the statements are to be added, Info is another class and Valid is a method to which i need to pass all these values as arguments..
Posted
Comments
Sergey Alexandrovich Kryukov 8-Apr-11 12:35pm    
Please see my last comment (to the comment by d@anich).
Could you share with us what's your intended purpose of all this?
Thank you.
--SA

1 solution

This won't work in principle, but you can do something similar which would work. I don't see any practical purpose of such things. Maybe you can explain the idea? And the ultimate goal?

As I understand, it can be used just as a toy used to understand programming better. Of course.

Now, Code DOM. It looks like right now you don't understand what it does from the very beginning. A code you can compile using Code DOM has to be a fully-fledged assembly, so the set of source codes (it can be more then one "file" a set of "files") should be a complete C# project, which can compile normally, as if you used a C# compiler directly. (By the way, Code DOM actually uses this C# compiler: csc.exe, the one bundled with the Framework redistributable.) So, it should be a complete project. This is assembly has nothing to do with the loaded assembly with is running in you process, the one which is calling the compiler. Let's call it "host assembly" and "host process". Let's call the assembly being compiled the "dynamic assembly". In particular, it means: if your source code uses combobox1, this variable or a method parameter should be declared in dynamic assembly; it cannot know anything about any objects declared in your host assembly. At the same time, the dynamic assembly can be loaded in the same process as the host assembly. Code DOM will return you the variable of the type System.Reflection.Assembly, if the compilation was successful, host assembly can instantiate some class in the dynamic assembly using Reflection, and then some code from the dynamic assembly can be called by the host process. You can pass your variable from the host assembly and make them processed by the dynamic assembly. This way, the dynamic assembly can modify you UI, depending on the code the user enters. Practically, this is the as as creating a small toy Visual Studio, but also with self modifying UI. Excellent programming exercise, I would say.

Next problem is this. Imaging you successfully compile some code into dynamic assembly. Once, twice, again and again… the memory used by your process will grow and grow. There is no way to unload any assembly — not from existing Application Domain; this is sound decision put in the base of .NET architecture. This will create a permanent memory leak in you process. There is a solution. You should not just load new assembly created out of source code. You can do the following: each time, create a new Application Domain, do all the compilation in the new Application Domain and load your new dynamic assembly and run its code there. When you need to do it again, you can delete whole Application Domain — this is the only method to unload any code; no other way of unloaded code is theoretically possible.

The problem of this approach is strict isolation between Application Domains: you cannot pass any data between them. You can only use IPC, as if the Application Domains withing the same process were different processes. The System.AppDomain class provides highly simplified IPC for this purpose. So, no, you won't be able to pass your instance of combobox1 or label2 to your dynamic assembly. Doing something like (without the memory leak which is unavoidable if you work in a single Application Domain) will need a serious work around. I suggest some wait to do something like that in my Answer to one past Question.

Please see my Answers to these Questions:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^]
These Answers refer to each other. You will find a lot of detail, so you can actually do it all.

My warning: this would be a really serious exercise, will take considerable working time and will need deep understanding of programming.

—SA
 
Share this answer
 
v4
Comments
Nuri Ismail 8-Apr-11 6:10am    
Great answer. My 5.

I believe, after your post, the OP will understand the heaviness of this task.

BTW, Reading your answers, my interest to CodeDom greatly increased. :)
I have no practical experience in this field but now (after your answers)
I'm eager for funny experiments... Maybe this weekend... :)

Best Regards,
Nuri
Sergey Alexandrovich Kryukov 8-Apr-11 11:48am    
Thank you very much, Nuri.
I'm very excited about stimulating your interest to the topic. :-)
--SA
dan!sh 8-Apr-11 12:03pm    
Very true and great answer as usual. Having written snippet compiler kind of application I know CodeDom is really tough to handle.
Sergey Alexandrovich Kryukov 8-Apr-11 12:34pm    
Thank you very much.
CodeDOM programming itself in not really tough; all available software components are pretty easy to use. It's the sum of technology put together is kind of difficult, especially when you need to work through AppDomain boundary.
I also can tell that making IDE type of software is not the most difficult -- I did it. What OP is trying to do here is much more difficult. I actually appreciate OP's wild fantasy: this idea can bring the most difficult problems. Such exercise can be useful as it can generate some exotic type of application hosting or a type of application server. The practical applicability of it is quite questionable. I have no idea of OP's purpose: it can be either the idea just to abuse technology heavily, or, on the opposite site, something very clever... :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900