| 1 |
#Region "Microsoft.VisualBasic::596f71846844bdb52590ca05b439c19d, Microsoft.VisualBasic.Core\Scripting\InputHandler.vb"
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#End Region
|
| 47 |
|
| 48 |
Imports System.Array
|
| 49 |
Imports System.Drawing
|
| 50 |
Imports System.IO
|
| 51 |
Imports System.Reflection
|
| 52 |
Imports System.Runtime.CompilerServices
|
| 53 |
Imports System.Text
|
| 54 |
Imports System.Text.RegularExpressions
|
| 55 |
Imports Microsoft.VisualBasic.ApplicationServices.Debugging.Logging
|
| 56 |
Imports Microsoft.VisualBasic.ComponentModel.Collection
|
| 57 |
Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
|
| 58 |
Imports Microsoft.VisualBasic.Imaging
|
| 59 |
Imports Microsoft.VisualBasic.Language
|
| 60 |
Imports Microsoft.VisualBasic.Language.Default
|
| 61 |
Imports Microsoft.VisualBasic.Scripting.Runtime
|
| 62 |
Imports Microsoft.VisualBasic.Serialization
|
| 63 |
Imports CLI = Microsoft.VisualBasic.CommandLine.CommandLine
|
| 64 |
|
| 65 |
Namespace Scripting
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
Public Module InputHandler
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
Public ReadOnly Property CasterString As New Dictionary(Of Type, LoadObject) From {
|
| 79 |
_
|
| 80 |
{GetType(String), Function(s$) s},
|
| 81 |
{GetType(Char), AddressOf Casting.CastChar},
|
| 82 |
{GetType(Integer), AddressOf Casting.CastInteger},
|
| 83 |
{GetType(Double), AddressOf Casting.ParseNumeric},
|
| 84 |
{GetType(Long), AddressOf Casting.CastLong},
|
| 85 |
{GetType(Boolean), AddressOf ParseBoolean},
|
| 86 |
{GetType(Char()), AddressOf Casting.CastCharArray},
|
| 87 |
{GetType(Date), AddressOf Casting.CastDate},
|
| 88 |
{GetType(StringBuilder), AddressOf Casting.CastStringBuilder},
|
| 89 |
{GetType(CLI), AddressOf Casting.CastCommandLine},
|
| 90 |
{GetType(Image), AddressOf Casting.CastImage},
|
| 91 |
{GetType(FileInfo), AddressOf Casting.CastFileInfo},
|
| 92 |
{GetType(Graphics2D), AddressOf Casting.CastGDIPlusDeviceHandle},
|
| 93 |
{GetType(Color), AddressOf TranslateColor},
|
| 94 |
{GetType(Font), AddressOf Casting.CastFont},
|
| 95 |
{GetType(System.Net.IPEndPoint), AddressOf Casting.CastIPEndPoint},
|
| 96 |
{GetType(LogFile), AddressOf Casting.CastLogFile},
|
| 97 |
{GetType(Process), AddressOf Casting.CastProcess},
|
| 98 |
{GetType(RegexOptions), AddressOf Casting.CastRegexOptions},
|
| 99 |
{GetType(Single), AddressOf Casting.CastSingle},
|
| 100 |
{GetType(Decimal), Function(x) CDec(x)},
|
| 101 |
{GetType(Point), AddressOf PointParser},
|
| 102 |
{GetType(PointF), AddressOf FloatPointParser},
|
| 103 |
{GetType(Size), AddressOf SizeParser},
|
| 104 |
{GetType(SizeF), AddressOf FloatSizeParser},
|
| 105 |
{GetType(DoubleRange), AddressOf NumericRangeParser}
|
| 106 |
}
|
| 107 |
|
| 108 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 109 |
Public Function StringParser(type As Type) As DefaultValue(Of Func(Of String, Object))
|
| 110 |
Return New Func(Of String, Object)(Function(s$) s.CTypeDynamic(type))
|
| 111 |
End Function
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
<Extension> Public Function CTypeDynamic(expression$, target As Type) As Object
|
| 121 |
If expression.StringEmpty Then
|
| 122 |
Return Nothing
|
| 123 |
End If
|
| 124 |
If _CasterString.ContainsKey(target) Then
|
| 125 |
Dim caster As LoadObject = _CasterString(target)
|
| 126 |
Return caster(expression$)
|
| 127 |
End If
|
| 128 |
|
| 129 |
Static errCaster As New Index(Of String)
|
| 130 |
|
| 131 |
If expression.Length < 100 Then
|
| 132 |
|
| 133 |
|
| 134 |
If errCaster.IndexOf(expression & "|" & target.FullName) > -1 Then
|
| 135 |
|
| 136 |
Return Nothing
|
| 137 |
End If
|
| 138 |
End If
|
| 139 |
|
| 140 |
Try
|
| 141 |
Return Conversion.CTypeDynamic(expression, target)
|
| 142 |
Catch ex As Exception
|
| 143 |
|
| 144 |
If expression.Length < 100 Then
|
| 145 |
errCaster.Add(expression & "|" & target.FullName)
|
| 146 |
End If
|
| 147 |
|
| 148 |
ex = New Exception($"{expression} ==> {target.FullName}", ex)
|
| 149 |
Call App.LogException(ex, MethodBase.GetCurrentMethod.GetFullName)
|
| 150 |
Return Nothing
|
| 151 |
End Try
|
| 152 |
End Function
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
<Extension> Public Function CTypeDynamic(Of T)(expr$, Optional [default] As T = Nothing) As T
|
| 162 |
Dim value As Object = CTypeDynamic(expr, GetType(T))
|
| 163 |
|
| 164 |
If value Is Nothing Then
|
| 165 |
Return [default]
|
| 166 |
Else
|
| 167 |
Return DirectCast(value, T)
|
| 168 |
End If
|
| 169 |
End Function
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 177 |
Public Function DefaultTextParser(Of T)() As DefaultValue(Of IStringParser(Of T))
|
| 178 |
Return New IStringParser(Of T)(AddressOf CTypeDynamic(Of T)).AsDefault
|
| 179 |
End Function
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 188 |
Public Function IsPrimitive(targetType As Type) As Boolean
|
| 189 |
Return CasterString.ContainsKey(targetType)
|
| 190 |
End Function
|
| 191 |
|
| 192 |
|
| 193 |
Dynamics updates the capability of function <see cref="InputHandler.CTypeDynamic(String, Type)"/>,
|
| 194 |
<see cref="InputHandler.CTypeDynamic(Of T)(String, T)"/> and
|
| 195 |
<see cref="InputHandler.IsPrimitive(Type)"/>
|
| 196 |
|
| 197 |
<param name="briefName"></param>
|
| 198 |
<param name="stringConvertType"></param>
|
| 199 |
<param name="cast"></param>
|
| 200 |
Public Sub CapabilityPromise(briefName$, stringConvertType As Type, cast As LoadObject)
|
| 201 |
With _CasterString
|
| 202 |
If .ContainsKey(stringConvertType) Then
|
| 203 |
Call .Remove(stringConvertType)
|
| 204 |
End If
|
| 205 |
|
| 206 |
Call .Add(stringConvertType, cast)
|
| 207 |
End With
|
| 208 |
|
| 209 |
Dim key$ = briefName.ToLower
|
| 210 |
|
| 211 |
If Types.ContainsKey(key) Then
|
| 212 |
Call Types.Remove(key)
|
| 213 |
End If
|
| 214 |
Call Types.Add(key, stringConvertType)
|
| 215 |
End Sub
|
| 216 |
|
| 217 |
|
| 218 |
Enumerate all of the types that can be handled in this module. All of the key string is in lower case.(键值都是小写的)
|
| 219 |
|
| 220 |
Public ReadOnly Property Types As New SortedDictionary(Of String, Type) From {
|
| 221 |
_
|
| 222 |
{"string", GetType(String)},
|
| 223 |
{"integer", GetType(Integer)},
|
| 224 |
{"int32", GetType(Integer)},
|
| 225 |
{"int64", GetType(Long)},
|
| 226 |
{"long", GetType(Long)},
|
| 227 |
{"double", GetType(Double)},
|
| 228 |
{"byte", GetType(Byte)},
|
| 229 |
{"date", GetType(Date)},
|
| 230 |
{"logfile", GetType(LogFile)},
|
| 231 |
{"color", GetType(Color)},
|
| 232 |
{"process", GetType(Process)},
|
| 233 |
{"font", GetType(Font)},
|
| 234 |
{"image", GetType(Image)},
|
| 235 |
{"fileinfo", GetType(IO.FileInfo)},
|
| 236 |
{"ipaddress", GetType(System.Net.IPAddress)},
|
| 237 |
{"commandline", GetType(CLI)},
|
| 238 |
{"gdi+", GetType(Graphics2D)},
|
| 239 |
{"stringbuilder", GetType(StringBuilder)},
|
| 240 |
{"boolean", GetType(Boolean)},
|
| 241 |
{"char()", GetType(Char())},
|
| 242 |
{"string()", GetType(String())},
|
| 243 |
{"integer()", GetType(Integer())},
|
| 244 |
{"double()", GetType(Double())},
|
| 245 |
{"bitmap", GetType(Bitmap)},
|
| 246 |
{"object", GetType(Object)},
|
| 247 |
{"regexoptions", GetType(RegexOptions)}
|
| 248 |
}
|
| 249 |
|
| 250 |
|
| 251 |
Get .NET <see cref="Type"/> definition info from its name.
|
| 252 |
(类型获取失败会返回空值,大小写不敏感)
|
| 253 |
|
| 254 |
<param name="name">Case insensitive.(类型的名称简写)</param>
|
| 255 |
<param name="ObjectGeneric">是否出错的时候返回<see cref="Object"/>类型,默认返回Nothing</param>
|
| 256 |
|
| 257 |
Public Function [GetType](name As Value(Of String), Optional ObjectGeneric As Boolean = False) As Type
|
| 258 |
If Types.ContainsKey(name = name.Value.ToLower) Then
|
| 259 |
Return Types(name)
|
| 260 |
Else
|
| 261 |
Dim typeInfo As Type = Type.GetType(name, False, True)
|
| 262 |
|
| 263 |
If typeInfo Is Nothing AndAlso ObjectGeneric Then
|
| 264 |
Return GetType(Object)
|
| 265 |
Else
|
| 266 |
Return typeInfo
|
| 267 |
End If
|
| 268 |
End If
|
| 269 |
End Function
|
| 270 |
|
| 271 |
Public Function [GetType](obj As Object, Optional ObjectGeneric As Boolean = False) As Type
|
| 272 |
If obj Is Nothing Then
|
| 273 |
If ObjectGeneric Then
|
| 274 |
Return GetType(Object)
|
| 275 |
Else
|
| 276 |
Return Nothing
|
| 277 |
End If
|
| 278 |
Else
|
| 279 |
Return obj.GetType
|
| 280 |
End If
|
| 281 |
End Function
|
| 282 |
|
| 283 |
|
| 284 |
<see cref="System.Type"/> information for <see cref="System.String"/> type from GetType operator
|
| 285 |
|
| 286 |
|
| 287 |
Public ReadOnly Property [String] As Type = GetType(String)
|
| 288 |
|
| 289 |
|
| 290 |
Does the <paramref name="inputtype"/> type can be cast to type <paramref name="DefType"/>.
|
| 291 |
(主要为了方便减少脚本编程模块的代码)
|
| 292 |
|
| 293 |
<param name="inputType"></param>
|
| 294 |
<param name="DefType"></param>
|
| 295 |
|
| 296 |
|
| 297 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 298 |
Public Function Convertible(inputType As Type, DefType As Type) As Boolean
|
| 299 |
Return inputType.Equals([String]) AndAlso CasterString.ContainsKey(DefType)
|
| 300 |
End Function
|
| 301 |
|
| 302 |
|
| 303 |
<seealso cref="CStrSafe"/>, 出现错误的时候总是会返回空字符串的
|
| 304 |
|
| 305 |
<param name="obj"></param>
|
| 306 |
|
| 307 |
|
| 308 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 309 |
Public Function ToString(obj As Object, Optional null$ = "") As String
|
| 310 |
Return CStrSafe(obj, null)
|
| 311 |
End Function
|
| 312 |
|
| 313 |
|
| 314 |
The parameter <paramref name="obj"/> should implements a <see cref="IEnumerable"/> interface on the type. and then DirectCast object to target type.
|
| 315 |
|
| 316 |
|
| 317 |
<param name="obj"></param>
|
| 318 |
|
| 319 |
Public Function CastArray(Of T)(obj As Object) As T()
|
| 320 |
Dim array = DirectCast(obj, IEnumerable)
|
| 321 |
Dim data = (From val In array Select DirectCast(val, T)).ToArray
|
| 322 |
Return data
|
| 323 |
End Function
|
| 324 |
|
| 325 |
Public Function CastArray(obj As Object, type As Type) As Object
|
| 326 |
If Array.IndexOf(obj.GetType.GetInterfaces, GetType(IEnumerable)) = -1 Then
|
| 327 |
Return obj
|
| 328 |
End If
|
| 329 |
|
| 330 |
Dim source As IEnumerable = DirectCast(obj, IEnumerable)
|
| 331 |
Dim data = LinqAPI.Exec(Of Object) _
|
| 332 |
_
|
| 333 |
() <= From val As Object
|
| 334 |
In source
|
| 335 |
Let value = Conversion.CTypeDynamic(val, type)
|
| 336 |
Select value
|
| 337 |
|
| 338 |
Return [DirectCast](data, type)
|
| 339 |
End Function
|
| 340 |
|
| 341 |
|
| 342 |
Cast the <see cref="Object"/> array to typed object array.
|
| 343 |
|
| 344 |
<param name="array"></param>
|
| 345 |
<param name="type">数组里面的元素的类型</param>
|
| 346 |
|
| 347 |
Public Function [DirectCast](array As Object(), type As Type) As Object
|
| 348 |
Dim out = CreateInstance(type, array.Length)
|
| 349 |
Call Copy(array, out, array.Length)
|
| 350 |
Return out
|
| 351 |
End Function
|
| 352 |
End Module
|
| 353 |
End Namespace
|