views:

228

answers:

3

If I have a text stored in db, file, resource does not matter:

<code><%= Integer.MaxValue %></code>

Is there a way to do this:

Dim el As XElement = XElement.Parse({variable containing text above})

and to have it evaluate the expression Integer.MaxValue?

A: 

I think they key is understanding how embedded expressions work. I've asked this as a separate question.

Larsenal
+2  A: 

Short answer is no. Compiler knows how to parse this and replaces your source code with something that looks like one would do in C# code.

Test code:

Dim source As String = "a,s,d,f"
Dim ar As String() = source.Split(","c)
Dim el As XElement = <code>
                         <%= From s In ar Select s + "22" %>
                     </code>

Reflected code:

Dim VB$CG$t_i4$S0 As Integer
Dim ar As String() = "a,s,d,f".Split(New Char() { ","c })
Dim VB$t_ref$S0 As New XElement(XName.Get("code", ""))
VB$t_ref$S0.Add(ar.Select(Of String, String)(New Func(Of String, String)(AddressOf Test._Lambda$__1)))
Dim el As XElement = VB$t_ref$S0
epitka
Thanks for posting the final answer. Would you mind saying how you learned this?
John Saunders
I used reflector to decompile my assembly and look what compiler did with my code. You can find it at http://www.red-gate.com/products/reflector/
epitka
+1  A: 

Not easily. The closest you could get it to use CodeDOM to generate some code - you'll need to produce a wrapper module and a Function with Return, then put the expression from the database into that Return - compile it (and check for compile errors), run it, and see the result. All in all, this would be very slow.

Pavel Minaev
Do you know of any articles or resources that would help one wanting to attempt such a thing?
Dennis Palmer
http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx covers CodeDOM. It doesn't specifically cover XML literals, though, since that's a VB-specific feature, so you'd need to use a literal expression - http://msdn.microsoft.com/en-us/library/system.codedom.codesnippetexpression.aspx
Pavel Minaev