I'm trying to create a class that has fields in it that are of an anonymous type. (This is for Json deserialization.)
I can't find a syntax that the compiler will accept. I'm trying:
class Foo {
var Bar = new {
int num;
}
var Baz = new {
int[] values;
}
}
This is supposed to represent this example Json object:
{
"Bar": { "num": 0 }
"Baz": { "values": [0, 1, 2] }
}
Is this even possible, or must I declare each class normally with a full class identifier?
You can declare a field using an anonymous type initializer... you can't use implicit typing (var). So this works:
using System;
class Test
{
static object x = new { Name = "jon" };
public static void Main(string[] args)
{
Console.WriteLine(x);
}
}
... but you can't change the type of x to var.
Yes it is possible, here is EXAMPLE
var Bar = new {num = 0};
var Baz = new {values = new List<int>()};
var Foo = new {Bar, Baz};
Console.WriteLine(JsonConvert.SerializeObject(Foo));
Of Course you can type it in one line
var Foo = {Bar = new {num = 0}, Baz = new {values = new List<int>()}};
Edit updated .Net fiddle with using Foo as class
No, this is not possible. The most straightforward way to do this is to simply create classes like you said. This is what I'd recommend.
void Main()
{
Console.WriteLine(JsonConvert.SerializeObject(new Foo { Bar = new Bar {
num = 0
},
Baz = new Baz { values = new[] { 0, 1, 2 } }
})); // {"Bar":{"num":0},"Baz":{"values":[0,1,2]}}
}
public class Foo {
public Bar Bar { get; set; }
public Baz Baz { get; set; }
}
public class Bar {
public int num { get; set; }
}
public class Baz {
public int[] values { get; set; }
}
Another approach, which loses static type checking, is typing it as object or dynamic:
void Main()
{
JsonConvert.SerializeObject(new Foo { Bar = new {
num = 0
},
Baz = new { values = new[] { 0, 1, 2 } }
}); // {"Bar":{"num":0},"Baz":{"values":[0,1,2]}}
}
class Foo {
public object Bar { get; set; }
public object Baz { get; set; }
}
It would probably be possible to write a custom JsonConverter to serialize a class like this as you wish (since each anonymous type in your example only has one real value inside it; if your real types are more complex, this won't work for those).
[JsonConverter(typeof(MyFooConverter))]
class Foo {
public int Bar { get; set; }
public int[] Baz { get; set; }
}
Related
i would like to merge the two objects and display their sum in the new object
The first i want to achieve is i have two object that have a 100 target sales and then the result must be 200.
The second i want to achieve is the two object has array and the array consist different value.
if the two object is merge the value of the array in the new object is the value of the two objects
simple example of my class
public class IncentivePayout
{
public decimal Sales { get; set; }
public List<IncentivePayoutDetail> IncentivePayoutDetails { get; set; }
}
public class IncentivePayoutDetail
{
public string ProductFamilyName { get; set; }
}
example
totalEthical: {
target: 100,
incentivePayoutDetails: [
{
productFamilyName: X,
},
]
totalConsumer: {
target: 100,
incentivePayoutDetails: [
{
productFamilyName: Y,
},
]
the result i want to achieve is
newObject: {
target: 200,
incentivePayoutDetails: [
{
productFamilyName: X,
},
{
productFamilyName: Y,
},
]
Ok here's my solution. Pretty straightforward. Link to codeshare.
I assumed how your classes might look like. If there are any inconsistencies, try to modify it to your needs.
If codeshare doesn't work. :
class Ethical{
public int SomeValue = {get;private set;}
public List<PayoutDetails> details = {get; private set;}
public Ethical(int SomeValue){
this.SomeValue = SomeValue;
this.details = new List<PayoutDetails>();
}
public Ethical(){
this.SomeValue = 0;
this.details = new List<PayoutDetails>();
}
public void AddPayoutDetails(PayoutDetails details){
this.details.add(details);
}
public void AddPayoutDetails(List<PayoutDetails> details){
details.ForEach(el => this.details.Add(el));
}
public void AddToValue(int anyNumber){
SomeValue += anyNumber;
}
}
//for merging 2 objects
public static Ethical sumEthicalProperties(Ethical obj1, Ethical obj2){
Ethical newObj = new Ethical(obj1.SomeValue + obj2.SomeValue)
newObj.Add(obj1.details);
newObj.Add(obj2.details);
return newObj;
}
//MoreUniversal
public static Ethical sumEthicalProperties(params Ethical[] objs){
int fullSum = 0;
Ethical newObj = new Ethical()
foreach(Ethical singleObj in objs){
fullSum += singleObj.SomeValue;
newObj.AddPayoutDetails = singleObj.details;
}
newObj.AddToValue(fullSum;
return newObj;
}
I have an object that I want to check whether it contains default values or not, in the below code but that doesn't cut it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
MyClass obj1 = new MyClass();
MyClass obj2 = null;
if(obj1 == new MyClass())
Console.WriteLine("Initialized");
if(Object.ReferenceEquals(obj1, new MyClass()))
Console.WriteLine("Initialized");
}
}
}
public class MyClass
{
public int Value {get; set; }
public MyClass()
{
this.Value = 10;
}
}
I have also used Object.ReferenceEquals() but that doesn't cut it as well.
This is the fiddle I am working on.
Is there a way to check whether an object contains default values, or if the object is empty?
Edit: In case of an newly initialized object with many nested properties, how to check whether they contain a default value or not?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
MyClass obj1 = new MyClass();
MyClass obj2 = null;
if(obj1 == new MyClass())
Console.WriteLine("Initialized");
if(Object.ReferenceEquals(obj1, new MyClass()))
Console.WriteLine("Initialized");
}
}
}
public class MyClass
{
public int Value {get; set; }
public MyNestedClass MyProperty { get; set; }
public MyClass()
{
this.Value = 10;
this.MyProperty = new MyNestedClass();
}
}
public class MyNestedClass
{
public string SomeStringProperty { get; set; }
public MyNestedClass()
{
this.SomeStringProperty = "Some string";
}
}
Here is the fiddle in the case of nested objects.
You can achieve your goal by overriding Equals and GetHashCode, creating and saving an immutable "default" instance, and comparing the value to it:
public class MyClass {
public static readonly MyClass DefaultInstance = new MyClass();
public int Value { get; set; }
public MyClass() {
this.Value = 10;
}
public override int GetHashCode() {
return Value.GetHashCode();
}
public override bool Equals(Object obj) {
if (obj == this) return true;
var other = obj as MyClass;
return other?.Value == this.Value;
}
}
Now you can check if the instance is equal to a newly created one by calling
if (MyClass.DefaultInstance.Equals(instanceToCheck)) {
... // All defaults
}
You can change what it means for an instance to be "default" by altering DefaultInstance object.
Note: this trick works well only with immutable MyClass. Otherwise some code could perform MyClass.DefaultInstance.Value = 20 and change the "default" object.
Here is one method using JSON serialization that allows you to check if the objects are equal or not:
DotNetFiddle:
using System;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
var defaultObj = new MasterObject();
var notDefaultObject = new MasterObject();
var defaultJson = JsonConvert.SerializeObject(defaultObj);
var notDefaultJson = JsonConvert.SerializeObject(notDefaultObject);
Console.WriteLine("First Test");
if (defaultJson == notDefaultJson)
Console.WriteLine("Same thing");
else
Console.WriteLine("Not same thing");
notDefaultObject.Sub1.SomeObject.SomeOtherValue = "Not a default Value";
notDefaultJson = JsonConvert.SerializeObject(notDefaultObject);
Console.WriteLine("Second Test");
if (defaultJson == notDefaultJson)
Console.WriteLine("Same thing");
else
Console.WriteLine("Not same thing");
}
}
public class MasterObject
{
public SubObject1 Sub1 { get; set; }
public SubObject2 Sub2 { get; set; }
public string SomeString { get; set; }
public MasterObject()
{
Sub1 = new SubObject1();
Sub2 = new SubObject2();
SomeString = "Some Default String";
}
}
public class SubObject1
{
public string SomeValue { get; set; }
public SubObject2 SomeObject { get; set; }
public SubObject1()
{
SomeObject = new SubObject2();
SomeValue = "Some other Default String";
}
}
public class SubObject2
{
public string SomeOtherValue { get; set; }
public SubObject2()
{
SomeOtherValue = "Some default";
}
}
Output:
First Test
Same thing
Second Test
Not same thing
What is happening is that you serialize the default object and then you make changes to the "not default object", re-serialize and compare again. This can be slow because you are generating strings, but as long as all the sub-objects can be serialized this will be the simplest way to compare if an object is "default" (what you get from new) or has been modified.
I'm trying to use Microsoft Bond to serialize nested objects. But Bond throws internal errors (such KeyNotFoundException).
My classes:
interface IFoo
{
}
[Bond.Schema]
class Foo1 : IFoo
{
[Bond.Id(0)]
public string Foo1Field { get; set; }
}
[Bond.Schema]
class Bar
{
[Bond.Id(0)]
public IFoo SomeFooInstance { get; set; }
}
Then I create an instance and serialize:
var src = new Bar() { SomeFooInstance = new Foo1() { Foo1Field = "Str" }};
var output = new OutputBuffer();
var writer = new CompactBinaryWriter<OutputBuffer>(output);
Serialize.To(writer, src);
var input = new InputBuffer(output.Data);
var reader = new CompactBinaryReader<InputBuffer>(input);
var dst = Deserialize<Bar>.From(reader);
But I'm getting exceptions (such KeyNotFoundException) at Serialize.To(writer, src);.
I also tried to add [Bond.Schema] to IFoo, but then the Deserialize<Bar>.From(reader); fails...
How can I serialize Bar class that contains some Foo class with Bond without getting exceptions like that?
If you want to use interfaces with behavioral differences (and not structural differences), the trick is to provide the deserializer with a factory so that it knows how to create a concrete instance of IFoo when it needs to. Notice that the implementations do not have the the [Bond.Schema] attribute, as they both implement the IFoo schema.
namespace NS
{
using System;
using Bond.IO.Unsafe;
using Bond.Protocols;
internal static class Program
{
[Bond.Schema]
interface IFoo
{
[Bond.Id(10)]
string FooField { get; set; }
}
[Bond.Schema]
class Bar
{
[Bond.Id(20)]
public IFoo SomeFooInstance { get; set; }
}
class AlwaysUppercaseFoo : IFoo
{
private string fooField;
public string FooField
{
get
{
return fooField;
}
set
{
fooField = value.ToUpperInvariant();
}
}
}
class IdentityFoo : IFoo
{
public string FooField { get; set; }
}
public static Expression NewAlwaysUppercaseFoo(Type type, Type schemaType, params Expression[] arguments)
{
if (schemaType == typeof(IFoo))
{
return Expression.New(typeof(AlwaysUppercaseFoo));
}
// tell Bond we don't handle the requested type, so it should use it's default behavior
return null;
}
public static Expression NewIdentityFoo(Type type, Type schemaType, params Expression[] arguments)
{
if (schemaType == typeof(IFoo))
{
return Expression.New(typeof(IdentityFoo));
}
// tell Bond we don't handle the requested type, so it should use it's default behavior
return null;
}
public static void Main(string[] args)
{
var src = new Bar() { SomeFooInstance = new IdentityFoo() { FooField = "Str" } };
var output = new OutputBuffer();
var writer = new CompactBinaryWriter<OutputBuffer>(output);
Bond.Serialize.To(writer, src);
{
var input = new InputBuffer(output.Data);
var deserializer = new Bond.Deserializer<CompactBinaryReader<InputBuffer>>(typeof(Bar), NewAlwaysUppercaseFoo);
var reader = new CompactBinaryReader<InputBuffer>(input);
var dst = deserializer.Deserialize<Bar>(reader);
Debug.Assert(dst.SomeFooInstance.FooField == "STR");
}
{
var input = new InputBuffer(output.Data);
var deserializer = new Bond.Deserializer<CompactBinaryReader<InputBuffer>>(typeof(Bar), NewIdentityFoo);
var reader = new CompactBinaryReader<InputBuffer>(input);
var dst = deserializer.Deserialize<Bar>(reader);
Debug.Assert(dst.SomeFooInstance.FooField == "Str");
}
}
}
}
If you need both behavioral and structural differences, then you'll need to pair this with polymorphism and a bonded<IFoo> field so that you can delay deserialization until you have enough type information to select the proper implementation. (Polymorphism is explicit and opt-in in Bond.)
I'd show an example of this, but while writing up this answer on 2018-02-21, I found a bug in the handling of classes with [Bond.Schema] that implement interfaces with [Bond.Schema]: the fields from the interface are omitted.
For now, the workaround would be to use inheritance with classes and use virtual properties. For example:
namespace NS
{
using System;
using Bond.IO.Unsafe;
using Bond.Protocols;
internal static class Program
{
enum FooKind
{
Unknown = 0,
AlwaysUppercase = 1,
Identity = 2,
}
// intentionally a class to work around https://github.com/Microsoft/bond/issues/801 but simulate an interface somewhat
[Bond.Schema]
class IFoo
{
[Bond.Id(0)]
public virtual string FooField { get; set; }
}
[Bond.Schema]
class Bar
{
[Bond.Id(0)]
public Bond.IBonded<IFoo> SomeFooInstance { get; set; }
[Bond.Id(1)]
public FooKind Kind { get; set; }
}
[Bond.Schema]
class AlwaysUppercaseFoo : IFoo
{
private string fooField;
public override string FooField
{
get
{
return fooField;
}
set
{
fooField = value.ToUpperInvariant();
}
}
[Bond.Id(0)]
public string JustAlwaysUppercaseFooField { get; set; }
}
[Bond.Schema]
class IdentityFoo : IFoo
{
[Bond.Id(42)]
public string JustIdentityFooField { get; set; }
}
static void RoundTripAndPrint(Bar src)
{
var output = new OutputBuffer();
var writer = new CompactBinaryWriter<OutputBuffer>(output);
Bond.Serialize.To(writer, src);
var input = new InputBuffer(output.Data);
var reader = new CompactBinaryReader<InputBuffer>(input);
var dst = Bond.Deserialize<Bar>.From(reader);
switch (dst.Kind)
{
case FooKind.Identity:
{
var fooId = dst.SomeFooInstance.Deserialize<IdentityFoo>();
Console.WriteLine($"IdFoo: \"{fooId.FooField}\", \"{fooId.JustIdentityFooField}\"");
}
break;
case FooKind.AlwaysUppercase:
{
var fooUc = dst.SomeFooInstance.Deserialize<AlwaysUppercaseFoo>();
Console.WriteLine($"UcFoo: \"{fooUc.FooField}\", \"{fooUc.JustAlwaysUppercaseFooField}\"");
}
break;
default:
Console.WriteLine($"Unknown Kind: {dst.Kind}");
break;
}
}
public static void Main(string[] args)
{
var o = new OutputBuffer();
var w = new CompactBinaryWriter<OutputBuffer>(o);
Bond.Serialize.To(w, new IdentityFoo() { FooField = "Str", JustIdentityFooField = "id" });
var src_id = new Bar()
{
SomeFooInstance = new Bond.Bonded<IdentityFoo>(new IdentityFoo() { FooField = "Str", JustIdentityFooField = "id" }),
Kind = FooKind.Identity
};
var src_uc = new Bar()
{
SomeFooInstance = new Bond.Bonded<AlwaysUppercaseFoo>(new AlwaysUppercaseFoo() { FooField = "Str", JustAlwaysUppercaseFooField = "I LIKE TO YELL!" }),
Kind = FooKind.AlwaysUppercase
};
RoundTripAndPrint(src_id);
RoundTripAndPrint(src_uc);
}
}
}
This prints:
IdFoo: "Str", "id"
UcFoo: "STR", "I LIKE TO YELL!"
I define two interfaces as following:
public interface IData
{
double value { set; get; }
}
public interface IInterval<C, M>
{
C left { set; get; }
M data { set; get; }
}
Then I use these two interfaces for following class declaration.
public class TESTClass<I, M>
where I : IInterval<int, M>, new()
where M : IData
{
public I interval{ set; get; }
public TESTClass()
{
// This is invalid, cos property value is not visible at initialization ...
interval = new I() { left = 0, data.value = 0 };
// instead I have to do as:
interval = new I() { left = 0 };
interval.data.value = 0;
}
}
am I missing something here ?
I would appreciate if you could help me figure this out.
Well, you could certainly do that. Syntax is bit different.
public TESTClass()
{
interval = new I()
{
left = 0,
data = //Reads data property,
{
value = 0 //set data.value to 0
}
}
}
You can't access sub-properties in object initializers using the member-access operator (.).
This works:
public class TESTClass<I, M>
where I : IInterval<int, M>, new()
where M : IData, new()
{
public I interval{ set; get; }
public TESTClass()
{
interval = new I() { left = 0, data = new M {value = 0} };
}
}
C#4.0 brings optional parameters, which I've been waiting for for quite some time. However it seems that because only System types can be const, I cannot use any class/struct which I have created as an optional parameter.
Is there a some way which allows me to use a more complex type as an optional parameter. Or is this one of the realities that one must just live with?
The best I could come up with for reference types was:
using System;
public class Gizmo
{
public int Foo { set; get; }
public double Bar { set; get; }
public Gizmo(int f, double b)
{
Foo = f;
Bar = b;
}
}
class Demo
{
static void ShowGizmo(Gizmo g = null)
{
Gizmo gg = g ?? new Gizmo(12, 34.56);
Console.WriteLine("Gizmo: Foo = {0}; Bar = {1}", gg.Foo, gg.Bar);
}
public static void Main()
{
ShowGizmo();
ShowGizmo(new Gizmo(7, 8.90));
}
}
You can use the same idea for structs by making the parameter nullable:
public struct Whatsit
{
public int Foo { set; get; }
public double Bar { set; get; }
public Whatsit(int f, double b) : this()
{
Foo = f; Bar = b;
}
}
static void ShowWhatsit(Whatsit? s = null)
{
Whatsit ss = s ?? new Whatsit(1, 2.3);
Console.WriteLine("Whatsit: Foo = {0}; Bar = {1}",
ss.Foo, ss.Bar);
}
You can use any type as an optional parameter:
using System;
class Bar { }
class Program
{
static void Main()
{
foo();
}
static void foo(Bar bar = null) { }
}
Okay, I reread your question and I think I see what you mean - you want to be able to do something like this:
static void foo(Bar bar = new Bar()) { }
Unfortunately this is a not allowed since the value of the default parameter must be known at compile time so that the compiler can bake it into the assembly.