views:

43

answers:

3

Suppose i have input array byte A[50];

i have put three diffrent data types values in array as below

  1. string of length 42 bytes(converted into binary)
  2. long with length 4 bytes(converted into binary)
  3. float with length of 4 bytes(converted into binary)

Now i have defined a schema like as below

    <schemaforparsing>
          <field>
                <name>fieldname1</name>
                <type>string</type>
                <length>42</length>
          </field>

         <field>
               <name>fieldname2</name>
               <type>long</type>
               <length>4</length>
         </field>

         <field>
              <name>fieldname3</name>
               <type>float</type>
              <length>4</length>
         </field>
    </schemaforparsing>

i want to parse this bytes array in to an user defined object. Userdefined object 's class should be generated from specified schema.like in this case class will be as below

      classGenerated
      {
           String fieldname1[42];
           long fieldname2;
           float fiedlname3;
      }

So basically i want a component which will take input of a schema and based upon that schema, after parsing binary data in array it will generate object of class related to transaction schema.

Does dot net 3.5 platform provides such a component?

+1  A: 

Well, it sounds like you basically want binary serialization of some form.

Normal built-in .NET serialization works with existing classes rather than a template, but personally I'm not terribly keen on it.

If you want template-generated classes, I can recommend Protocol Buffers - portable, compact, efficient to serialize/deserialize. There are two main C# ports - my own, and Marc Gravell's one. Of course, there are other binary serialization formats around as well... protobuf is just the one I'm most familiar with.

Jon Skeet
i dont want to deserialize binary into user defined object.I have edited my question.Please review it.Thanks
Maddy.Shik
You have binary data, and you want to end up with an object. "i want to parse this bytes array in to an user defined object" In what way are you *not* trying to deserialize binary into a user-defined object? (Byte array = binary, just in case that's the source of confusion.)
Jon Skeet
+1  A: 

(Edited)There is no such component in the framework besides the usual binary serializer which uses its own "schema". It might not be too hard to implement yourself. I suppose the schema tells you something like this:

  • first there are 42 bytes to be interpreted as string, assign to field 'fieldname1'
  • second there are 4 bytes to be interpreted as long, assign to field 'fieldname2'
  • ...

Shouldn't be too hard.

EricSchaefer
i dont want to deserialize binary into user defined object.I have edited my question.Please review it.Thanks
Maddy.Shik
+1  A: 

Without context this is a difficult question to answer - what do you want to do with the "user defined" object?

If you need to be able to create objects at runtime with dynamic fields, a possible solution is to wait until c# 4's dynamic type and deserialize into the ExpandoObject, see ExpandoObject.

David_001