1

I am trying to convert the code inside a function into a string. Is there any way to do this in VB.Net or C#.Net? For example:

Public Function Test() As String
    Dim timestr As String = Now().ToString()
    Return timestr
End Function

Then somehow convert the above into a string, i.e…

Dim funcstr As String = Test.ToString()

which funcstr would equal "Dim timestr As String = Now().ToString()Return timestr"

Thanks

EDIT

I marked JaredPar's answer because it helped solve my problem, even though what I'm asking isn't possible. My purpose was to have a duplicate copy of very sensitive and complicated code, and when the class is instantiated, compare the two and make sure they are exact just in case I accidentally changed a character (human error) during my coding. His solution allows me to accomplish this task. Here is my updated code from Jared's solution. Thanks!

    Dim _type As Type = Me.GetType

    Dim _method As System.Reflection.MethodInfo = _type.GetMethod("Test")
    Dim _bytes() As Byte = _method.GetMethodBody.GetILAsByteArray()

    Dim _method2 As System.Reflection.MethodInfo = _type.GetMethod("Test2")
    Dim _bytes2() As Byte = _method2.GetMethodBody.GetILAsByteArray()

    MsgBox(_bytes.SequenceEqual(_bytes2))
0

2 Answers 2

4

Is there any way to [get the source code of a function] ..?

No, not really.

While a function can be "decompiled" - such as done in tools like dotPeek and ILSpy which allow the viewing of "[decompiled] source" - the original function source is irreversibly transformed to CIL when it is compiled. (While decompiling is possible, it is not "standard" or "trivial" to convert CIL back to VB.NET/C# source and the resulting code will generally not be identical to the original source text.)

But now to the Y question: What is the goal of converting the function to a string representation?

Sign up to request clarification or add additional context in comments.

2 Comments

To have a duplicate copy of very sensitive code that I can perform a comparison on class instantiation just in case of human error, i.e. i accidentally change 1 number. Thanks!
@scuzzlebuzzle That sounds more like a job for a development tool or process - i.e. file hashes, version control (maybe with commit hooks), and a review/acceptance process. Also, make sure to have the appropriate unit tests. There is, unfortunately, no way to eliminate "human error" from standard programming, only to mitigate it.
3

This is not possible because compiled methods aren't stored as string values but as IL. All languages compile to IL and it's the universal representation of .Net programs. It is possible to retrieve this IL by reflection if that is what you desire

Dim type = TypeOf(TestContainer)
Dim method = type.GetMethod("Test")
Dim il = method.MethodBody.GetILAsByteArray();

Converting this back to textual form though is not a simple matter. It requires identifying patterns in order to change a try / finally to using and so forth.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.