Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# copy each property

public static void CopyPropertiesTo<T, TU>(this T source, TU dest)
{
    var sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList();
    var destProps = typeof(TU).GetProperties()
            .Where(x => x.CanWrite)
            .ToList();

    foreach (var sourceProp in sourceProps)
    {
        if (destProps.Any(x => x.Name == sourceProp.Name))
        {
            var p = destProps.First(x => x.Name == sourceProp.Name);
            if(p.CanWrite) { // check if the property can be set or no.
                p.SetValue(dest, sourceProp.GetValue(source, null), null);
            }
        }

    }

}
Comment

PREVIOUS NEXT
Code Example
Csharp :: attribute c# get method name reflection 
Csharp :: how to call method in different project in c# visual studio 
Csharp :: Nested objects with linq expression 
Csharp :: c# int cast error 
Csharp :: ef null check 
Csharp :: How to compile just one file in c# 
Csharp :: how to make a variable unity 
Csharp :: find first occurrence of character in string 
Csharp :: unity inspector sliders 
Csharp :: integer to boolean conversion in unity C# 
Csharp :: how to round in c# 
Csharp :: unity stack overflow error 
Csharp :: protected override void OnExiting(Object sender, EventArgs args) { base.OnExiting(sender, args); Environment.Exit(Environment.ExitCode); } 
Csharp :: how to make play button in unity 
Csharp :: freelance 
Csharp :: c# arraylist to listview 
Csharp :: ik not working unity 
Csharp :: block nulltarge tpl dataflow 
Csharp :: export2excel with logo and header and many table on one click stackoverflow 
Html :: html input integer and positive 
Html :: how to open link in a new tab 
Html :: align center inner div using bootstrap 
Html :: autoredirect html 
Html :: notyf 
Html :: input type file csv only 
Html :: how to link new tab in html button 
Html :: image drive inside html 
Html :: html favicon 
Html :: start html 
Html :: html star symbol 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =