-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectExtensions.cs
More file actions
60 lines (53 loc) · 1.62 KB
/
ObjectExtensions.cs
File metadata and controls
60 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace RexStudios.CsharpExtensions
{
public static class ObjectExtensions
{
/// <summary>
/// Makes a copy from the object.
/// Doesn't copy the reference memory, only data.
/// https://www.extensionmethod.net/csharp/object/clone-t
/// </summary>
/// <typeparam name="T">Type of the return object.</typeparam>
/// <param name="item">Object to be copied.</param>
/// <returns>Returns the copied object.</returns>
public static T Clone<T>(this object item)
{
if (item != null)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, item);
stream.Seek(0, SeekOrigin.Begin);
T result = (T)formatter.Deserialize(stream);
stream.Close();
return result;
}
else
return default(T);
}
/// <summary>
/// if the object this method is called on is not null, runs the given function and returns the value.
/// if the object is null, returns default(TResult)
/// https://www.extensionmethod.net/csharp/object/ifnotnull-t-tresult
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="target"></param>
/// <param name="getValue"></param>
/// <returns></returns>
public static TResult IfNotNull<T, TResult>(this T target, Func<T, TResult> getValue)
{
if (target != null)
return getValue(target);
else
return default(TResult);
}
}
}