none
[VB.Net 2005]问个问题,怎样根据输入的字符串,自动生成代码?? RRS feed

  • 问题

  • 怎样根据输入的字符串,自动生成代码

     

    比如有一个控件A输入"50>",另一个控件B输入"40",做验证时就是50>40,通过。

     

    如果B是50就不通过

     

    再比如A"10<,50>",B"20",这样也通过

     

    有没有方法可以生成表达式或者方法随便什么,然后我通过什么方法拿到判断的值

     

    xdjm么帮帮忙~谢谢啦~

    2008年8月10日 12:36

答案

  • 我实在是新手..但你的意思我好像是明白了。

    如果单单是判断你这种情况,比如说好像你的意思

     

    你可以用正则表达式来分别判断。

    但我本人..非常..不喜欢正则表达式.......

    所以..

     

    所以...嘿嘿..偶尔用用CodeDom也不是件坏事是吧....

     

    (C#写顺手了又变成VB很怪..)

     

     

     

    Code Snippet

    Imports System.CodeDom
    Imports System.CodeDom.Compiler
    Imports System.Reflection
    Imports Microsoft.VisualBasic

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MsgBox(Codedom_Check("10<,50>", "20"))
        End Sub

     

        Public Function Codedom_Check(ByVal A As String, ByVal B As String) As Boolean
            Dim compiler As ICodeCompiler = CodeDomProvider.CreateProvider("VB").CreateCompiler()
            Dim cp As New Compiler.CompilerParameters
            cp.GenerateExecutable = False
            cp.GenerateInMemory = True

            Dim code As New System.Text.StringBuilder
            Dim checker As String = ""

            For Each T As String In Split(A, ",")
                checker = checker + " " + T + B + " and"
            Next
            checker = checker.Remove(checker.Length - 4)

     


            code.AppendLine("Public Class M" + vbCrLf)                                    'Class
            code.AppendLine("Public Shared Function Check() as Boolean" + vbCrLf)                'Function
            code.AppendLine("If (" + checker + ") Then" + vbCrLf)                         'If
            code.AppendLine("Return True" + vbCrLf)                                       'ST1
            code.AppendLine("End If" + vbCrLf)                                            'End if
            code.AppendLine("Return False" + vbCrLf)                                      'ST2
            code.AppendLine("End Function" + vbCrLf)                                      'End FUNCTION
            code.AppendLine("End Class" + vbCrLf)                                           'End Class

            Dim C As String = code.ToString()

            Dim results As CompilerResults
            results = compiler.CompileAssemblyFromSource(cp, C)
            If (results.Errors.HasErrors) Then
                Return False
            Else
                Dim assm As Assembly = results.CompiledAssembly
                Dim Minfo As MethodInfo = assm.GetType("M").GetMethod("Check")
                Dim R As Object = Minfo.Invoke(Nothing, Nothing)
                Return Convert.ToBoolean(R)
            End If


        End Function
               

    End Class

     

     

     

     

    代码量不大..写的不规范...抱歉

     

    qq   498483439

     

    msn luancfan@hotmail.com

     

     

    http://struggle-luan.blog.163.com

     

    我是新手!多多指教!

     

    2008年8月10日 14:09