Monthly Archives: August 2014

Serialization with internal types

Hey people, I’ve run into some issues with serialization.

The usual problem with internal types comes when you are testing. If you want the test to access those internal members, you have to add in the AssemblyInfo.cs of the “exposed” assembly something like this:

[assemblyInternalsVisibleTo(“BlablaAssemblyThatAccessessMyInternals, PublicKey=blabla”)]

But this isn’t the only problem when you deal with internals.

I happened to me just today: I have some test data formatted in XML. I wanted to deserialize it, but… the type that I wanted to deserialize was internal. I used the XmlSerializer as anyone would do in the first approach (at least I did it! J) but as soon as I ran the test it complained that

“[TypeName] is inaccessible due to its protection level. Only public types can be processed.”

Glad to know, dude!

But isn’t that a shitty message? I think so.

Then, I made a brief research on the topic and soon discovered that the DataContractSerializer could possibly make my day, so I decorated all the entities that are implied in the serialization with [DataContact] and [DataMember] attributes.

Yay, it worked!

But not the way I thought.

And this is important: the DataContractSerializer is known to be much more restrictive than the XmlSerializer. One of the restrictions is that it reads XML elements are in alphabetical order!! Yes, ORDER MATTERS!! I have realized of that after most of the properties didn’t have values set!

For example, this Entity is read correctly by the DataContractSerializer

<Entity xmlns="http://schemas.datacontract.org/2004/07/MyAssembly.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
	<AccountData>54148300670010000089111</AccountData>
	<Date>2010-03-12T00:00:00</Date>
	<UserName>JMN</UserName >
</Entity>

Did you notice the order in the elements?

The following entity will not be read correctly!

<Entity xmlns="http://schemas.datacontract.org/2004/07/MyAssembly.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
	<UserName>JMN</UserName >	
	<Date>2010-03-12T00:00:00</Date>
	<AccountData>54148300670010000089111</AccountData>
</Entity>

Problem solved!

More 😀

Advertisements

SQL Inner Join

Yeah, I’m a SQL loser. But in my current you I’m making some progress and I have just made a successful INNER JOIN after more than 5 years of total SQL apathy!

SELECT *

FROM A

INNER JOIN B ON

 A.IdToB = B.Id WHERE bla bla bla

Definitely I prefer the LINQ syntax 😛

Feeling happy!