English 中文(简体)
is there any api provided by dot net platform to convert a string or bytres array into user defined object?
原标题:

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?

最佳回答

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.

问题回答

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.

(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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签