~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/Data/ICSharpCode.Data.Core/Common/Objects.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
#region Usings
 
5
 
 
6
using System;
 
7
using System.Collections.Generic;
 
8
using System.Linq;
 
9
using System.Text;
 
10
 
 
11
#endregion
 
12
 
 
13
namespace ICSharpCode.Data.Core.Common
 
14
{
 
15
        /// <summary>
 
16
        /// ExtensionMethods for System.Object.
 
17
        /// </summary>
 
18
        public static class Objects
 
19
        {
 
20
                #region Public methods
 
21
 
 
22
                #region DoIfNull
 
23
 
 
24
                /// <summary>
 
25
                /// Does something if the object isn't null.
 
26
                /// </summary>
 
27
                /// <typeparam name="TSource"></typeparam>
 
28
                /// <param name="source"></param>
 
29
                /// <param name="action"></param>
 
30
                /// <returns>Source object</returns>
 
31
                public static TSource DoIfNull<TSource>(this TSource source, Action action)
 
32
                {
 
33
                        Helper.CheckIfParameterIsNull(action, "action");
 
34
 
 
35
                        if (source == null)
 
36
                                action();
 
37
                        return source;
 
38
                }
 
39
 
 
40
                #endregion
 
41
 
 
42
                #region DoIfNotNull
 
43
 
 
44
                /// <summary>
 
45
                /// Does something if the object isn't null, otherwise the method returns null.
 
46
                /// </summary>
 
47
                /// <typeparam name="TSource"></typeparam>
 
48
                /// <typeparam name="TTarget"></typeparam>
 
49
                /// <param name="source"></param>
 
50
                /// <param name="action"></param>
 
51
                /// <returns></returns>
 
52
                public static TTarget DoIfNotNull<TSource, TTarget>(this TSource source, Func<TSource, TTarget> action)
 
53
                {
 
54
                        Helper.CheckIfParameterIsNull(action, "action");
 
55
 
 
56
                        return source == null ? default(TTarget) : action(source);
 
57
                }
 
58
 
 
59
                /// <summary>
 
60
                /// Does something if the object isn't null.
 
61
                /// </summary>
 
62
                /// <typeparam name="TSource"></typeparam>
 
63
                /// <param name="source"></param>
 
64
                /// <param name="action"></param>
 
65
                public static void DoIfNotNull<TSource>(this TSource source, Action<TSource> action)
 
66
                {
 
67
                        Helper.CheckIfParameterIsNull(action, "action");
 
68
 
 
69
                        if (source != null)
 
70
                                action(source);
 
71
                }
 
72
 
 
73
                #endregion
 
74
 
 
75
                #endregion
 
76
        }
 
77
}