|
16 | 16 | using System;
|
17 | 17 | using System.Collections.Generic;
|
18 | 18 | using System.Reflection;
|
| 19 | +using System.Runtime.CompilerServices; |
19 | 20 | using System.Threading.Tasks;
|
20 | 21 | using OpenSilver.Internal;
|
21 | 22 |
|
@@ -66,105 +67,76 @@ public static int DumpAllJavascriptObjectsEveryMs
|
66 | 67 |
|
67 | 68 | private static T ConvertJavascriptResult<T>(object value)
|
68 | 69 | {
|
69 |
| - object converted; |
70 | 70 | Type t = typeof(T);
|
71 | 71 | if (t == typeof(string))
|
72 | 72 | {
|
73 |
| - if (IsRunningInTheSimulator) |
74 |
| - converted = INTERNAL_JSObjectReference.ToString(value); |
75 |
| - else |
76 |
| - converted = Convert.ToString(value); |
| 73 | + string s = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToString(value) : Convert.ToString(value); |
| 74 | + return Unsafe.As<string, T>(ref s); |
77 | 75 | }
|
78 | 76 | else if (t == typeof(double))
|
79 | 77 | {
|
80 |
| - if (IsRunningInTheSimulator) |
81 |
| - converted = INTERNAL_JSObjectReference.ToDouble(value); |
82 |
| - else |
83 |
| - converted = Convert.ToDouble(value); |
| 78 | + double d = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToDouble(value) : Convert.ToDouble(value); |
| 79 | + return Unsafe.As<double, T>(ref d); |
84 | 80 | }
|
85 | 81 | else if (t == typeof(int))
|
86 | 82 | {
|
87 |
| - if (IsRunningInTheSimulator) |
88 |
| - converted = INTERNAL_JSObjectReference.ToInt32(value); |
89 |
| - else |
90 |
| - converted = Convert.ToInt32(value); |
| 83 | + int i = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToInt32(value) : Convert.ToInt32(value); |
| 84 | + return Unsafe.As<int, T>(ref i); |
91 | 85 | }
|
92 | 86 | else if (t == typeof(bool))
|
93 | 87 | {
|
94 |
| - if (IsRunningInTheSimulator) |
95 |
| - converted = INTERNAL_JSObjectReference.ToBoolean(value); |
96 |
| - else |
97 |
| - converted = Convert.ToBoolean(value); |
| 88 | + bool b = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToBoolean(value) : Convert.ToBoolean(value); |
| 89 | + return Unsafe.As<bool, T>(ref b); |
98 | 90 | }
|
99 | 91 | else if (t == typeof(char))
|
100 | 92 | {
|
101 |
| - if (IsRunningInTheSimulator) |
102 |
| - converted = INTERNAL_JSObjectReference.ToChar(value); |
103 |
| - else |
104 |
| - converted = Convert.ToChar(value); |
| 93 | + char c = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToChar(value) : Convert.ToChar(value); |
| 94 | + return Unsafe.As<char, T>(ref c); |
105 | 95 | }
|
106 | 96 | else if (t == typeof(float))
|
107 | 97 | {
|
108 |
| - if (IsRunningInTheSimulator) |
109 |
| - converted = INTERNAL_JSObjectReference.ToSingle(value); |
110 |
| - else |
111 |
| - converted = Convert.ToSingle(value); |
| 98 | + float f = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToSingle(value) : Convert.ToSingle(value); |
| 99 | + return Unsafe.As<float, T>(ref f); |
112 | 100 | }
|
113 | 101 | else if (t == typeof(byte))
|
114 | 102 | {
|
115 |
| - if (IsRunningInTheSimulator) |
116 |
| - converted = INTERNAL_JSObjectReference.ToByte(value); |
117 |
| - else |
118 |
| - converted = Convert.ToByte(value); |
| 103 | + byte b = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToByte(value) : Convert.ToByte(value); |
| 104 | + return Unsafe.As<byte, T>(ref b); |
119 | 105 | }
|
120 | 106 | else if (t == typeof(uint))
|
121 | 107 | {
|
122 |
| - if (IsRunningInTheSimulator) |
123 |
| - converted = INTERNAL_JSObjectReference.ToUInt32(value); |
124 |
| - else |
125 |
| - converted = Convert.ToUInt32(value); |
| 108 | + uint i = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToUInt32(value) : Convert.ToUInt32(value); |
| 109 | + return Unsafe.As<uint, T>(ref i); |
126 | 110 | }
|
127 | 111 | else if (t == typeof(long))
|
128 | 112 | {
|
129 |
| - if (IsRunningInTheSimulator) |
130 |
| - converted = INTERNAL_JSObjectReference.ToInt64(value); |
131 |
| - else |
132 |
| - converted = Convert.ToInt64(value); |
| 113 | + long l = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToInt64(value) : Convert.ToInt64(value); |
| 114 | + return Unsafe.As<long, T>(ref l); |
133 | 115 | }
|
134 | 116 | else if (t == typeof(ulong))
|
135 | 117 | {
|
136 |
| - if (IsRunningInTheSimulator) |
137 |
| - converted = INTERNAL_JSObjectReference.ToUInt64(value); |
138 |
| - else |
139 |
| - converted = Convert.ToUInt64(value); |
| 118 | + ulong l = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToUInt64(value) : Convert.ToUInt64(value); |
| 119 | + return Unsafe.As<ulong, T>(ref l); |
140 | 120 | }
|
141 | 121 | else if (t == typeof(short))
|
142 | 122 | {
|
143 |
| - if (IsRunningInTheSimulator) |
144 |
| - converted = INTERNAL_JSObjectReference.ToInt16(value); |
145 |
| - else |
146 |
| - converted = Convert.ToInt16(value); |
| 123 | + short s = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToInt16(value) : Convert.ToInt16(value); |
| 124 | + return Unsafe.As<short, T>(ref s); |
147 | 125 | }
|
148 | 126 | else if (t == typeof(decimal))
|
149 | 127 | {
|
150 |
| - if (IsRunningInTheSimulator) |
151 |
| - converted = INTERNAL_JSObjectReference.ToDecimal(value); |
152 |
| - else |
153 |
| - converted = Convert.ToDecimal(value); |
| 128 | + decimal d = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToDecimal(value) : Convert.ToDecimal(value); |
| 129 | + return Unsafe.As<decimal, T>(ref d); |
154 | 130 | }
|
155 | 131 | else if (t == typeof(DateTime))
|
156 | 132 | {
|
157 |
| - if (IsRunningInTheSimulator) |
158 |
| - converted = INTERNAL_JSObjectReference.ToDateTime(value); |
159 |
| - else |
160 |
| - converted = Convert.ToDateTime(value); |
| 133 | + DateTime d = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToDateTime(value) : Convert.ToDateTime(value); |
| 134 | + return Unsafe.As<DateTime, T>(ref d); |
161 | 135 | }
|
162 | 136 | else
|
163 | 137 | {
|
164 | 138 | throw new ArgumentException($"Type '{t.FullName}' is not supported.");
|
165 | 139 | }
|
166 |
| - |
167 |
| - return (T)converted; |
168 | 140 | }
|
169 | 141 |
|
170 | 142 | public static T1 ExecuteJavaScriptGetResult<T1>(string javascript)
|
|
0 commit comments