// re-enable once https://github.com/dotnet/csharplang/issues/99 is implemented // see https://github.com/dotnet/runtime/issues/49069 //public StructWithPublicDefaultConstructor StructWithConstructor { get; } #pragma warning disable SA1129 public ClassWithOptionalArgsCtorWithStructs( DateTime dateTime = new DateTime(), DateTime dateTimeDefault = default(DateTime), TimeSpan timeSpan = new TimeSpan(), TimeSpan timeSpanDefault = default(TimeSpan), DateTimeOffset dateTimeOffset = new DateTimeOffset(), DateTimeOffset dateTimeOffsetDefault = default(DateTimeOffset), Guid guid = new Guid(), Guid guidDefault = default(Guid), CustomStruct customStruct = new CustomStruct(), CustomStruct customStructDefault = default(CustomStruct), ConsoleColor?color = ConsoleColor.DarkGreen, ConsoleColor?colorNull = null, int?integer = 12, int?integerNull = null //StructWithPublicDefaultConstructor structWithConstructor = default ) #pragma warning restore SA1129 { Color = color; ColorNull = colorNull; Integer = integer; IntegerNull = integerNull; //StructWithConstructor = structWithConstructor; }
public static void Boxing() { var a = new CustomStruct(); // This will box CustomStruct, so this method should fail when compiling a.GetType(); }
public void SerializeCustomStruct() { var target = new CustomStruct { A = "a string", B = 5 }; JsonValue expected = new JsonObject { ["A"] = "a string", ["B"] = 5 }; var serializer = new JsonSerializer { Options = { AutoSerializeFields = true } }; var json = serializer.Serialize(target); Assert.AreEqual(expected, json); }
public static float test_struct_func_call_by_value() { var localVar = new CustomStruct(); localVar.firstfield = 94; localVar.value = 123; return(byvalue_function_helper(localVar)); }
public void Should_WrapStructs() { var @struct = new CustomStruct(); var wrapped = @struct.AsDynamic(); Assert.IsType <PrivateReflectionDynamicObjectInstance>(wrapped); }
public void CloneInsideStructIsNotTheSameObject() { var c = new CustomClass <int>(1, 2, 3, 4); var foo = new CustomStruct <CustomClass <int> >(c, c, c, c); var bar = Cloning.Clone(foo); Assert.False(ReferenceEquals(foo.Field1, bar.Field1)); }
private void Foo() { var x = new CustomStruct(); if (x != null && x.Value > 0) { } }
public void Register_CustomCanvasType_Height_CustomInterface() { CustomCanvasType the_object = new CustomCanvasType(); CustomCanvasType custom_canvas = new CustomCanvasType(); Canvas canvas = new Canvas(); CustomStruct custom_struct_1 = new CustomStruct(1); CustomEnum custom_enum = CustomEnum.EnumValue1; CustomDelegate custom_delegate = delegate { }; CustomInterface custom_interface_a = new CustomInterfaceImplA(); CustomInterface custom_interface_b = new CustomInterfaceImplB(); DependencyProperty property; DependencyPropertyInfo info; DependencyPropertyInfo.ChangedInfo changed_info; InkPresenter ink = new InkPresenter(); // The only builtin type derived from Canvas object actual_value; object previous_expected_value = null; int iterations = 0; int changes = 0; CustomCanvasType_Height_CustomInterface = new DependencyPropertyInfo("Height", typeof(CustomCanvasType), typeof(CustomInterface), true); info = CustomCanvasType_Height_CustomInterface; property = info.Property; Assert.AreEqual(null, the_object.GetValue(property), "Default value 1"); Assert.AreEqual(null, ink.GetValue(property), "Default value 2"); Assert.Throws(delegate { the_object.SetValue(property, 0); }, typeof(ArgumentException)); Assert.Throws(delegate { the_object.SetValue(property, 1); }, typeof(ArgumentException)); Assert.Throws(delegate { the_object.SetValue(property, ""); }, typeof(ArgumentException)); Assert.Throws(delegate { the_object.SetValue(property, new CustomClass()); }, typeof(ArgumentException)); Assert.Throws(delegate { the_object.SetValue(property, new Canvas()); }, typeof(ArgumentException)); Assert.Throws(delegate { the_object.SetValue(property, custom_enum); }, typeof(ArgumentException)); foreach (object expected_value in new object [] { null, custom_interface_a, null, custom_interface_b, custom_interface_b, null }) { iterations++; the_object.SetValue(property, expected_value); actual_value = the_object.GetValue(property); if (!object.Equals(expected_value, previous_expected_value)) { changes++; changed_info = info.Changes [info.Changes.Count - 1]; DependencyPropertyChangedEventArgs args = changed_info.args; Assert.AreEqual(args.OldValue, previous_expected_value, "OldValue"); Assert.AreEqual(args.NewValue, expected_value, "NewValue"); Assert.AreSame(changed_info.obj, the_object); } previous_expected_value = expected_value; Assert.AreEqual(expected_value, actual_value, "Iteration #{0}", iterations); Assert.AreEqual(changes, info.Changes.Count, "Iteration #{0} there should be {1} changes, but there were {2} changes", iterations, changes, info.Changes.Count); } }
static void InitializeStruct() { _customStruct = new CustomStruct() { FieldOne = 1, FieldTwo = 2, FieldThree = 3, }; }
public void CustomStructIsClonable() { var foo = new CustomStruct <int>(10, 20, 30, 40); var bar = Cloning.Clone(foo); Assert.Equal(foo.Field1, bar.Field1); Assert.Equal(foo.Field2, bar.Field2); Assert.Equal(foo.Field3, bar.Field3); Assert.Equal(foo.Field4, bar.Field4); }
public void ObjectRelationshipsInStructArePreserved() { var o = new object(); var foo = new CustomStruct <object>(o, o, o, o); var bar = Cloning.Clone(foo); Assert.True(ReferenceEquals(bar.Field1, bar.Field2)); Assert.True(ReferenceEquals(bar.Field2, bar.Field3)); Assert.True(ReferenceEquals(bar.Field3, bar.Field4)); }
public void Custom_struct_value() { var value = new CustomStruct { Prop1 = "one", Prop2 = 2 }; const string expected = "{\"Prop1\":\"one\",\"Prop2\":2}"; Serialize(value).Should().Be(expected); }
public static float test_struct_func_call_by_ref() { var localVar = new CustomStruct { firstfield = 94, value = 123 }; byref_function_helper(ref localVar); return(localVar.value); }
public void Should_read_and_write_custom_structs() { var buffer = new byte[1024]; using (var wrapper = new UnsafeBufferWrapper(buffer)) { var value = new CustomStruct {Field1 = 42, Field2 = TimeSpan.FromHours(42), Field3 = DateTime.UtcNow}; wrapper.Write(ref value); Assert.AreEqual(wrapper.Position, sizeof(CustomStruct)); wrapper.ResetCursor(); var readValue = wrapper.Read<CustomStruct>(); Assert.AreEqual(value, readValue); } }
public ClassWithServiceAndOptionalArgsCtorWithStructs(IFakeService fake, DateTime dateTime = new DateTime(), DateTime dateTimeDefault = default(DateTime), TimeSpan timeSpan = new TimeSpan(), TimeSpan timeSpanDefault = default(TimeSpan), DateTimeOffset dateTimeOffset = new DateTimeOffset(), DateTimeOffset dateTimeOffsetDefault = default(DateTimeOffset), Guid guid = new Guid(), Guid guidDefault = default(Guid), CustomStruct customStruct = new CustomStruct(), CustomStruct customStructDefault = default(CustomStruct) ) { }
private static void Main(string[] args) { var obj = new CustomStruct() { AutoInt = 10, AutoString = "autostring", ManualInt = 5, ManualString = "manualstring", PublicInt = 20, PublicString = "publicstring" }; var json = JsonConvert.SerializeObject(obj, Formatting.Indented); var dObj = JsonConvert.DeserializeObject <CustomStruct>(json); }
// The specialization functionality supports user-defined types, as long as they // are value types and implement the IEquatable interface (and have useful // GetHashCode and Equals implementations). public static void SpecializedCustomStructKernel( ArrayView <int> view, SpecializedValue <CustomStruct> specialized) { var globalIndex = Grid.GlobalIndex.X; // Note that an implicit conversion from a specialized value to // a non-specialized value is possible. But: not the other way around ;) CustomStruct customValue = specialized; // The value is specialized and the additional optimization passes will // perform constant propagation to create an 'optimized' store with a single constant // value (in this case) view[globalIndex] = customValue.Value1 + customValue.Value2; }
public void CustomStructArrReadWriteTest([Random(1, ArrCount_Max, ArrTest_RepeatCount)] int test_data_len) { string smem_name = $"{nameof(CustomStructArrReadWriteTest)}_{random_int}"; var test_data = new CustomStruct[test_data_len]; for (int i = 0; i < test_data.Length; i++) { test_data[i] = new() { A = (float)random_double, B = random_int, C = random_double, D = (random_int % 2) == 0 } } ; ArrDataRWTest(smem_name, 0, test_data); }
public static void Main (string[] args) { var a = new CustomType(1); var b = new CustomType(2); CustomType c = null; Console.WriteLine(a ?? b); Console.WriteLine(b ?? a); Console.WriteLine(a ?? c); Console.WriteLine(b ?? c); CustomStruct? a1 = new CustomStruct(1); CustomStruct? b1 = new CustomStruct(2); CustomStruct? c1 = null; Console.WriteLine(a1 ?? b1); Console.WriteLine(b1 ?? a1); Console.WriteLine(a1 ?? c1); Console.WriteLine(b1 ?? c1); }
public StructDefaultValueService(DateTime dateTime = new DateTime(), DateTime dateTimeDefault = default(DateTime), TimeSpan timeSpan = new TimeSpan(), TimeSpan timeSpanDefault = default(TimeSpan), DateTimeOffset dateTimeOffset = new DateTimeOffset(), DateTimeOffset dateTimeOffsetDefault = default(DateTimeOffset), Guid guid = new Guid(), Guid guidDefault = default(Guid), CustomStruct customStruct = new CustomStruct(), CustomStruct customStructDefault = default(CustomStruct), ConsoleColor?color = ConsoleColor.DarkGreen, ConsoleColor?colorNull = null, int?integer = 12, int?integerNull = null) { Color = color; ColorNull = colorNull; Integer = integer; IntegerNull = integerNull; }
public static void Main(string[] args) { var a = new CustomType(1); var b = new CustomType(2); CustomType c = null; Console.WriteLine(a ?? b); Console.WriteLine(b ?? a); Console.WriteLine(a ?? c); Console.WriteLine(b ?? c); CustomStruct?a1 = new CustomStruct(1); CustomStruct?b1 = new CustomStruct(2); CustomStruct?c1 = null; Console.WriteLine(a1 ?? b1); Console.WriteLine(b1 ?? a1); Console.WriteLine(a1 ?? c1); Console.WriteLine(b1 ?? c1); }
public void it_will_read_struct() { var expected = new CustomStruct() { Byte = 0x10, Sbyte = -0x10, UShort = 0x2ABC, Short = -0x2ABC, UInt32 = 0x400ABC00, Int32 = -0x400ABC00, UInt64 = 0x7000000ABC000000, Int64 = -0x7000000ABC000000, Bool = true, Float = 12345.678f, Double = 123456789.123456789d, Decimal = 123456789.123456789m }; //var size = Marshal.SizeOf(expected); //var ptr = Marshal.AllocHGlobal(size); //var expectedBytes = new byte[size]; //Marshal.StructureToPtr(expected, ptr, false); //Marshal.Copy(ptr, expectedBytes, 0, size); //var test = Marshal.PtrToStructure(ptr, expected.GetType()); //expectedBytes.ToList().ForEach(b => Debug.Write("0x" + b.ToString("x2") + ", ")); var source = new byte[] { 0x10, 0xf0, 0x44, 0xd5, 0xbc, 0x2a, 0x00, 0x44, 0xf5, 0xbf, 0x00, 0xbc, 0x0a, 0x40, 0x00, 0x00, 0x00, 0x44, 0xf5, 0xff, 0xff, 0x8f, 0x00, 0x00, 0x00, 0xbc, 0x0a, 0x00, 0x00, 0x70, 0x01, 0x00, 0x00, 0x00, 0xb6, 0xe6, 0x40, 0x46, 0x75, 0x6b, 0x7e, 0x54, 0x34, 0x6f, 0x9d, 0x41, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x5f, 0xd0, 0xac, 0x4b, 0x9b, 0xb6, 0x01 }; using (var reader = new BinaryReader(new MemoryStream(source))) { var value = reader.ReadStruct <CustomStruct>(); value.Should().Be(expected); reader.BaseStream.ShouldBeEof(); } //Marshal.FreeHGlobal(ptr); }
static void Main(string[] args) { var dataTable = new DataTable(); dataTable.Columns.Add("ABC", typeof(string)); var a = new CustomStruct() { IntValue = 1, DataTable = dataTable }; Console.WriteLine("IsValueType: {0}", a.GetType().IsValueType); Console.WriteLine(); var b = a; a.IntValue = 2; a.DataTable.Columns.Add("DEF", typeof(int)); Console.WriteLine("a.IntValue: {0}", a.IntValue); Console.WriteLine("a.DataTable.Columns.Count: {0}", a.DataTable.Columns.Count); Console.WriteLine(); Console.WriteLine("b.IntValue: {0}", b.IntValue); Console.WriteLine("b.DataTable.Columns.Count: {0}", b.DataTable.Columns.Count); }
public void ShouldCreateSimpleFunc_NestedExpression_ValueParameter_CustomStructResult() { // Arrange IList <CustomStruct> list = new List <CustomStruct>(); list.Add(new CustomStruct(50)); list.Add(new CustomStruct(100)); Expression <Func <IList <CustomStruct>, CustomStruct> > expression = a => a.FirstOrDefault(x => x.Value == 50); Console.WriteLine(expression.ToString()); // Act Func <IList <CustomStruct>, CustomStruct> emit = expression.Compile(); Func <IList <CustomStruct>, CustomStruct> reflection = expression.Reflect(); CustomStruct emitResult = emit.Invoke(list); CustomStruct reflectionResult = reflection.Invoke(list); // Assert emitResult.Value.Should().Be(50); reflectionResult.Value.Should().Be(50); }
public ClassWithCustomStruct(DateTime timestamp, double value) { Value = new CustomStruct(timestamp, value); }
public bool Equals(CustomStruct other) { return(string.Equals(A, other.A) && B == other.B); }
public void it_will_read_struct() { var expected = new CustomStruct() { Byte = 0x10, Sbyte = -0x10, UShort = 0x2ABC, Short = -0x2ABC, UInt32 = 0x400ABC00, Int32 = -0x400ABC00, UInt64 = 0x7000000ABC000000, Int64 = -0x7000000ABC000000, Bool = true, Float = 12345.678f, Double = 123456789.123456789d, Decimal = 123456789.123456789m }; //var size = Marshal.SizeOf(expected); //var ptr = Marshal.AllocHGlobal(size); //var expectedBytes = new byte[size]; //Marshal.StructureToPtr(expected, ptr, false); //Marshal.Copy(ptr, expectedBytes, 0, size); //var test = Marshal.PtrToStructure(ptr, expected.GetType()); //expectedBytes.ToList().ForEach(b => Debug.Write("0x" + b.ToString("x2") + ", ")); var source = new byte[] { 0x10, 0xf0, 0x44, 0xd5, 0xbc, 0x2a, 0x00, 0x44, 0xf5, 0xbf, 0x00, 0xbc, 0x0a, 0x40, 0x00, 0x00, 0x00, 0x44, 0xf5, 0xff, 0xff, 0x8f, 0x00, 0x00, 0x00, 0xbc, 0x0a, 0x00, 0x00, 0x70, 0x01, 0x00, 0x00, 0x00, 0xb6, 0xe6, 0x40, 0x46, 0x75, 0x6b, 0x7e, 0x54, 0x34, 0x6f, 0x9d, 0x41, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x5f, 0xd0, 0xac, 0x4b, 0x9b, 0xb6, 0x01 }; using (var reader = new BinaryReader(new MemoryStream(source))) { var value = reader.ReadStruct<CustomStruct>(); value.Should().Be(expected); reader.BaseStream.ShouldBeEof(); } //Marshal.FreeHGlobal(ptr); }
public void Register_CustomCanvasType_Height_CustomDelegate () { CustomCanvasType the_object = new CustomCanvasType (); CustomCanvasType custom_canvas = new CustomCanvasType (); Canvas canvas = new Canvas (); CustomStruct custom_struct_1 = new CustomStruct (1); CustomEnum custom_enum = CustomEnum.EnumValue1; CustomDelegate custom_delegate = delegate { }; DependencyProperty property; DependencyPropertyInfo info; DependencyPropertyInfo.ChangedInfo changed_info; InkPresenter ink = new InkPresenter (); // The only builtin type derived from Canvas object actual_value; object previous_expected_value = null; int iterations = 0; int changes = 0; CustomCanvasType_Height_CustomDelegate = new DependencyPropertyInfo ("Height", typeof (CustomCanvasType), typeof (CustomDelegate), true); info = CustomCanvasType_Height_CustomDelegate; property = info.Property; Assert.AreEqual (null, the_object.GetValue (property), "Default value 1"); Assert.AreEqual (null, ink.GetValue (property), "Default value 2"); Assert.Throws (delegate { the_object.SetValue (property, 0); }, typeof (ArgumentException)); Assert.Throws (delegate { the_object.SetValue (property, 1); }, typeof (ArgumentException)); Assert.Throws (delegate { the_object.SetValue (property, ""); }, typeof (ArgumentException)); Assert.Throws (delegate { the_object.SetValue (property, new CustomClass ()); }, typeof (ArgumentException)); Assert.Throws (delegate { the_object.SetValue (property, new Canvas ()); }, typeof (ArgumentException)); foreach (object expected_value in new CustomDelegate [] { null, delegate { }, delegate { }, custom_delegate, custom_delegate }) { iterations++; the_object.SetValue (property, expected_value); actual_value = the_object.GetValue (property); if (!object.Equals (expected_value, previous_expected_value)) { changes++; changed_info = info.Changes [info.Changes.Count - 1]; DependencyPropertyChangedEventArgs args = changed_info.args; Assert.AreEqual (args.OldValue, previous_expected_value, "OldValue"); Assert.AreEqual (args.NewValue, expected_value, "NewValue"); Assert.AreSame (changed_info.obj, the_object); } previous_expected_value = expected_value; Assert.AreEqual (expected_value, actual_value, "Iteration #{0}", iterations); Assert.AreEqual (changes, info.Changes.Count, "Iteration #{0} there should be {1} changes, but there were {2} changes", iterations, changes, info.Changes.Count); } }
static void StackMoreTypes() { Console.Write("\nStack - more types "); Engine engine = CreateEngine(); bool?v00 = false; bool?n00 = null; engine.Push(v00.Value); engine.Push(v00); engine.Push(n00); Assert(engine.ItemType(-3), VarType.Bool); Assert(engine.ItemType(-2), VarType.Bool); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v00.Value, VarType.Bool); byte?v01 = 5; byte?n01 = null; engine.Push(v01.Value); engine.Push(v01); engine.Push(n01); Assert(engine.ItemType(-3), VarType.Int); Assert(engine.ItemType(-2), VarType.Int); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v01.Value, VarType.Int); sbyte?v02 = 5; sbyte?n02 = null; engine.Push(v02.Value); engine.Push(v02); engine.Push(n02); Assert(engine.ItemType(-3), VarType.Int); Assert(engine.ItemType(-2), VarType.Int); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v02.Value, VarType.Int); short?v03 = 5; short?n03 = null; engine.Push(v03.Value); engine.Push(v03); engine.Push(n03); Assert(engine.ItemType(-3), VarType.Int); Assert(engine.ItemType(-2), VarType.Int); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v03.Value, VarType.Int); ushort?v04 = 5; ushort?n04 = null; engine.Push(v04.Value); engine.Push(v04); engine.Push(n04); Assert(engine.ItemType(-3), VarType.Int); Assert(engine.ItemType(-2), VarType.Int); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v04.Value, VarType.Int); int?v05 = 5; int?n05 = null; engine.Push(v05.Value); engine.Push(v05); engine.Push(n05); Assert(engine.ItemType(-3), VarType.Int); Assert(engine.ItemType(-2), VarType.Int); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v05.Value, VarType.Int); uint?v06 = 5; uint?n06 = null; engine.Push(v06.Value); engine.Push(v06); engine.Push(n06); Assert(engine.ItemType(-3), VarType.Int); Assert(engine.ItemType(-2), VarType.Int); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v06.Value, VarType.Int); long?v07 = 5; long?n07 = null; engine.Push(v07.Value); engine.Push(v07); engine.Push(n07); Assert(engine.ItemType(-3), VarType.Int); Assert(engine.ItemType(-2), VarType.Int); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v07.Value, VarType.Int); ulong?v08 = 5; ulong?n08 = null; engine.Push(v08.Value); engine.Push(v08); engine.Push(n08); Assert(engine.ItemType(-3), VarType.Int); Assert(engine.ItemType(-2), VarType.Int); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); try { NoteTest(); SMT_CheckObj(engine, v08.Value, VarType.Int); ExpectUnreached(); } catch (SGSException e) { Assert(e.resultCode, RC.ENOTSUP); } float?v09 = 5; float?n09 = null; engine.Push(v09.Value); engine.Push(v09); engine.Push(n09); Assert(engine.ItemType(-3), VarType.Real); Assert(engine.ItemType(-2), VarType.Real); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v09.Value, VarType.Real); double?v10 = 5; double?n10 = null; engine.Push(v10.Value); engine.Push(v10); engine.Push(n10); Assert(engine.ItemType(-3), VarType.Real); Assert(engine.ItemType(-2), VarType.Real); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v10.Value, VarType.Real); CustomStruct?v11 = new CustomStruct() { c = 12.3f, d = 123 }; CustomStruct?n11 = null; engine.Push(v11.Value); engine.Push(v11); engine.Push(n11); Assert(engine.ItemType(-3), VarType.Object); Assert(engine.ItemType(-2), VarType.Object); Assert(engine.ItemType(-1), VarType.Null); engine.Pop(3); SMT_CheckObj(engine, v11.Value, VarType.Object); DestroyEngine(engine); }
private static void byref_function_helper(ref CustomStruct customStruct) { customStruct.value = customStruct.value * 2; }
private static float byvalue_function_helper(CustomStruct customStruct) { return(customStruct.value * 2); }
public bool Equals(CustomStruct other) { return(this.a == other.a); }
public bool Equals(CustomStruct other) { return(this.A == other.A && this.c == other.c && string.Equals(this.B, other.B)); }
public CustomClass(int a, string b, CustomStruct c) { this.a = a; this.b = b; this.c = c; }