Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Struct [FieldOffset(0)] Attribute not Implemented


  • Please log in to reply
8 replies to this topic

#1 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 19 December 2011 - 12:48 AM

Why is this not implemented? It means that we cannot run code like this,
[StructLayout(LayoutKind.Explicit)]
 struct UIntFloat 
    {        
         [FieldOffset(0)] <-- not implemented
        public float FloatValue; 

        [FieldOffset(0)] 
        public uint IntValue;         
    } 

public static float ToSingle(uint value) 
    { 
        UIntFloat uf = new UIntFloat(); 
        uf.IntValue = value; 
        return uf.FloatValue; 
    } 

public static uint ToUint(float value) 
    { 
        UIntFloat uf = new UIntFloat(); 
        uf.FloatValue = value; 
        return uf.IntValue; 
    }
It seems to be ignored or of very low priority,

http://netmf.codeplex.com/workitem/309

Also, does anyone know how to do this with the Micro Framework?

http://www.jasonbock...nsInDotNet.aspx

Baxter

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 19 December 2011 - 04:41 AM

The FieldOffset has only reason to exist for binary serialization, and native interop. However, for structs only. To tell the truth, it's over five years I'm using regular .Net, but I never used any interop or FieldOffset. Unions are not available too: if you wish some dirty trick, you'd use the "unsafe" context. The basic question is: why should I need similar features? The aim of the managed code is just to hide *any* potential dangerous/unsafe operation, such as unions and pointers. However, I agree on the lack of de/serialization APIs on Netduino. Cheers
Biggest fault of Netduino? It runs by electricity.

#3 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 19 December 2011 - 05:09 AM

The basic question is: why should I need similar features?

Well, I guess the reason is to create a union in C#
Baxter

#4 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 19 December 2011 - 12:40 PM

Well, I guess the reason is to create a union in C#
Baxter


The question is right the same: why a C# programmer should need an union? That would be contrary to the C# type-safety base.
Biggest fault of Netduino? It runs by electricity.

#5 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 19 December 2011 - 07:23 PM

That would be contrary to the C# type-safety base

Hi Mario,
It is not. If you look at the code that I posted, the missing attribute simply allows a workaround for the missing Bitconverter functionality in the Micro Framework because it simulates a C union. Running the code in regular .NET C# (using System.Runtime.InteropServices;) gives,
static void Main(string[] args)
        {
            float a = 123.456f;
            uint b = ToUint(a); => 1123477881
        }
The integer is easily converted to an array of bytes. Note that there is no unsafe code at all (in Build, Allow unsafe code is unchecked), merely straight C#.

Baxter

#6 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 20 December 2011 - 04:33 AM

Baxter, I mean what's your goal. I also agree that the BitConverter is a missing brick.
However, the conversion could be yielded even using the "unsafe". That is, there's no need of new statements: the language is really powerful by itself.
Take a look at this:
http://forums.netdui...ch__1#entry2082
Biggest fault of Netduino? It runs by electricity.

#7 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 22 December 2011 - 12:14 AM

Well Mario, This dialog is going nowhere. Why does a mountain climber climb a mountain? Answer: because it is there. This is not a new a statement request. There is also a Codeplex request. The statement is in regular C# and VB; just search MSDN. I have implemented Luke Cummings unsafe BitConverter as a DLL for use in both VB and C#. So, I guess the question is moot. Baxter

#8 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 December 2011 - 07:23 PM

Interesting. .NET MF is a subset of .NET (necessary to fit it on these small devices). That said, I always like seeing more features of the regular CLR making it to NETMF... Chris

#9 aalmada

aalmada

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts
  • LocationPortugal

Posted 20 April 2012 - 09:43 AM

Unions are not available too: if you wish some dirty trick, you'd use the "unsafe" context.


"unsafe" is not officially supported: http://forums.netdui...ndpost__p__2151

Just like baxter, I used the "union" method several times in regular .NET. I tried this approach to avoid the "not-officially supported unsafe" and crashed into the same wall. StructLayoutAttribute is supported but FieldOffsetAttribute is not.

This is plain weird! .NET MF is a subset of .NET but for micro-processors. One of the basic required functionalities in this scenario is serial communication and, there is no official way to marshal floating points and structures... :-/

aalmada




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.