vb.net serialization of system objects? - vb.net

I am working with sending objects over network. I have it working with custom objects just fine but my question is can I serialize system objects? For example I want to serialize the System.Net.NetworkInformation.NetworkInterface object on one computer and send it to another so I can load all of its properties easily. Is this possible? This doesn't seem to work:
<Serializable()> NIC As System.Net.NetworkInformation.NetworkInterface
Thanks in advance

An object type has to be designed to be serializable, and marked so in it's original code. You will need to build your own object to keep the properties that you care about, mark it serializable, and use that.

Related

can you give me an example of not serializable data?

I'm studying some basic informatic concepts, so this could be a very noob question. I've understood what Serialization is, and what is used for. What I'm trying to understand is if all objects are serializable. You can download from the Internet any file you like: to do that all the files have to be serializable, right?
Is there a file or an object that you cannot serialize?
Thank you!
Some objects contain references to system resources such as file descriptors. You may still serialize such objects because file descriptor is a 32/64 bit number. But if you deserialize it in another process/computer, it won't have any meaning because system resources are valid only in process where they were acquired. So, logically not all objects are serializable.

How can I modify my my RestKit managed object before it is saved via Core Data?

I have RestKit setup nicely with a Core Data managed object backing but I have some fields which are not present on the server, only in the local model class.
How can I set these fields before the object is persisted. Is there something like a 'willSave' delegate method I can implement?
Thanks
I don't quite understand what you want to accomplish, but you can override willSave in NSManagedObject. The docs give a good explanation of what it does.
If you want to modify the incoming data before you save it, you should consider willMapData

How can I serialize Petrel Property in my Custom DataSource and load it back?

I am not very "fluent" with Ocean serialization .
Can I serialize an entire Petrel Property (Properties, Grids or any other Petrel/Ocean's objects) into my custom DataSource? Will I be able to load it back?
Is there any good practice/pattern to do that?
Some code sample would be welcome!
Do you have an established DataSource already? The persistence back end (SQL? XML?) used by your DataSource dictates how data is stored. Any data you want to persist through the DataSource must be converted to your back-end's format.
Note that there is no such thing as "Ocean serialization" with DataSources - you (and only you) are in complete control of the DataSource. Usually, you are actually providing it as a service to Ocean, so that it, given a Droid, can resolve one of your objects (be they e.g. custom domain objects, workstep argument packages or seismic attribute argument packages).
Now, from your question, it sounds like you are seeking to store deep copies of the Petrel data you mention. Is this really the case? If so, I'm afraid you'll need to make your own data structures representing this data, mirroring what you can read out through Ocean's APIs.
If what you really want to store is a weak reference to the Petrel data (implementing IIdentifiable), you'll want to persist the contents of each object's Droid - a much simpler task.
Then, when your persisted data is resolved from your DataSource, you'd rebuild the Droid(s), which can then be resolved themselves (using some other DataSource but your own), resulting in a regular strong .NET reference to the object - assuming of course that this data is present in the currently loaded project.
The SimpleDataSourceExample in the Ocean SDK demonstrates a simple DataSource backed by a .dat file using BinaryFormatter. This is relatively trivial to modify to other back-ends. I strongly recommend XML over BinaryFormatter, but if you intend to store considerable amounts of bulk data, you should consider a database. At Blueback Reservoir, XML has served our needs very well.
A minor caveat: make sure that the objects you store in your DataSource implement IDisposable (as well as IIdentifiable), to free resources in the DataSource.

Serialization of Objects

how does Serialization of objects works? How object got deserialized and a instance is created from serialized date without a call to any constructor?
I've kept this answer language agnostic since a language wasn't given.
When the object is serialized, all the require information to rebuild it is encoded in way which can be retrieved. This typically includes the type of the object, as well as the value of all the instance variables.
When the object is deserialized, an area in memory of the correct size is allocated and is populated using the serialized information such that the new object is identical to the serialized one.
The running program can then refer to this new object in memory without having to actually call the constructor.
There are lots of little details which this doesn't explain, but this is the general idea of serialization/deserialization.
Are you talking about Java? If so, serialization is an extralingual object creation mechanism. It's a backdoor that uses native code to create the object without calling any constructors. Therefore, when designing a class for serializability, you need to make sure that a class created through deserialization maintains the same invariants (key fields being initialized) as you would through the constructor path. A third way to create objects in Java is through cloning, and similar issues apply.
Cloning and serialization don't interact well with the use of final fields if you need to set the value of that field to something different than what is returned by clone or the deserialization process.
Josh Bloch's "Effective Java" has some chapters that explain these issues in more depth.
(this answer may apply to other languages too, but I've only used serialization in Java)
Regarding .NET: this isn't a definitive or textbook answer, and I might be all-out wrong...
.NET Serialization needs to be seperated out into Binary vs. others (XML or an XML derivitave typically). Binary serialization is mostly a black-box to me, but it allows the object to be serialized and restored in their current state. XML serialization typically only serialized the public fields/properties of an object, unless overriden by adding a custom ISerializable implementation.
In the case of XML serialization I believe .NET uses Reflection to determine which fields and properties get converted to their equivalent Elements. Adding an [XMLSerializable] attribute will implement a default behavior which can be adjusted by applying other attributes at the field level (such as [XMLAttribute]).
The metadata (which Reflection depends on) stores all the object members as well as their attributes and addresses, which allows the serializer to determine how it should build the output.

Easiest way to convert CArchive to use SQL database for serialization?

I have an existing application that uses CArchive to serialize a object structure to a file. I am wondering if can replace the usage of CArchive with some custom class to write to a database the same way that things are serialized. Before I go about figuring if this is doable I was wondering if other people have done the same- what is the best approach to this problem? I would like to be able to create a drop in replacement for the usage of CArchive so that the existing object structure would simply read/write to/from a database rather than a serialized file. Is it as simple as overwriting the Serialize method for each class?
Short answer: forget it.
Longer answer:
CArchive doesn't have a single virtual member. Even it's destructor isn't virtual, which means you're not supposed to derive from that class (C# programmers say sealed).
There's only one possibility that I can think of to customize CArchive's work (and without rewriting the whole serialization code in CDocument): Construct your CArchive object by passing it a pointer to CFile derived class that would handle the data connection for you.
From there on, how you control your database by simply overriding CFile's Read() and Write() is beyond my imagination :-(