프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Extensions / CollectionExtenstions.cs @ 0b75c341

이력 | 보기 | 이력해설 | 다운로드 (8.42 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Linq.Expressions;
5
using System.Text;
6
using System.Threading.Tasks;
7

    
8
namespace KCOM
9
{
10
    public static class CollectionExtensions
11
    {
12
        //public static void Update<T>(this IList<T> source,IList<T> Target,IList<string> Keys)
13
        //{
14
        //    for (int i = source.Count; i == 0 ; --i)
15
        //    {
16
        //        var item = source[i];
17
        //         Target.Where(f=>)
18
        //        item.GetType().GetProperty()
19
        //    }
20
        //}
21

    
22
        public static T ChangeValues<T>(T item, T newitem)
23
        {
24
            var changeProp = item.GetType().GetProperties();
25
            var newProp = newitem.GetType().GetProperties();
26

    
27
            foreach (var Prop in changeProp)
28
            {
29

    
30
                var newinfo = newProp.Where(x => x.Name == Prop.Name);
31

    
32
                if (newinfo.Count() > 0)
33
                {
34
                    Prop.SetValue(item, newinfo.First().GetValue(newitem));
35
                }
36
            }
37

    
38
            return item;
39
        }
40

    
41
        /// <summary>
42
        /// Changes all elements of IEnumerable by the change function
43
        /// </summary>
44
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
45
        /// <param name="change">The way you want to change the stuff</param>
46
        /// <returns>An IEnumerable with all changes applied</returns>
47
        public static IEnumerable<T> Change<T>(this IEnumerable<T> enumerable, Func<T, T> change)
48
        {
49
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
50
            ArgumentCheck.IsNullorWhiteSpace(change, "change");
51

    
52
            foreach (var item in enumerable)
53
            {
54
                yield return change(item);
55
            }
56
        }
57

    
58
        /// <summary>
59
        /// Changes all elements of IEnumerable by the change function, that fullfill the where function
60
        /// </summary>
61
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
62
        /// <param name="change">The way you want to change the stuff</param>
63
        /// <param name="where">The function to check where changes should be made</param>
64
        /// <returns>
65
        /// An IEnumerable with all changes applied
66
        /// </returns>
67
        public static IEnumerable<T> ChangeWhere<T>(this IEnumerable<T> enumerable,
68
                                                    Func<T, T> change,
69
                                                    Func<T, bool> @where)
70
        {
71
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
72
            ArgumentCheck.IsNullorWhiteSpace(change, "change");
73
            ArgumentCheck.IsNullorWhiteSpace(@where, "where");
74

    
75
            foreach (var item in enumerable)
76
            {
77
                if (@where(item))
78
                {
79
                    yield return change(item);
80
                }
81
                else
82
                {
83
                    yield return item;
84
                }
85
            }
86
        }
87

    
88
        public static class ArgumentCheck
89
        {
90

    
91

    
92
            /// <summary>
93
            /// Checks if a value is string or any other object if it is string
94
            /// it checks for nullorwhitespace otherwhise it checks for null only
95
            /// </summary>
96
            /// <typeparam name="T">Type of the item you want to check</typeparam>
97
            /// <param name="item">The item you want to check</param>
98
            /// <param name="nameOfTheArgument">Name of the argument</param>
99
            public static void IsNullorWhiteSpace<T>(T item, string nameOfTheArgument = "")
100
            {
101

    
102
                Type type = typeof(T);
103
                if (type == typeof(string) ||
104
                    type == typeof(String))
105
                {
106
                    if (string.IsNullOrWhiteSpace(item as string))
107
                    {
108
                        throw new ArgumentException(nameOfTheArgument + " is null or Whitespace");
109
                    }
110
                }
111
                else
112
                {
113
                    if (item == null)
114
                    {
115
                        throw new ArgumentException(nameOfTheArgument + " is null");
116
                    }
117
                }
118

    
119
            }
120
        }
121

    
122
        /// <summary>
123
        /// Changes all elements of IEnumerable by the change function that do not fullfill the except function
124
        /// </summary>
125
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
126
        /// <param name="change">The way you want to change the stuff</param>
127
        /// <param name="where">The function to check where changes should not be made</param>
128
        /// <returns>
129
        /// An IEnumerable with all changes applied
130
        /// </returns>
131
        public static IEnumerable<T> ChangeExcept<T>(this IEnumerable<T> enumerable,
132
                                                     Func<T, T> change,
133
                                                     Func<T, bool> @where)
134
        {
135
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
136
            ArgumentCheck.IsNullorWhiteSpace(change, "change");
137
            ArgumentCheck.IsNullorWhiteSpace(@where, "where");
138

    
139
            foreach (var item in enumerable)
140
            {
141
                if (!@where(item))
142
                {
143
                    yield return change(item);
144
                }
145
                else
146
                {
147
                    yield return item;
148
                }
149
            }
150
        }
151

    
152
        /// <summary>
153
        /// Update all elements of IEnumerable by the update function (only works with reference types)
154
        /// </summary>
155
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
156
        /// <param name="update">The way you want to change the stuff</param>
157
        /// <returns>
158
        /// The same enumerable you passed in
159
        /// </returns>
160
        public static IEnumerable<T> Update<T>(this IEnumerable<T> enumerable,
161
                                               Action<T> update) where T : class
162
        {
163
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
164
            ArgumentCheck.IsNullorWhiteSpace(update, "update");
165
            foreach (var item in enumerable)
166
            {
167
                update(item);
168
            }
169
            return enumerable;
170
        }
171

    
172
        /// <summary>
173
        /// Update all elements of IEnumerable by the update function (only works with reference types)
174
        /// where the where function returns true
175
        /// </summary>
176
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
177
        /// <param name="update">The way you want to change the stuff</param>
178
        /// <param name="where">The function to check where updates should be made</param>
179
        /// <returns>
180
        /// The same enumerable you passed in
181
        /// </returns>
182
        public static IEnumerable<T> UpdateWhere<T>(this IEnumerable<T> enumerable,
183
                                               Action<T> update, Func<T, bool> where) where T : class
184
        {
185
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
186
            ArgumentCheck.IsNullorWhiteSpace(update, "update");
187
            foreach (var item in enumerable)
188
            {
189
                if (where(item))
190
                {
191
                    update(item);
192
                }
193
            }
194
            return enumerable;
195
        }
196

    
197
        /// <summary>
198
        /// Update all elements of IEnumerable by the update function (only works with reference types)
199
        /// Except the elements from the where function
200
        /// </summary>
201
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
202
        /// <param name="update">The way you want to change the stuff</param>
203
        /// <param name="where">The function to check where changes should not be made</param>
204
        /// <returns>
205
        /// The same enumerable you passed in
206
        /// </returns>
207
        public static IEnumerable<T> UpdateExcept<T>(this IEnumerable<T> enumerable,
208
                                               Action<T> update, Func<T, bool> where) where T : class
209
        {
210
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
211
            ArgumentCheck.IsNullorWhiteSpace(update, "update");
212

    
213
            foreach (var item in enumerable)
214
            {
215
                if (!where(item))
216
                {
217
                    update(item);
218
                }
219
            }
220
            return enumerable;
221
        }
222
    }
223
}
클립보드 이미지 추가 (최대 크기: 500 MB)