In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use VB.NET dynamic code, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Use VB.NET dynamic code
Creating a control at run time is a strategy adopted when the functionality of the application cannot be determined. However, dynamically creating controls is not suitable for all cases. Sometimes you have to create executable code, although your application runs to compensate for different configurations, different user needs, different environmental requirements, or other requirements. When the computer on which the application is running does not have controls, it is usually necessary to create VB.NET dynamic code.
Fortunately, .NET provides us with a range of VB.NET dynamic code options. For example, you can create an executable program that can run independently, or you can load a DLL and then execute a program that you want to run. When you need to demonstrate an external task, you can use options to execute, such as running a script-this DLL option is best suited for expanding existing application functionality.
You can run VB.NET dynamic code from files or memory. You can use files when you need to run the code more than once. The check of the code can run the external file again without recompiling it. When you need to demonstrate multiple tasks, such as an installation request, you can use in-memory images.
Of course, we can also change the source code. For example, you can use strings to create code that needs to be used directly in the application. If you need the code to be highly flexible and the code itself is not very long, the advantage of this approach is significant. You can also create code from a file, just like VS. This method is most suitable for requirements that are relatively stable and do not require complex coding. The third option is to use Documentation Object Model to create code and use it as a series of CodeDom trees. The tree structure includes CodeCormpileUnits. This is like creating a XML file in DOM schema.
The way to create code dynamically is to check it out with examples. Example 3 shows a basic "Hello World" example. This example creates the code directly from the source code so you can see the whole process of running and generating an external executable.
Private Sub btnTest3_Click () Handles btnTest3.Click 'Create a compiler. Dim Comp As VBCodeProvider = New VBCodeProvider () 'Define the parameters for the code you want to compile. Dim Parms As CompilerParameters = New CompilerParameters)'We do want to create an executable, rather than a DLL. Parms.GenerateExecutable = True 'The compiler will create an output assembly called Output. Parms.OutputAssembly = "Output" 'The compiler won't treat warnings as errors. Parms.TreatWarningsAsErrors = False 'Add any assembly you want to reference. Parms.ReferencedAssemblies.Add ("System.Windows.Forms.dll") 'Define the code you want to run. Dim SampleCode As StringBuilder = New StringBuilder () SampleCode.Append ("Imports System.Windows.Forms" + vbCrLf) SampleCode.Append ("Module TestAssembly" + vbCrLf) SampleCode.Append ("Sub Main ()" + vbCrLf) SampleCode.Append ("MessageBox.Show (" + Chr (34) + _ "Dynamically Created Code!" + _ Chr (34) + ") + vbCrLf) SampleCode.Append (" End Sub "+ vbCrLf) SampleCode.Append (" End Module " "+ vbCrLf) 'Define the code to run. Dim Executable As CompilerResults = _ Comp.CompileAssemblyFromSource (Parms, SampleCode.ToString ()) 'Display error messages if there are any. If Executable.Errors.HasErrors Then For Each Item As CompilerError In Executable.Errors MessageBox.Show (Item.ErrorText) Next Else'If there aren't any error messages, start the 'executable. Process.Start ("Output") End If End Sub above is all the content of this article "how to use VB.NET dynamic Code". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.