A concise Compiler

Source: Internet
Author: User

A concise Compiler
Once again, when someone raises a question about the calculation of text expressions, they can sort out the previous Code and add comments.
Writing a simple compiler is not very complicated. Some reflection knowledge is needed. In my opinion, the use of reflection is everywhere in NET, and there is no efficiency or inefficiency in using reflection. After all, the current computer configuration is not very low. Using Reflection as appropriate, or using reflection itself can help you gain a deeper understanding of NET. I will write some small "articles" that use reflection to increase code flexibility for beginners.
If you only want to calculate the value of an expression, you certainly cannot use so much code. This is just to make it more universal.
I have directly pasted the code below, so I will not talk about anything (I can say how to smell it, but 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 whether it is the default source code
Private m_Is_Default As Boolean = True
'// Record the compilation status
Private m_Compiled As Boolean = False
'// Compiled assembly
Private m_Assembly As System. Reflection. Assembly
'// Default source code generation instance
Private m_tmpClass As Object
'// The instance function generated by the default source code
Private m_MethodInfo As System. Reflection. MethodInfo
'// Expression parameters of the default source code Function
Private m_Expression As String
'// Returns the Assembly
Public ReadOnly Property cpAssembly () As System. Reflection. Assembly
Get
Return Me. m_Assembly
End Get
End Property
Sub New ()
'// Obtain the VB compiler instance
Me. m_Compiler = New VBCodeProvider (). CreateCompiler
'// Initial compiler Parameters
Me. m_CompilerParameters = New CompilerParameters
With Me. m_CompilerParameters
. GenerateExecutable = false' // The value of False indicates that the program is compiled as a class set, and True indicates that the program can be compiled as an executable program.

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.