如何使用Codedom获取标签的文本 [英] how to get the text of a label using codedom

查看:93
本文介绍了如何使用Codedom获取标签的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有各种标签和一个按钮的表格.在按钮单击事件上,编写了一个代码,该代码生成一个cs文件,我希望在其中显示标签的文本. >
我正在尝试使用以下代码dom中的以下功能来获取值,但是我无法提取标签的值,即我只是获取了label1.text,label2.text等.相反,我想要的是在标签和组合框中..

谁能帮忙..

 start.Statements.Add( CodeVariableReferenceExpression("  Info.Valid(" \   combobox1.SelectedValue.ToString()\""  \"label1.Text \""  \"label2.Text \""  \"label3.Text \""  \"numericupdown.Value .ToString()\" )" csc.exe,与Framework可再发行组件捆绑在一起.)因此,它应该是一个完整的项目.这是与您正在运行的进程中加载​​的程序集无关的程序集,即调用编译器的程序集.我们称其为主机程序集"和主机进程".让我们将被编译的程序集称为动态程序集".特别是,这意味着:如果源代码使用combobox1,则应在动态汇编中声明此变量或方法参数;它对主机程序集中声明的任何对象一无所知.同时,动态装配可以与宿主装配在相同的过程中加载.代码DOM将返回类型为System.Reflection.Assembly的变量,如果编译成功,主机程序集可以使用Reflection实例化动态程序集中的某个类,然后主机进程可以调用动态程序集中的某些代码.您可以从宿主程序集中传递变量,并使它们由动态程序集处理.这样,动态程序集可以修改用户界面,具体取决于用户输入的代码.实际上,这就像创建一个小型玩具Visual Studio一样,而且具有自修改UI.我会说出色的编程练习.

下一个问题是这个.通过映像可以成功地将一些代码编译为动态程序集.一次又一次,一次又一次……您的进程使用的内存将越来越大.无法卸载任何程序集-不能从现有的Application Domain中卸载.这是.NET体系结构基础中的明智决定.这将在您的进程中造成永久性内存泄漏.有一个解决方案.您不应该只加载用源代码创建的新程序集.您可以执行以下操作:每次创建新的应用程序域,在新的应用程序域中进行所有编译,并加载新的动态程序集并在其中运行其代码.当您需要再次执行此操作时,可以删除整个应用程序域-这是卸载任何代码的唯一方法.从理论上讲,没有其他方法可以卸载代码.

这种方法的问题是应用程序域之间的严格隔离:您不能在它们之间传递任何数据.您只能使用IPC,就好像使用同一进程的应用程序域是不同的进程一样. System.AppDomain类为此提供了高度简化的IPC.因此,不,您将无法将combobox1label2的实例传递给动态程序集.做类似的事情(没有内存泄漏,如果您在单个应用程序域中工作,这是不可避免的),则需要认真解决.我建议在我对过去的一个问题的回答中稍等一下.

请参阅我对这些问题的回答:
使用CodeDom生成代码 [创建使用可重载插件的WPF应用程序... [ ^ ]
这些答案相互参考.您会发现很多细节,因此您可以实际完成所有操作.

我的警告:这将是一个非常严肃的练习,将花费大量的工作时间,并且需要对编程有深刻的理解.

—SA


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..

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..

解决方案

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


这篇关于如何使用Codedom获取标签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆