Working around parameter as reference limitation

ZEKE ROSS 0 Reputation points
2023-03-06T19:12:26.1366667+00:00

Using C# 6.0

I need to copy the value of many properties (like 400 properties) from one object to another, however if the default value is the same then it is important that I don't copy it.

So I have code that looks like this

private void Set<T>(T to, T from)

{

if (EqualityComparer<T>.Default.Equals(to, from) == false) to = from;

}

However, since it is properties that I am passing, I can't pass the "to" by reference. I also can't just return the value, as I don't want to set the "to" value if it is the same as the "from" value.

I can't change the object itself, as I am using API for existing software, and the properties are part of the API.

A typical call to Set function currently looks like this

Set(oInvoice.Lines.ItemCode, oDelivery.Lines.ItemCode);

Any changes that could get me this limitation without having to write out for each line would be really appreciated.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,316 Reputation points
    2023-03-07T05:40:48.6033333+00:00

    @ZEKE ROSS, Welcome to Microsoft Q&A, if you want to copy value from one object to another object without having to write out for each line, I recommend that you could use reflection to do it.

    Here is a code example you could refer to.

        internal class Program
        {
            static void Main(string[] args)
            {
                Example ex1 = new Example() { Name = "test1", Id = 1001, Address = "home" };
                Example ex2 = new Example() { Name = "test2", Id = 1001, Address = "home2" };
                Set<Example>(ex1, ex2);
            }
            private static void Set<T>(T to, T from)
            {
                var result1 = to.GetType().GetProperties().ToList();
                var result2 = from.GetType().GetProperties().ToList();
                for (int i = 0; i < result1.Count; i++)
                {
                    if(result1[i].GetValue(to,null)!= result2[i].GetValue(from, null))
                    {
                        result1[i].SetValue(to, result2[i].GetValue(from, null));
                    }
                }
            }
     
        }
        public class Example
        {
    
            public string Name { get; set; }
    
            public int Id { get; set; }
    
            public string Address { get; set; }
    
        }
    
    

    As the following result shown. the ex1's property's value all set to be the same as the ex2's property's value.

    User's image

    Hope my solution could be helpful for you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.