问题
I'm taking a VB.Net DLL and turning it into a Portable Class Library. Once I got all of the classes moved into the new project for the PCL, Visual Studio started throwing errors for a lot of common VB syntax that I thought would still work just fine. Some examples:
- LCase
- InStr
- Left
- Mid
- On Error GoTo 0
- Err
Is it possible there is just some option or include I need to have to get these to work?
回答1:
When using portable to target the down-level platforms (.NET 4.0, Silverlight, Windows Phone, Xbox), we do not support the majority of the features that are exposed within Microsoft.VisualBasic.dll.
Instead, we make use of the embedded runtime feature. This embeds certain functionality that would traditionally be found in Microsoft.VisualBasic.dll, into the resulting binary itself. The features that are supported are called out on this page, under the /vbruntime* section: http://msdn.microsoft.com/en-us/library/bb531259.aspx.
When targeting .NET 4.5 & Windows Store apps only, then you do get access to the traditional Microsoft.VisualBasic.dll.
As a workaround, to help you move to portable, you can define your own module that bridges the old VB functions to their .NET equivalents:
Public Module VisualBasicBridge
Public Function LCase(value As String) As String
Return value.ToLower()
End Function
End Module
As far as On Error, I'm not aware of a good way to bridging that without providing your own implementation of Microsoft.VisualBasic, and passing that via the /vbruntime switch/msbuild property.
回答2:
You must use methods of assemblies that are supported by Portable Class Libraries (see Assemblies section). You'll be able to find equivelants to the methods that aren't working for you (ex = SubString, ToUpper, ToLower, IndexOf, etc).
回答3:
You can create those methods so that you don't have to update tons of your legacy code. Most of it is very straightforward, the biggest difference comes with the string functions where the legacy VB functions use a 1 based index and .Net uses a 0 index. To give you an example, here is the Mid function recreated to behave like VB (created like an extension method here):
''' <summary>
''' Simulates the same functionality provide by the traditional 1 based index Mid function.
''' </summary>
''' <param name="str"></param>
''' <param name="startPos"></param>
''' <param name="length"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()> _
Public Function Mid(ByVal str As String, ByVal startPos As Integer, ByVal length As Integer) As String
Return str.Substring(startPos - 1, length)
End Function
Here's a few more from your list and/or commonly used ones:
''' <summary>
''' Extension to the Visual Basic Left function
''' </summary>
''' <param name="str"></param>
''' <param name="length"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()> _
Public Function [Left](ByVal str As String, ByVal length As Integer) As String
Return str.Substring(0, length)
End Function
''' <summary>
''' Extension to the Visual Basic Right function
''' </summary>
''' <param name="str"></param>
''' <param name="length"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()> _
Public Function [Right](ByVal str As String, ByVal length As Integer) As String
Return str.Substring(str.Length - length, length)
End Function
''' <summary>
''' Determines whether a string is a numeric value. This implementation uses Decimal.TryParse to produce it's value.
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()> _
Public Function [IsNumeric](str As String) As Boolean
Dim result As Decimal = 0
Return Decimal.TryParse(str, result)
End Function
<Extension()> _
Public Function LCase(str As String) As String
Return str.ToLower
End Function
回答4:
All think those methods are in Microsoft.VisualBasic namespace. You can replace them with standard ones:
LCase => string.ToLower() InStr => string.IndexOf() ....
Replace "on error" with regular try/catch
Best regards
来源:https://stackoverflow.com/questions/12425887/portable-class-library-not-supporting-many-vb-methods