walruz
walruz

Reputation: 1329

How to copy fields from one structure to another

I have several input structures which I need to convert to some other structure so I can pass it to my method.

struct Source1
{
    public float x1;
    public float x2;
    public float x3;
}

struct Source2
{
    public float x1;
    public float x3;
    public float x2;
    public float x4;
}

struct Target
{
    public float x1;
    public float x2;
    public float x3;
}

I am sure that source structure has required field (type and name is what matters) but that field's offset is unknown. Also source structure might contain some extra fields that I don't need.

How do I copy required fields from source structure to target structure. I need to do it as fast as possible.

In C there is a very simple recipe for that kind of problems.

#define COPY(x, y) \
{\
x.x1 = y.x1;\
x.x2 = y.x2;\
x.x3 = y.x3;\
}

I was thinking about getting a collection of fields and then get field's value using its name as a key but it looks like slow solution to me.

Upvotes: 0

Views: 148

Answers (2)

Mahmood Shahrokni
Mahmood Shahrokni

Reputation: 236

Look at this explicit cast

This is the source struct.

 public struct Source
    {
        public int X1;
        public int X2;
    }

This is the target.

 public struct  Target
    {
        public int Y1;
        public int Y2;
        public int Y3;

        public static explicit operator Target(Source source)
        {
            return new Target
            {
                Y1 = source.X1,
                Y2 = source.X2,
                Y3 = 0
            };
        }

}

Converting phase :

 static void Main(string[] args)
        {
            var source = new Source {X1 = 1, X2 = 2};
            var target = (Target) source;
            Console.WriteLine("Y1:{0} ,Y2{1} ,Y3:{2} ",target.Y1,target.Y2,target.Y3);
            Console.Read();
        }

Upvotes: 0

mjwills
mjwills

Reputation: 23898

Have a squiz at https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/how-to-implement-user-defined-conversions-between-structs .

It details the use of implicit operators which are one approach to consider.

Some example code:

using System;

namespace Test
{
    struct Source1
    {
        public float x1;
        public float x2;
        public float x3;

        public static implicit operator Target(Source1 value)
        {
            return new Target() { x1 = value.x1, x2 = value.x2, x3 = value.x3 };
        }
    }

    struct Target
    {
        public float x1;
        public float x2;
        public float x3;
    }

    public class Program
    {
        static void Main(string[] args)
        {
            var source = new Source1() { x1 = 1, x2 = 2, x3 = 3 };
            Target target = source;

            Console.WriteLine(target.x2);

            Console.ReadLine();
        }
    }
}

Another alternative is using AutoMapper. Performance will be slower though.

Upvotes: 2

Related Questions