Skip to content

Instantly share code, notes, and snippets.

@sunghwan2789
Last active November 15, 2022 10:42
Show Gist options
  • Save sunghwan2789/54a1aeecbb8abbfe282a040cd35e8dff to your computer and use it in GitHub Desktop.
Save sunghwan2789/54a1aeecbb8abbfe282a040cd35e8dff to your computer and use it in GitHub Desktop.
using System.Linq.Expressions;
using Shouldly;
R a = new(
new int[] { 0 },
new uint[] { 1 },
new short[] { 2 },
new ushort[] { 3 },
new sbyte[] { 4 },
new byte[] { 5 }
);
LambdaExpression[] getterExpressions = {
(R r) => r.i32,
(R r) => r.u32,
(R r) => r.i16,
(R r) => r.u16,
(R r) => r.i8,
(R r) => r.u8,
};
getterExpressions
.Select(x => x.ReturnType.GetElementType())
.Select(Type.GetTypeCode)
.ShouldBe(new[] {
TypeCode.Int32,
TypeCode.UInt32,
TypeCode.Int16,
TypeCode.UInt16,
TypeCode.SByte,
TypeCode.Byte,
});
var getters = getterExpressions.Select(x => x.Compile());
foreach (var (getter, idx) in getters.Select((x, i) => (x, i))) {
switch (getter) {
case Func<R, int[]>: idx.ShouldBeOneOf(0, 1); break;
case Func<R, uint[]>: throw new Exception();
case Func<R, short[]>: idx.ShouldBeOneOf(2, 3); break;
case Func<R, ushort[]>: throw new Exception();
case Func<R, sbyte[]>: idx.ShouldBeOneOf(4, 5); break;
case Func<R, byte[]>: throw new Exception();
}
}
record R(int[] i32, uint[] u32, short[] i16, ushort[] u16, sbyte[] i8, byte[] u8);
using System.Linq.Expressions;
using Shouldly;
R a = new(
new int[] { 0 },
new uint[] { 1 },
new short[] { 2 },
new ushort[] { 3 },
new sbyte[] { 4 },
new byte[] { 5 }
);
LambdaExpression[] getterExpressions = {
(R r) => r.i32,
(R r) => r.u32,
(R r) => r.i16,
(R r) => r.u16,
(R r) => r.i8,
(R r) => r.u8,
};
getterExpressions
.Select(x => x.ReturnType.GenericTypeArguments[0])
.Select(Type.GetTypeCode)
.ShouldBe(new[] {
TypeCode.Int32,
TypeCode.UInt32,
TypeCode.Int16,
TypeCode.UInt16,
TypeCode.SByte,
TypeCode.Byte,
});
var getters = getterExpressions.Select(x => x.Compile());
foreach (var (getter, idx) in getters.Select((x, i) => (x, i))) {
switch (getter) {
case Func<R, IEnumerable<int>>: idx.ShouldBe(0); break;
case Func<R, IEnumerable<uint>>: idx.ShouldBe(1); break;
case Func<R, IEnumerable<short>>: idx.ShouldBe(2); break;
case Func<R, IEnumerable<ushort>>: idx.ShouldBe(3); break;
case Func<R, IEnumerable<sbyte>>: idx.ShouldBe(4); break;
case Func<R, IEnumerable<byte>>: idx.ShouldBe(5); break;
}
}
record R(IEnumerable<int> i32, IEnumerable<uint> u32, IEnumerable<short> i16, IEnumerable<ushort> u16, IEnumerable<sbyte> i8, IEnumerable<byte> u8);
using System.Linq.Expressions;
using Shouldly;
R a = new(
(int)0,
(uint)1,
(short)2,
(ushort)3,
(sbyte)4,
(byte)5
);
LambdaExpression[] getterExpressions = {
(R r) => r.i32,
(R r) => r.u32,
(R r) => r.i16,
(R r) => r.u16,
(R r) => r.i8,
(R r) => r.u8,
};
getterExpressions
.Select(x => x.ReturnType)
.Select(Type.GetTypeCode)
.ShouldBe(new[] {
TypeCode.Int32,
TypeCode.UInt32,
TypeCode.Int16,
TypeCode.UInt16,
TypeCode.SByte,
TypeCode.Byte,
});
var getters = getterExpressions.Select(x => x.Compile());
foreach (var (getter, idx) in getters.Select((x, i) => (x, i))) {
switch (getter) {
case Func<R, int>: idx.ShouldBe(0); break;
case Func<R, uint>: idx.ShouldBe(1); break;
case Func<R, short>: idx.ShouldBe(2); break;
case Func<R, ushort>: idx.ShouldBe(3); break;
case Func<R, sbyte>: idx.ShouldBe(4); break;
case Func<R, byte>: idx.ShouldBe(5); break;
}
}
record R(int i32, uint u32, short i16, ushort u16, sbyte i8, byte u8);
using Shouldly;
int[] a = { 1 };
uint[] b = { 2 };
object[] getters = {
() => a,
() => b,
};
getters.All(x => x is Func<int[]>).ShouldBe(true);
getters.All(x => x is Func<uint[]>).ShouldBe(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment