1

I serialized a class and sent it to another application using IPC, however when i try to cast the received class to the same class (but in different assembly) i always get null if (in case of safe cast) or throw exception if direct cast..

So how can i access data members of the received class throw IPC ? note i use JSON Serialize and De-serialize to send my class to the other application.

I tried setting both applications with the same assembly name but this doesn't work.

Also i tried setting the Assembly GUID with the same result.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
Daniel Eugen
  • 2,712
  • 8
  • 33
  • 56
  • Check [this](http://stackoverflow.com/questions/4865104/convert-any-object-to-a-byte) out and apply the reversed logic to convert it back from bytes to an object. – SimpleVar Feb 11 '15 at 05:45
  • @YoryeNathan it is not byte it is Json object as i stated i use JSON.net to serialize and deserialize the object – Daniel Eugen Feb 11 '15 at 05:46
  • 2
    If you have same class name in different assemblies then they are two different types even if the have same name. Make a common assembly and using on both serialization and de-serialization end. – Adil Feb 11 '15 at 05:47
  • because binary needs a lot of edits to the existing code, JSON.NET seems to work very nicely however it doesn't cast the received objects to their equivelants in the target assembly – Daniel Eugen Feb 11 '15 at 05:47
  • Everything is made out of bytes. If you already have a different serialization/deserialization method (e.g JSON), that's just as good. Include your code to show where the actual problem is. – SimpleVar Feb 11 '15 at 05:48
  • Also see solutions from: [InvalidCastException for two Objects of the same type](https://stackoverflow.com/q/2500280/1366033) – KyleMit Dec 27 '17 at 22:24

2 Answers2

2

If you have same class name in different assemblies then they are two different types even if the have same name. Make a common assembly and using on both serialization and de-serialization end.

If you do not want to have a common assembly which is recommended way, you can get the serialized json string and get the properties and values from this string and use them to create the object you want.

I used JSON.NET to convert the json string to dictionary to access the properties with values. You can more way here.

Dictionary<string, string> dic = JsonConvert.DeserializeObject < Dictionary<string, string>>(jsonSerializedString);
MyObject myObject = new MyObject { Name = dic["Name"], Id = int.Parse(dic["Id"]) };

I used MyObject which would be replaced by your type

public class MyObject
{
    public int Id { get; set; }
    public string Name { get; set; }
}
Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
-2

Don't confuse different assembly with different namespace.

If this is really the same class you should have it in a namespace which is visible/accessible by both sides. Try to put your class in a common assembly which is referenced on both sides.

If you can't change the assembly structure define an interface (in an additional assembly) which can be referenced by both sides. On the receiving side you may declare a new class which implements this interface.

DrKoch
  • 9,556
  • 2
  • 34
  • 43