A concise compiler

Source: Internet
Author: User
A concise compiler
multiple times to see someone raise the question of the calculation of the text expression, and then start to tidy up the previous code and add comments.
Writing a simple compiler is not very complicated, with some reflective knowledge to be used. I feel that the use of reflection in net is really everywhere, the use of reflection is not inefficient problem, after all, the current computer configuration is not very low. Using reflection appropriately, or by using reflection itself, can deepen your understanding of net. Later, you will write small "articles" that use reflection to increase the flexibility of your code for beginners ' reference.
If you're just calculating the value of an expression, of course, you can't use that much code. This way, it just makes it more versatile.
The following I directly paste the code, no longer say what (can say how how smelly, just don't swear).
Imports System.Reflection
Imports system.codedom
Imports System.CodeDom.Compiler
Public Class Sourcecomp
'//Compiler interface
Private M_compiler as ICodeCompiler
'//Compiler parameters
Private M_compilerparameters as CompilerParameters
'//referenced assembly
Private m_refassemblies as String () = {"System.dll", "System.Data.dll"}
'//Source code
Private M_source as String = ""
'//record is the default source code
Private M_is_default as Boolean = True
'//Log compilation status
Private m_compiled as Boolean = False
'//compile the generated assembly
Private m_assembly as System.Reflection.Assembly
'//instance of default source code generation
Private M_tmpclass as Object
'//default source code generated instance function
Private M_methodinfo as System.Reflection.MethodInfo
'//The default source function's expression parameter
Private M_expression as String
'//Back to Assembly
Public ReadOnly Property cpassembly () as System.Reflection.Assembly
Get
Return me.m_assembly
End Get
End Property
Sub New ()
'//get VB Compiler instance
Me.m_compiler = New VBCodeProvider (). CreateCompiler
'//initial compiler parameters
Me.m_compilerparameters = New CompilerParameters
With Me.m_compilerparameters
. generateexecutable = False '//false value specifies compilation as Class set, true compiled as executable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.