C# .我认为当覆盖的字段是对象引用时,这是不允许的,但如果它不起作用,那就糟了。美好的(是的,邪恶,邪恶!)调试器不太喜欢这种方法。在尝试评估时,它很难理解发生了什么。 struct Color { public byte R, G, B, A; }

C# .我认为当覆盖的字段是对象引用时,这是不允许的,但如果它不起作用,那就糟了。美好的(是的,邪恶,邪恶!)调试器不太喜欢这种方法。在尝试评估时,它很难理解发生了什么。 struct Color { public byte R, G, B, A; } ,c#,arrays,casting,C#,Arrays,Casting,.我认为当覆盖的字段是对象引用时,这是不允许的,但如果它不起作用,那就糟了。美好的(是的,邪恶,邪恶!)调试器不太喜欢这种方法。在尝试评估时,它很难理解发生了什么。 struct Color { public byte R, G, B, A; } Color[] bitmap1 = ...; uint[] bitmap2 = MagicCast(bitmap1); unit[] bitmap2 = bitmap1.Select((c)=>c.ToUint32()).ToArra


.我认为当覆盖的字段是对象引用时,这是不允许的,但如果它不起作用,那就糟了。美好的(是的,邪恶,邪恶!)调试器不太喜欢这种方法。在尝试评估时,它很难理解发生了什么。
struct Color
{
    public byte R, G, B, A;
}

Color[] bitmap1 = ...;
uint[] bitmap2 = MagicCast(bitmap1);
unit[] bitmap2 = bitmap1.Select((c)=>c.ToUint32()).ToArray();
public static uint ToUint32(this Color color) { ... }
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Explicit)]
struct Color
{
  [FieldOffset(0)]
  public byte R;

  [FieldOffset(1)]
  public byte G;

  [FieldOffset(2)]
  public byte B;

  [FieldOffset(3)]
  public byte A;

  [FieldOffset(0)]
  public uint value;
}
var allRedBytes = bitmap1.Select(c => c.R);
var allUInts = bitmap1.Select(c => c.value);
[StructLayout(LayoutKind.Explicit)]
struct Color
{
   [FieldOffset(0)]
   byte R;
   [FieldOffset(1)]
   byte G;
   [FieldOffset(2)]
   byte B;
   [FieldOffset(3)]
   byte A;

   [FieldOffset(0)]
   uint Value;
}
var converted = colorArray.Select( c => c.Value );
struct Color
{
   //...As before...

   public static implicit operator uint(Color input)
   {
      return input.Value;
   }

   public static explicit operator uint(Color input)
   {
      return input.Value;
   }
}
Color a = new Color();
uint b = a;        //Completely valid, with the implicit cast.
uint c = (uint)a;  //Also valid, but the implicit cast makes it unnecessary.
public static class ColorExtension
{
    public static uint[] GetUInts(this Color[] colors)
    {
        if(colors == null) throw new ArgumentNullException("colors");
        Evil e = new Evil { Colors = colors};
        return e.UInts;
    }

    [StructLayout(LayoutKind.Explicit)]
    struct Evil
    {
        [FieldOffset(0)]
        public Color[] Colors;
        [FieldOffset(0)]
        public uint[] UInts;
    }
}
Color[] colors =  ...

uint[] uints = colors.GetUInts();
[StructLayout(LayoutKind.Sequential)] // Or explicit, but i bellieve Sequential is enough.
struct Color
{
      public byte R;

      public byte G;

      public byte B;

      public byte A;
}
unsafe void Main()
{
    Color[] c = new Color[2]
    {
        new Color { R = 255, G = 0, B = 0, A = 0 },
        new Color { R = 0, G = 255, B = 0, A = 0 }
    };
    c.Dump();


    fixed (byte* p = &c[0].R)
    {
        uint* i = (uint*)p;
        *i = 0x11223344;
        *(i + 1) = 0x55667788;
    };
    c.Dump();
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Color
{
    public byte R, G, B, A;
}
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    [StructLayout(LayoutKind.Explicit)]
    struct Color
    {
        [FieldOffset(0)]
        public uint value;

        [FieldOffset(0)]
        public byte R;
        [FieldOffset(1)]
        public byte G;
        [FieldOffset(2)]
        public byte B;
        [FieldOffset(3)]
        public byte A;

        static public implicit operator uint(Color c)
        {
            return c.value;
        }
    }

    class Program
    {
        static unsafe void Main(string[] args)
        {
            Color[] colors = new Color[] { new Color { R = 255 }, new Color { B = 255 } };
            Console.WriteLine(colors[0]);
            Console.WriteLine(colors[1]);
            fixed (Color* thisPtr = &colors[0])
            {
                ((uint*)thisPtr)[0] = 2;
                ((uint*)thisPtr)[1] = 4;
                Console.WriteLine(colors[0]);
                Console.WriteLine(colors[1]);
            }
            Console.ReadKey();
        }
    }
}