Emission

Team Fly 

Page 213

However, imagine my excitement when I discovered the new Parse method. For a moment I thought my dream had come true and this was the built-in way to parse a string. No. Instead, they are using the term parse in a way it's never been used in English before. The Parse method doesn't parse (examine, divide into components); it casts.

Whatever. You can use it to change a string into various numeric data types.

In this example, I merely check for string or int32 types. However, in a real-world application you would include a Case to handle each possible data type that might be discovered as a parameter in an unknown assembly.

Each parameter's correct data type is created and added to a parameter array (p) in this loop:

         For Each s In split             'figure out parameter's variable type             Select Case paramInfo(C).ParameterType.Name                 Case ''String" 'case sensitive                     p(C) = split(C)                 Case "Int32" Dim NewInt As Integer = Integer.Parse(split(C)) 'turn string into integer                 p(C) = NewInt             End Select             C += 1         Next s 

And, finally, you use the Invoke method to pass the array of parameters (p) to the instantiated method (obj):

 mInfo.Invoke(obj, p) 

If there are no parameters to pass to an instantiated method, your job is easier. Just pass Nothing:

 mInfo.Invoke(obj, Nothing) 

Emission

If you think reflection has a bit of a Twilight Zone quality to it, just wait until you find out about its ability to emit code. The Reflection.Emit namespace contains facilities that permit you to generate code at runtime. Yes, generate code.

It's not VB.NET source code; it's Intermediate Language (IL), so the drawback is that you have to use a kind of opcode (assembly language) low-level programming. It's so specialized that few of us are likely to learn the language in order to emit. It features the usual assembly-language specificity, though in fact most of the instructions in this language should be familiar to you (the usual features

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net