Created
November 7, 2019 07:19
-
-
Save MaDOS/4904683d461d022e4b24f4080009ae5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Program | |
{ | |
public enum DataType | |
{ | |
Single, | |
Double, | |
UInt16, | |
UInt32, | |
UInt64, | |
Int16, | |
Int32, | |
Int64, | |
Byte | |
} | |
static void Main(string[] args) | |
{ | |
dynamic value1 = 5; | |
dynamic value2 = 6; | |
var casted = CastToType(value1, DataType.Int16); | |
var casted1 = CastToTypeExpression(value2, DataType.Int16); | |
var type = casted.GetType(); // Int16 | |
var type1 = casted1.GetType(); // Double | |
var bytes = BitConverter.GetBytes(casted); // byte arr with 2 el => [5, 0] <- expected behavior | |
var bytes1 = BitConverter.GetBytes(casted1); // byte arr with 8 el => [0, 0, 0, 0, 0, 0, 24, 64] | |
} | |
public static dynamic CastToType(dynamic value, DataType type) | |
{ | |
switch (type) | |
{ | |
case DataType.Byte: | |
return (byte)value; | |
case DataType.Double: | |
return (double)value; | |
case DataType.Int16: | |
return (short)value; | |
case DataType.Int32: | |
return (int)value; | |
case DataType.Int64: | |
return (long)value; | |
case DataType.Single: | |
return (float)value; | |
case DataType.UInt16: | |
return (ushort)value; | |
case DataType.UInt32: | |
return (uint)value; | |
case DataType.UInt64: | |
return (ulong)value; | |
default: throw new InvalidCastException(); | |
} | |
} | |
public static dynamic CastToTypeExpression(dynamic value, DataType type) | |
{ | |
return type switch | |
{ | |
DataType.Byte => (byte)value, | |
DataType.Double => (double)value, | |
DataType.Int16 => (short)value, | |
DataType.Int32 => (int)value, | |
DataType.Int64 => (long)value, | |
DataType.Single => (float)value, | |
DataType.UInt16 => (ushort)value, | |
DataType.UInt32 => (uint)value, | |
DataType.UInt64 => (ulong)value, | |
_ => throw new InvalidCastException(), | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CSharp.RuntimeBinder; | |
using System; | |
using System.Diagnostics; | |
using System.Reflection; | |
using System.Runtime.CompilerServices; | |
using System.Security; | |
using System.Security.Permissions; | |
[assembly: CompilationRelaxations(8)] | |
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] | |
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)] | |
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] | |
[assembly: AssemblyVersion("0.0.0.0")] | |
[module: UnverifiableCode] | |
internal class Program | |
{ | |
public enum DataType | |
{ | |
Single, | |
Double, | |
UInt16, | |
UInt32, | |
UInt64, | |
Int16, | |
Int32, | |
Int64, | |
Byte | |
} | |
[CompilerGenerated] | |
private static class <>o__1 | |
{ | |
public static CallSite<Func<CallSite, Type, object, DataType, object>> <>p__0; | |
public static CallSite<Func<CallSite, Type, object, DataType, object>> <>p__1; | |
public static CallSite<Func<CallSite, object, object>> <>p__2; | |
public static CallSite<Func<CallSite, object, object>> <>p__3; | |
public static CallSite<Func<CallSite, Type, object, object>> <>p__4; | |
public static CallSite<Func<CallSite, Type, object, object>> <>p__5; | |
} | |
[CompilerGenerated] | |
private static class <>o__2 | |
{ | |
public static CallSite<Func<CallSite, object, byte>> <>p__0; | |
public static CallSite<Func<CallSite, object, double>> <>p__1; | |
public static CallSite<Func<CallSite, object, short>> <>p__2; | |
public static CallSite<Func<CallSite, object, int>> <>p__3; | |
public static CallSite<Func<CallSite, object, long>> <>p__4; | |
public static CallSite<Func<CallSite, object, float>> <>p__5; | |
public static CallSite<Func<CallSite, object, ushort>> <>p__6; | |
public static CallSite<Func<CallSite, object, uint>> <>p__7; | |
public static CallSite<Func<CallSite, object, ulong>> <>p__8; | |
} | |
[CompilerGenerated] | |
private static class <>o__3 | |
{ | |
public static CallSite<Func<CallSite, object, byte>> <>p__0; | |
public static CallSite<Func<CallSite, object, double>> <>p__1; | |
public static CallSite<Func<CallSite, object, short>> <>p__2; | |
public static CallSite<Func<CallSite, object, int>> <>p__3; | |
public static CallSite<Func<CallSite, object, long>> <>p__4; | |
public static CallSite<Func<CallSite, object, float>> <>p__5; | |
public static CallSite<Func<CallSite, object, ushort>> <>p__6; | |
public static CallSite<Func<CallSite, object, uint>> <>p__7; | |
public static CallSite<Func<CallSite, object, ulong>> <>p__8; | |
} | |
private static void Main(string[] args) | |
{ | |
object arg = 5; | |
object arg2 = 6; | |
if (<>o__1.<>p__0 == null) | |
{ | |
Type typeFromHandle = typeof(Program); | |
CSharpArgumentInfo[] obj = new CSharpArgumentInfo[3]; | |
obj[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsStaticType, null); | |
obj[1] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null); | |
obj[2] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.Constant, null); | |
<>o__1.<>p__0 = CallSite<Func<CallSite, Type, object, DataType, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "CastToType", null, typeFromHandle, obj)); | |
} | |
object obj2 = <>o__1.<>p__0.Target(<>o__1.<>p__0, typeof(Program), arg, DataType.Int16); | |
if (<>o__1.<>p__1 == null) | |
{ | |
Type typeFromHandle2 = typeof(Program); | |
CSharpArgumentInfo[] obj3 = new CSharpArgumentInfo[3]; | |
obj3[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsStaticType, null); | |
obj3[1] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null); | |
obj3[2] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.Constant, null); | |
<>o__1.<>p__1 = CallSite<Func<CallSite, Type, object, DataType, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "CastToTypeExpression", null, typeFromHandle2, obj3)); | |
} | |
object obj4 = <>o__1.<>p__1.Target(<>o__1.<>p__1, typeof(Program), arg2, DataType.Int16); | |
if (<>o__1.<>p__2 == null) | |
{ | |
Type typeFromHandle3 = typeof(Program); | |
CSharpArgumentInfo[] obj5 = new CSharpArgumentInfo[1]; | |
obj5[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null); | |
<>o__1.<>p__2 = CallSite<Func<CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "GetType", null, typeFromHandle3, obj5)); | |
} | |
object obj6 = <>o__1.<>p__2.Target(<>o__1.<>p__2, obj2); | |
if (<>o__1.<>p__3 == null) | |
{ | |
Type typeFromHandle4 = typeof(Program); | |
CSharpArgumentInfo[] obj7 = new CSharpArgumentInfo[1]; | |
obj7[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null); | |
<>o__1.<>p__3 = CallSite<Func<CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "GetType", null, typeFromHandle4, obj7)); | |
} | |
object obj8 = <>o__1.<>p__3.Target(<>o__1.<>p__3, obj4); | |
if (<>o__1.<>p__4 == null) | |
{ | |
Type typeFromHandle5 = typeof(Program); | |
CSharpArgumentInfo[] obj9 = new CSharpArgumentInfo[2]; | |
obj9[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsStaticType, null); | |
obj9[1] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null); | |
<>o__1.<>p__4 = CallSite<Func<CallSite, Type, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "GetBytes", null, typeFromHandle5, obj9)); | |
} | |
object obj10 = <>o__1.<>p__4.Target(<>o__1.<>p__4, typeof(BitConverter), obj2); | |
if (<>o__1.<>p__5 == null) | |
{ | |
Type typeFromHandle6 = typeof(Program); | |
CSharpArgumentInfo[] obj11 = new CSharpArgumentInfo[2]; | |
obj11[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsStaticType, null); | |
obj11[1] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null); | |
<>o__1.<>p__5 = CallSite<Func<CallSite, Type, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "GetBytes", null, typeFromHandle6, obj11)); | |
} | |
object obj12 = <>o__1.<>p__5.Target(<>o__1.<>p__5, typeof(BitConverter), obj4); | |
} | |
[return: Dynamic] | |
public static object CastToType([Dynamic] object value, DataType type) | |
{ | |
switch (type) | |
{ | |
case DataType.Byte: | |
if (<>o__2.<>p__0 == null) | |
{ | |
<>o__2.<>p__0 = CallSite<Func<CallSite, object, byte>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(byte), typeof(Program))); | |
} | |
return <>o__2.<>p__0.Target(<>o__2.<>p__0, value); | |
case DataType.Double: | |
if (<>o__2.<>p__1 == null) | |
{ | |
<>o__2.<>p__1 = CallSite<Func<CallSite, object, double>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(double), typeof(Program))); | |
} | |
return <>o__2.<>p__1.Target(<>o__2.<>p__1, value); | |
case DataType.Int16: | |
if (<>o__2.<>p__2 == null) | |
{ | |
<>o__2.<>p__2 = CallSite<Func<CallSite, object, short>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(short), typeof(Program))); | |
} | |
return <>o__2.<>p__2.Target(<>o__2.<>p__2, value); | |
case DataType.Int32: | |
if (<>o__2.<>p__3 == null) | |
{ | |
<>o__2.<>p__3 = CallSite<Func<CallSite, object, int>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(int), typeof(Program))); | |
} | |
return <>o__2.<>p__3.Target(<>o__2.<>p__3, value); | |
case DataType.Int64: | |
if (<>o__2.<>p__4 == null) | |
{ | |
<>o__2.<>p__4 = CallSite<Func<CallSite, object, long>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(long), typeof(Program))); | |
} | |
return <>o__2.<>p__4.Target(<>o__2.<>p__4, value); | |
case DataType.Single: | |
if (<>o__2.<>p__5 == null) | |
{ | |
<>o__2.<>p__5 = CallSite<Func<CallSite, object, float>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(float), typeof(Program))); | |
} | |
return <>o__2.<>p__5.Target(<>o__2.<>p__5, value); | |
case DataType.UInt16: | |
if (<>o__2.<>p__6 == null) | |
{ | |
<>o__2.<>p__6 = CallSite<Func<CallSite, object, ushort>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(ushort), typeof(Program))); | |
} | |
return <>o__2.<>p__6.Target(<>o__2.<>p__6, value); | |
case DataType.UInt32: | |
if (<>o__2.<>p__7 == null) | |
{ | |
<>o__2.<>p__7 = CallSite<Func<CallSite, object, uint>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(uint), typeof(Program))); | |
} | |
return <>o__2.<>p__7.Target(<>o__2.<>p__7, value); | |
case DataType.UInt64: | |
if (<>o__2.<>p__8 == null) | |
{ | |
<>o__2.<>p__8 = CallSite<Func<CallSite, object, ulong>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(ulong), typeof(Program))); | |
} | |
return <>o__2.<>p__8.Target(<>o__2.<>p__8, value); | |
default: | |
throw new InvalidCastException(); | |
} | |
} | |
[return: Dynamic] | |
public static object CastToTypeExpression([Dynamic] object value, DataType type) | |
{ | |
double num; | |
switch (type) | |
{ | |
case DataType.Byte: | |
if (<>o__3.<>p__0 == null) | |
{ | |
<>o__3.<>p__0 = CallSite<Func<CallSite, object, byte>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(byte), typeof(Program))); | |
} | |
num = (int)<>o__3.<>p__0.Target(<>o__3.<>p__0, value); | |
break; | |
case DataType.Double: | |
if (<>o__3.<>p__1 == null) | |
{ | |
<>o__3.<>p__1 = CallSite<Func<CallSite, object, double>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(double), typeof(Program))); | |
} | |
num = <>o__3.<>p__1.Target(<>o__3.<>p__1, value); | |
break; | |
case DataType.Int16: | |
if (<>o__3.<>p__2 == null) | |
{ | |
<>o__3.<>p__2 = CallSite<Func<CallSite, object, short>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(short), typeof(Program))); | |
} | |
num = <>o__3.<>p__2.Target(<>o__3.<>p__2, value); | |
break; | |
case DataType.Int32: | |
if (<>o__3.<>p__3 == null) | |
{ | |
<>o__3.<>p__3 = CallSite<Func<CallSite, object, int>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(int), typeof(Program))); | |
} | |
num = <>o__3.<>p__3.Target(<>o__3.<>p__3, value); | |
break; | |
case DataType.Int64: | |
if (<>o__3.<>p__4 == null) | |
{ | |
<>o__3.<>p__4 = CallSite<Func<CallSite, object, long>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(long), typeof(Program))); | |
} | |
num = <>o__3.<>p__4.Target(<>o__3.<>p__4, value); | |
break; | |
case DataType.Single: | |
if (<>o__3.<>p__5 == null) | |
{ | |
<>o__3.<>p__5 = CallSite<Func<CallSite, object, float>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(float), typeof(Program))); | |
} | |
num = <>o__3.<>p__5.Target(<>o__3.<>p__5, value); | |
break; | |
case DataType.UInt16: | |
if (<>o__3.<>p__6 == null) | |
{ | |
<>o__3.<>p__6 = CallSite<Func<CallSite, object, ushort>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(ushort), typeof(Program))); | |
} | |
num = (int)<>o__3.<>p__6.Target(<>o__3.<>p__6, value); | |
break; | |
case DataType.UInt32: | |
if (<>o__3.<>p__7 == null) | |
{ | |
<>o__3.<>p__7 = CallSite<Func<CallSite, object, uint>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(uint), typeof(Program))); | |
} | |
num = <>o__3.<>p__7.Target(<>o__3.<>p__7, value); | |
break; | |
case DataType.UInt64: | |
if (<>o__3.<>p__8 == null) | |
{ | |
<>o__3.<>p__8 = CallSite<Func<CallSite, object, ulong>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(ulong), typeof(Program))); | |
} | |
num = <>o__3.<>p__8.Target(<>o__3.<>p__8, value); | |
break; | |
default: | |
throw new InvalidCastException(); | |
} | |
double num2 = num; | |
return num2; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": 1, | |
"target": "C#", | |
"mode": "Debug" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment