static async Task Main(string[] args) { // NOTE: Comment / Uncomment demoes as desired await AsyncIterators.Demo(); DefaultInterfaceMembers.Demo(); IndicesAndRanges.Demo1(); IndicesAndRanges.Demo2(); IndicesAndRanges.Demo3(); IndicesAndRanges.Demo4(); IndicesAndRanges.Demo5(); NullableTypes.Demo(); Patterns.Demo1(); Patterns.Demo2(); ReadOnlyMembers.Demo1(); ReadOnlyMembers.Demo2(); StaticLocalFunctions.Demo1(); StaticLocalFunctions.Demo2(); SwitchExpressions.Demo1(); SwitchExpressions.Demo2(); UsingStatement.Demo1(); UsingStatement.Demo2(); Console.ReadKey(); await Task.CompletedTask; }
public static void PartThreeStart() { int amount = 50; string splitter = "="; Program.center(amount, "Start of The Third Part", "*"); ////////////////////////////////////////////// Program.center(amount, "Extinsions", splitter); Extinsions extinsions = new Extinsions(); extinsions.StartExample(); ////////////////////////////////////////////// Program.center(amount, "Partial methods and classes", splitter); StartPartial.StartExample(); ////////////////////////////////////////////// Program.center(amount, "Anon types", splitter); AnonoimoysTypes.StartExample(); ////////////////////////////////////////////// Program.center(amount, "Local functions", splitter); LocalFunctions.StartExample(); ////////////////////////////////////////////// Program.center(amount, "Deconstruction for beginers", splitter); Deconstruction.StartExample(); ////////////////////////////////////////////// Program.center(amount, "Pattern matching", splitter); PatternMatching.StartExample();//// ////////////////////////////////////////////// Program.center(amount, "Nullable Types", splitter); NullableTypes.StartExample(); ////////////////////////////////////////////// Program.center(amount, "Reference vars", splitter); ReferenceVars.StartExample(); ////////////////////////////////////////////// }
private string GenEntityClass(TableModel table) { var columns = table.Columns.Select(x => new { x.TableName, x.ColumnName, x.DataType, x.IsNullable, x.Description, x.CharacterMaximumLength, x.NumericPrecision, x.NumericScale, ColumnNameMap = x.ColumnName.ToPascalCase(), TypeName = SqlTypeMap.ContainsKey(x.DataType) ? SqlTypeMap[x.DataType] : x.DataType, IsNull = x.IsNullable == "YES" && NullableTypes.Contains(SqlTypeMap.ContainsKey(x.DataType) ? SqlTypeMap[x.DataType] : x.DataType), Key = table.TableConstraints.FirstOrDefault(y => y.ConstraintType == "PRIMARY KEY"). KeyColumnUsages.FirstOrDefault(y => y.ColumnName == x.ColumnName) }); var classTemplate = File.ReadAllText(@"Template/ClassTemplate.Scriban"); return(_templateService.Render(classTemplate, new { columns, table.TableName, TableNameMap = table.TableName.ToPascalCase() })); }
public void op_JsonSerialize_object_whenNullableTypesDefault(string expected) { var example = new NullableTypes(); var actual = example.JsonSerialize(); Assert.Equal(expected, actual); }
public static void Run() { char key; while (true) { printMenu(); key = Console.ReadKey().KeyChar; Console.WriteLine(); switch (key) { case 'n': namespaceDemo(); break; case 'e': extentionMethodDemo(); break; case 'p': partialClass(); break; case 'a': AnonymousTypes.Display(); break; case 'l': LocalFunction.Display(); break; case 'm': PatternMatching.Display(); break; case 'd': DeconstructorDemo.Display(); break; case 'u': NullableTypes.Display(); break; case 'r': ReferenceVars.Display(); break; case 'x': return; } Console.ReadKey(); } }
static void Main(string[] args) { /*ObjectTypes objectTypes = new ObjectTypes(); * BoxingUnboxing boxingUnboxing = new BoxingUnboxing(); * * objectTypes.exam(); * boxingUnboxing.Exam();*/ NullableTypes nullableType = new NullableTypes(); nullableType.exam(); }
/// <summary> /// Tests if the provided type is nullable /// </summary> /// <param name="arg"></param> /// <returns></returns> public static bool IsNullableType(this Type arg) { if (NullableTypes.TryGetValue(arg, out var cachedResult)) { return(cachedResult); } var method = GenericGetDefaultValueMethod.MakeGenericMethod(arg); var defaultValueForType = method.Invoke(null, new object[] { }); var result = defaultValueForType == null; return(NullableTypes[arg] = result); }
public void Test_06_Nullable_2() { NullableTypes Obj = new NullableTypes() { Int8 = -0x01, Int16 = null, Int32 = -0x01020304, Int64 = null, UInt8 = 0x01, UInt16 = null, UInt32 = 0x01020304, UInt64 = null, S = 1.2345F, D = null, Dec = 1.2345M, B = null, Ch = 'x', DT = null, DTO = DateTimeOffset.UtcNow, TS = null, Id = Guid.NewGuid(), E1 = null, E2 = FlagsEnum.A | FlagsEnum.C }; byte[] Bin = this.endpoint.Serialize(Obj); NullableTypes Obj2 = this.endpoint.Deserialize(Bin) as NullableTypes; Assert.IsNotNull(Obj2); Assert.AreEqual(Obj.Int8, Obj2.Int8); Assert.AreEqual(Obj.Int16, Obj2.Int16); Assert.AreEqual(Obj.Int32, Obj2.Int32); Assert.AreEqual(Obj.Int64, Obj2.Int64); Assert.AreEqual(Obj.UInt8, Obj2.UInt8); Assert.AreEqual(Obj.UInt16, Obj2.UInt16); Assert.AreEqual(Obj.UInt32, Obj2.UInt32); Assert.AreEqual(Obj.UInt64, Obj2.UInt64); Assert.AreEqual(Obj.S, Obj2.S); Assert.AreEqual(Obj.D, Obj2.D); Assert.AreEqual(Obj.Dec, Obj2.Dec); Assert.AreEqual(Obj.B, Obj2.B); Assert.AreEqual(Obj.Ch, Obj2.Ch); Assert.AreEqual(Obj.DT, Obj2.DT); Assert.AreEqual(Obj.DTO, Obj2.DTO); Assert.AreEqual(Obj.TS, Obj2.TS); Assert.AreEqual(Obj.Id, Obj2.Id); Assert.AreEqual(Obj.E1, Obj2.E1); Assert.AreEqual(Obj.E2, Obj2.E2); }
public void Test_05_Nullable_1() { NullableTypes Obj = new NullableTypes() { Int8 = null, Int16 = -0x0102, Int32 = null, Int64 = -0x0102030405060708, UInt8 = null, UInt16 = 0x0102, UInt32 = null, UInt64 = 0x0102030405060708, S = null, D = 1.2345, Dec = null, B = true, Ch = null, DT = DateTime.UtcNow, DTO = null, TS = DateTime.Now.TimeOfDay, Id = null, E1 = NormalEnum.B, E2 = null }; byte[] Bin = this.endpoint.Serialize(Obj); NullableTypes Obj2 = this.endpoint.Deserialize(Bin) as NullableTypes; Assert.IsNotNull(Obj2); Assert.AreEqual(Obj.Int8, Obj2.Int8); Assert.AreEqual(Obj.Int16, Obj2.Int16); Assert.AreEqual(Obj.Int32, Obj2.Int32); Assert.AreEqual(Obj.Int64, Obj2.Int64); Assert.AreEqual(Obj.UInt8, Obj2.UInt8); Assert.AreEqual(Obj.UInt16, Obj2.UInt16); Assert.AreEqual(Obj.UInt32, Obj2.UInt32); Assert.AreEqual(Obj.UInt64, Obj2.UInt64); Assert.AreEqual(Obj.S, Obj2.S); Assert.AreEqual(Obj.D, Obj2.D); Assert.AreEqual(Obj.Dec, Obj2.Dec); Assert.AreEqual(Obj.B, Obj2.B); Assert.AreEqual(Obj.Ch, Obj2.Ch); Assert.AreEqual(Obj.DT, Obj2.DT); Assert.AreEqual(Obj.DTO, Obj2.DTO); Assert.AreEqual(Obj.TS, Obj2.TS); Assert.AreEqual(Obj.Id, Obj2.Id); Assert.AreEqual(Obj.E1, Obj2.E1); Assert.AreEqual(Obj.E2, Obj2.E2); }
public void NullableDeserializeTest() { const string insertCql = @"insert into Test.Types(aInt) values (4);"; const string selectCql = "select * from Test.Types limit 1;"; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); NullableTypes result = null; using (var reader = selectCmd.ExecuteReader <NullableTypes>()) { if (reader.Read()) { result = reader.Current; } } Assert.IsNotNull(result); Assert.IsNull(result.aASCIIString); Assert.IsNull(result.aVarcharString); Assert.IsNull(result.aVarint); Assert.IsNull(result.aTextString); Assert.IsNull(result.aBool); Assert.IsNull(result.aDouble); Assert.IsNull(result.aFloat); Assert.IsNull(result.aInet); Assert.IsNull(result.aLong); Assert.IsNull(result.aTimeUUID); Assert.IsNull(result.aUUID); Assert.IsNull(result.aBlob); Assert.IsNull(result.aList); Assert.IsNull(result.aSet); Assert.IsNull(result.aMap); } }
static bool IsNullable(this Type type) { return(NullableTypes.GetOrAdd(type, key => key.IsGenericType && key.GetGenericTypeDefinition() == NullableType)); }
public void SerializeObjectInOutNullTest() { const string insertCql = @"insert into SerializationTest.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDecimal , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var values = new NullableTypes { aInt = 3, aASCIIString = null, aBlob = null, aBool = null, aDecimal = null, aDouble = null, aFloat = null, aInet = null, aLong = null, aTextString = null, aVarcharString = null, aTimeUUID = null, aUUID = null, aTimestamp = null, aVarint = null, aList = null, aSet = null, aMap = null }; var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters.Set(values); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); NullableTypes result = null; using(var reader = selectCmd.ExecuteReader<NullableTypes>()) { if(reader.Read()) result = reader.Current; } Assert.IsNotNull(result); Assert.IsNull(result.aASCIIString); Assert.IsNull(result.aVarcharString); Assert.IsNull(result.aVarint); Assert.IsNull(result.aTextString); Assert.IsNull(result.aBool); Assert.IsNull(result.aDecimal); Assert.IsNull(result.aDouble); Assert.IsNull(result.aFloat); Assert.IsNull(result.aInet); Assert.IsNull(result.aLong); Assert.IsNull(result.aTimeUUID); Assert.IsNull(result.aUUID); Assert.IsNull(result.aBlob); Assert.IsNull(result.aList); Assert.IsNull(result.aSet); Assert.IsNull(result.aMap); } }
public void NullableSerializeTest() { const string insertCql = @"insert into Test.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from Test.Types limit 1;"; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var values = new NullableTypes { aInt = 3, aASCIIString = null, aBlob = null, aBool = null, aDouble = null, aFloat = null, aInet = null, aLong = null, aTextString = null, aVarcharString = null, aTimeUUID = null, aUUID = null, aTimestamp = null, aVarint = null, aList = null, aSet = null, aMap = null }; var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters.Set(values); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); NullableTypes result = null; using (var reader = selectCmd.ExecuteReader <NullableTypes>()) { if (reader.Read()) { result = reader.Current; } } Assert.IsNotNull(result); Assert.IsNull(result.aASCIIString); Assert.IsNull(result.aVarcharString); Assert.IsNull(result.aVarint); Assert.IsNull(result.aTextString); Assert.IsNull(result.aBool); Assert.IsNull(result.aDouble); Assert.IsNull(result.aFloat); Assert.IsNull(result.aInet); Assert.IsNull(result.aLong); Assert.IsNull(result.aTimeUUID); Assert.IsNull(result.aUUID); Assert.IsNull(result.aBlob); Assert.IsNull(result.aList); Assert.IsNull(result.aSet); Assert.IsNull(result.aMap); } }
public static void AdvancedConcepts() { ScalarTypes scalarType = Helper.FillObject <ScalarTypes>(); var scalarTypeEncode = Helper.Encode(scalarType); var scalarTypeDecode = Helper.Decode(scalarTypeEncode, ScalarTypes.Parser); Assert.IsTrue(Helper.CompareObjects(scalarType, scalarTypeDecode)); ComplexTypes complexType = Helper.FillObject <ComplexTypes>(); complexType.Maps.Add(1, "Suresh"); complexType.Numbers.Add(10); complexType.Details = Any.Pack(Helper.FillObject <Person>()); var complexTypeEncode = Helper.Encode(complexType); var complexTypeDecode = Helper.Decode(complexTypeEncode, ComplexTypes.Parser); Assert.IsTrue(Helper.CompareObjects(complexType, complexTypeDecode)); if (complexTypeDecode.Details.Is(Person.Descriptor)) { complexTypeDecode.Details.TryUnpack(out Person person); } var oneOfCase = complexTypeDecode.OneofCase; //returns the name of the field that is set NullableTypes nullableType = Helper.FillObject <NullableTypes>(); var nullableTypeEncode = Helper.Encode(nullableType); var nullableTypeDecode = Helper.Decode(nullableTypeEncode, NullableTypes.Parser); Assert.IsTrue(Helper.CompareObjects(nullableType, nullableTypeDecode)); TimeTypes timeType = Helper.FillObject <TimeTypes>(); var timeTypeEncode = Helper.Encode(timeType); var timeTypeDecode = Helper.Decode(timeTypeEncode, TimeTypes.Parser); Assert.IsTrue(Helper.CompareObjects(timeType, timeTypeDecode)); //Write a collections List <ScalarTypes> sts = new List <ScalarTypes>(); sts.Add(Helper.FillObject <ScalarTypes>()); sts.Add(Helper.FillObject <ScalarTypes>()); MemoryStream ipStream = new MemoryStream(); foreach (var st in sts) { st.WriteDelimitedTo(ipStream); } using (var file = File.Create("data.bin")) file.Write(ipStream.ToArray()); ipStream.Close(); List <ScalarTypes> stsDecoded = new List <ScalarTypes>(); using (var stream = File.OpenRead("data.bin")) { while (stream.Position < stream.Length) { var sto = ScalarTypes.Parser.ParseDelimitedFrom(stream); stsDecoded.Add(sto); } } Assert.IsTrue(Helper.CompareObjects(sts, stsDecoded)); }
public void SerializeNullableObjectInOutDefaultsTest() { var values = new NullableTypes { aInt = 10 }; using (var context = new SerializationContext()) { context.NullableTypes.Add(values); context.SaveChanges(); } using (var context = new SerializationContext()) { var result = context.NullableTypes.FirstOrDefault(); Assert.IsNotNull(result); Assert.IsNull(result.aASCIIString); Assert.IsNull(result.aVarcharString); Assert.IsNull(result.aVarint); Assert.IsNull(result.aTextString); Assert.IsNull(result.aBool); Assert.IsNull(result.aDecimal); Assert.IsNull(result.aDouble); Assert.IsNull(result.aFloat); Assert.IsNull(result.aInet); Assert.IsNull(result.aLong); Assert.IsNull(result.aTimeUUID); Assert.IsNull(result.aUUID); Assert.IsNull(result.aBlob); Assert.IsNull(result.aList); Assert.IsNull(result.aSet); Assert.IsNull(result.aMap); } }
public void SerializeNullableObjectInOutTest() { var values = new NullableTypes { aASCIIString = "hello world!", aBlob = new byte[] { 1, 2, 3, 4 }, aBool = true, aDecimal = decimal.MaxValue / 2, aDouble = 1.234, aFloat = 5.789f, aInet = new IPAddress(new byte[] { 127, 0, 0, 1 }), aInt = 10, aLong = 56789012456, aTextString = "some other text with \u005C unicode", aVarcharString = "some other varchar with \u005C unicode", aTimeUUID = DateTime.Now.GenerateTimeBasedGuid(), aUUID = Guid.NewGuid(), aTimestamp = DateTime.Now, aVarint = new BigInteger(12345678901234), aList = new List<string> { "string 1", "string 2" }, aSet = new HashSet<int> { 1, 3, 3 }, aMap = new Dictionary<long, string> { { 1, "value 1" }, { 2, "value 2" }, { 3, "value 3" } }, }; using (var context = new SerializationContext()) { context.NullableTypes.Add(values); context.SaveChanges(); } using (var context = new SerializationContext()) { var result = context.NullableTypes.FirstOrDefault(); Assert.IsNotNull(result); Assert.AreEqual(values.aASCIIString, result.aASCIIString); Assert.AreEqual(values.aVarcharString, result.aVarcharString); Assert.AreEqual(values.aVarint, result.aVarint); Assert.AreEqual(values.aTextString, result.aTextString); Assert.AreEqual(values.aBool, result.aBool); Assert.AreEqual(values.aDecimal, result.aDecimal); Assert.AreEqual(values.aDouble, result.aDouble); Assert.AreEqual(values.aFloat, result.aFloat); Assert.AreEqual(values.aInet, result.aInet); Assert.AreEqual(values.aInt, result.aInt); Assert.AreEqual(values.aLong, result.aLong); Assert.AreEqual(values.aTimeUUID, result.aTimeUUID); Assert.AreEqual(values.aUUID, result.aUUID); Assert.IsTrue(result.aBlob.SequenceEqual(values.aBlob)); Assert.IsTrue(result.aList.SequenceEqual(values.aList)); Assert.IsTrue(result.aSet.SequenceEqual(values.aSet)); } }