Converting values between same structure within different namespace. C#

前端 未结 7 1420
情书的邮戳
情书的邮戳 2021-01-13 17:28

I have two similar[all attributes are same.] structures within different namespaces.

Now when i try to copy values between the objects of these structures i am getti

7条回答
  •  北海茫月
    2021-01-13 17:32

    You can use a union:

    public struct A
    {
        int x, y;
        double a, b;
    
        public A(int x, int y, double a, double b)
        {
            this.x = x;
            this.y = y;
            this.a = a;
            this.b = b;
        }
    }
    public struct B
    {
        int x, y;
        double a, b;
    }
    
    [StructLayout(LayoutKind.Explicit)]
    public class Union
    {
        [FieldOffset(0)]
        public A a;
        [FieldOffset(0)]
        public B b;
    
        public Union(A a)
        {
            this.b = default(B);
            this.a = a;            
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A(5, 10, 0.25, 0.75);
            Union union = new Union(a);
            B b = union.b; //contains 5,10,0.25,0.75
        }
    }
    

提交回复
热议问题