开发者

How to get the variable names types, and values in the current class or Method in VB.net?

Hi I am working on a project where my class has to execute VB code provided by the user, to make it simple i am trying to recreate my own eval function, i am using the following code i found on the web to do this task

Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO
Namespace PAB.Util
    Public Class EvalProvider

        Public Function VS_Eval(ByVal vbCode As String) As Object

            Dim c As VBCodeProvider = New VBCodeProvider
            Dim icc As ICodeCompiler = c.CreateCompiler()
            Dim cp As CompilerParameters = New CompilerParameters

            cp.ReferencedAssemblies.Add("system.dll")
            cp.ReferencedAssemblies.Add("system.xml.dll")
            cp.ReferencedAssemblies.Add("system.data.dll")
            ' Sample code for adding your own referenced assemblies
            'cp.ReferencedAssemblies.Add("c:\yourProjectDir\bin\YourBaseClass.dll")
            'cp.ReferencedAssemblies.Add("YourBaseclass.dll")
            cp.CompilerOptions = "/t:library"
            cp.GenerateInMemory = True
            Dim sb As StringBuilder = New StringBuilder("")
            sb.Append("Imports System" & vbCrLf)
            sb.Append("Imports System.Xml" & vbCrLf)
            sb.Append("Imports System.Data" & vbCrLf)
            sb.Append("Imports System.Data.SqlClient" & vbCrLf)
            sb.Append("Namespace PAB  " & vbCrLf)
            sb.Append("Class PABLib " & vbCrLf)

            sb.Append("public function  EvalCode() as Object " & vbCrLf)

            'sb.Append("YourNamespace.YourBaseClass thisObject = New YourNamespace.YourBaseClass()")
            sb.Append(vbCode & vbCrLf)
            sb.Append("End Function " & vbCrLf)
            sb.Append("End Class " & vbCrLf)
            sb.Append("End Namespace" & vbCrLf)
            Debug.WriteLine(sb.ToString()) ' look at this to debug your eval string
            Dim cr As CompilerResults = icc.CompileAssemblyFromSource(cp, sb.ToString())
            Dim a As System.Reflection.Assembly = cr.CompiledAssembly
            Dim o As Object
            Dim mi As MethodInfo
            o = a.CreateInstance("PAB.PABLib")
            Dim t As Type = o.GetType()
            mi = t.GetMethod("EvalCode")开发者_如何学运维
            Dim s As Object
            s = mi.Invoke(o, Nothing)
            Return s



        End Function
    End Class
End Namespace

The problem with code is that it can't access any variables or there values, so i have decided to get the variable names, there values types and there types dynamically and recreate them in the class that is being created dynamically.

Please suggest a way to get the variable names there types and values in the current class or method, so that i can recreate them, and execute the user passed code, the user knows what variables are in the current class or method and there datatypes but he don't know there values as they may have changed, so he can't initialize them.

Is there a way to do this, this code will be called in an asp.net page on page_load event, the code passed by the user is stored in the variable vbCode that is passed as a parameter.

If there is any other method to do this, please suggest

Thank you


I have finally found the solution of how to get the variables and values of the current instance of a class.

using System.Reflection; it is very easy to achieve, all you have to write is the following line:

Dim fields As FieldInfo() = Me.GetType().GetFields()

For Each fld As FieldInfo In fields
    Dim name As String = fld.Name
    Dim value = fld.GetValue(Me)
    Dim typ As Type = fld.FieldType
Next

Note : It only works for public variables


This may help you?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜