annotate MetroWpf/MetroWpf.Framework/Extensions/ObjectExtensions.cs @ 89:3c67e54e3a17

Working version
author stevenhollidge <stevenhollidge@hotmail.com>
date Mon, 30 Apr 2012 10:45:32 +0100
parents 060f02cd4591
children
rev   line source
15
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
1 using System;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
2 using System.Collections.Generic;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
3 using System.ComponentModel;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
4
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
5 namespace MetroWpf
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
6 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
7 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
8 /// Extension methods for the root data type object
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
9 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
10 public static class ObjectExtensions
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
11 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
12 #region · Extensions ·
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
13
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
14 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
15 /// Determines whether the object is equal to any of the provided values.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
16 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
17 /// <typeparam name="T"></typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
18 /// <param name="obj">The object to be compared.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
19 /// <param name="values">The values to compare with the object.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
20 /// <returns></returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
21 public static bool EqualsAny<T>(this T obj, params T[] values)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
22 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
23 return (Array.IndexOf(values, obj) != -1);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
24 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
25
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
26 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
27 /// Determines whether the object is equal to none of the provided values.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
28 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
29 /// <typeparam name="T"></typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
30 /// <param name="obj">The object to be compared.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
31 /// <param name="values">The values to compare with the object.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
32 /// <returns></returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
33 public static bool EqualsNone<T>(this T obj, params T[] values)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
34 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
35 return (obj.EqualsAny(values) == false);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
36 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
37
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
38 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
39 /// Converts an object to the specified target type or returns the default value.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
40 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
41 /// <typeparam name="T"></typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
42 /// <param name="value">The value.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
43 /// <returns>The target type</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
44 public static T ConvertTo<T>(this object value)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
45 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
46 return value.ConvertTo(default(T));
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
47 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
48
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
49 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
50 /// Converts an object to the specified target type or returns the default value.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
51 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
52 /// <typeparam name="T"></typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
53 /// <param name="value">The value.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
54 /// <param name="defaultValue">The default value.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
55 /// <returns>The target type</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
56 public static T ConvertTo<T>(this object value, T defaultValue)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
57 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
58 if (value != null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
59 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
60 var targetType = typeof(T);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
61
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
62 var converter = TypeDescriptor.GetConverter(value);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
63
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
64 if (converter != null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
65 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
66 if (converter.CanConvertTo(targetType))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
67 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
68 return (T)converter.ConvertTo(value, targetType);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
69 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
70 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
71
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
72 converter = TypeDescriptor.GetConverter(targetType);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
73
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
74 if (converter != null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
75 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
76 if (converter.CanConvertFrom(value.GetType()))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
77 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
78 return (T)converter.ConvertFrom(value);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
79 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
80 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
81 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
82
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
83 return defaultValue;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
84 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
85
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
86 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
87 /// Converts an object to the specified target type or returns the default value. Any exceptions are optionally ignored.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
88 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
89 /// <typeparam name="T"></typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
90 /// <param name="value">The value.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
91 /// <param name="defaultValue">The default value.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
92 /// <param name="ignoreException">if set to <c>true</c> ignore any exception.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
93 /// <returns>The target type</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
94 public static T ConvertTo<T>(this object value, T defaultValue, bool ignoreException)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
95 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
96 if (ignoreException)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
97 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
98 try
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
99 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
100 return value.ConvertTo<T>();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
101 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
102 catch
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
103 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
104 return defaultValue;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
105 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
106 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
107
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
108 return value.ConvertTo<T>();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
109 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
110
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
111 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
112 /// Determines whether the value can (in theory) be converted to the specified target type.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
113 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
114 /// <typeparam name="T"></typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
115 /// <param name="value">The value.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
116 /// <returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
117 /// <c>true</c> if this instance can be convert to the specified target type; otherwise, <c>false</c>.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
118 /// </returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
119 public static bool CanConvertTo<T>(this object value)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
120 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
121 if (value != null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
122 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
123 var targetType = typeof(T);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
124
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
125 var converter = TypeDescriptor.GetConverter(value);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
126
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
127 if (converter != null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
128 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
129 if (converter.CanConvertTo(targetType))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
130 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
131 return true;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
132 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
133 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
134
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
135 converter = TypeDescriptor.GetConverter(targetType);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
136
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
137 if (converter != null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
138 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
139 if (converter.CanConvertFrom(value.GetType()))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
140 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
141 return true;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
142 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
143 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
144 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
145 return false;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
146 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
147
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
148 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
149 /// Converts the specified value to a different type.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
150 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
151 /// <typeparam name="T"></typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
152 /// <param name="value">The value.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
153 /// <returns>An universal converter suppliying additional target conversion methods</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
154 /// <example><code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
155 /// var value = "123";
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
156 /// var numeric = value.Convert().ToInt32();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
157 /// </code></example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
158 public static IConverter<T> Convert<T>(this T value)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
159 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
160 return new Converter<T>(value);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
161 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
162
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
163 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
164 /// Dynamically invokes a method using reflection
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
165 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
166 /// <param name="obj">The object to perform on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
167 /// <param name="methodName">The name of the method.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
168 /// <param name="parameters">The parameters passed to the method.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
169 /// <returns>The return value</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
170 /// <example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
171 /// <code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
172 /// var type = Type.GetType("System.IO.FileInfo, mscorlib");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
173 /// var file = type.CreateInstance(@"c:\autoexec.bat");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
174 /// if(file.GetPropertyValue&lt;bool&gt;("Exists")) {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
175 /// var reader = file.InvokeMethod&lt;StreamReader&gt;("OpenText");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
176 /// Console.WriteLine(reader.ReadToEnd());
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
177 /// reader.Close();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
178 /// }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
179 /// </code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
180 /// </example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
181 public static object InvokeMethod(this object obj, string methodName, params object[] parameters)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
182 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
183 return InvokeMethod<object>(obj, methodName, parameters);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
184 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
185
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
186 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
187 /// Dynamically invokes a method using reflection and returns its value in a typed manner
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
188 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
189 /// <typeparam name="T">The expected return data types</typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
190 /// <param name="obj">The object to perform on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
191 /// <param name="methodName">The name of the method.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
192 /// <param name="parameters">The parameters passed to the method.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
193 /// <returns>The return value</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
194 /// <example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
195 /// <code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
196 /// var type = Type.GetType("System.IO.FileInfo, mscorlib");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
197 /// var file = type.CreateInstance(@"c:\autoexec.bat");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
198 /// if(file.GetPropertyValue&lt;bool&gt;("Exists")) {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
199 /// var reader = file.InvokeMethod&lt;StreamReader&gt;("OpenText");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
200 /// Console.WriteLine(reader.ReadToEnd());
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
201 /// reader.Close();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
202 /// }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
203 /// </code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
204 /// </example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
205 public static T InvokeMethod<T>(this object obj, string methodName, params object[] parameters)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
206 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
207 var type = obj.GetType();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
208 var method = type.GetMethod(methodName);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
209
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
210 if (method == null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
211 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
212 throw new ArgumentException(string.Format("Method '{0}' not found.", methodName), methodName);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
213 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
214
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
215 var value = method.Invoke(obj, parameters);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
216
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
217 return (value is T ? (T)value : default(T));
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
218 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
219
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
220 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
221 /// Dynamically retrieves a property value.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
222 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
223 /// <param name="obj">The object to perform on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
224 /// <param name="propertyName">The Name of the property.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
225 /// <returns>The property value.</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
226 /// <example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
227 /// <code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
228 /// var type = Type.GetType("System.IO.FileInfo, mscorlib");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
229 /// var file = type.CreateInstance(@"c:\autoexec.bat");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
230 /// if(file.GetPropertyValue&lt;bool&gt;("Exists")) {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
231 /// var reader = file.InvokeMethod&lt;StreamReader&gt;("OpenText");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
232 /// Console.WriteLine(reader.ReadToEnd());
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
233 /// reader.Close();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
234 /// }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
235 /// </code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
236 /// </example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
237 public static object GetPropertyValue(this object obj, string propertyName)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
238 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
239 return GetPropertyValue<object>(obj, propertyName, null);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
240 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
241
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
242 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
243 /// Dynamically retrieves a property value.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
244 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
245 /// <typeparam name="T">The expected return data type</typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
246 /// <param name="obj">The object to perform on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
247 /// <param name="propertyName">The Name of the property.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
248 /// <returns>The property value.</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
249 /// <example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
250 /// <code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
251 /// var type = Type.GetType("System.IO.FileInfo, mscorlib");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
252 /// var file = type.CreateInstance(@"c:\autoexec.bat");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
253 /// if(file.GetPropertyValue&lt;bool&gt;("Exists")) {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
254 /// var reader = file.InvokeMethod&lt;StreamReader&gt;("OpenText");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
255 /// Console.WriteLine(reader.ReadToEnd());
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
256 /// reader.Close();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
257 /// }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
258 /// </code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
259 /// </example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
260 public static T GetPropertyValue<T>(this object obj, string propertyName)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
261 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
262 return GetPropertyValue<T>(obj, propertyName, default(T));
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
263 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
264
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
265 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
266 /// Dynamically retrieves a property value.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
267 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
268 /// <typeparam name="T">The expected return data type</typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
269 /// <param name="obj">The object to perform on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
270 /// <param name="propertyName">The Name of the property.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
271 /// <param name="defaultValue">The default value to return.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
272 /// <returns>The property value.</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
273 /// <example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
274 /// <code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
275 /// var type = Type.GetType("System.IO.FileInfo, mscorlib");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
276 /// var file = type.CreateInstance(@"c:\autoexec.bat");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
277 /// if(file.GetPropertyValue&lt;bool&gt;("Exists")) {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
278 /// var reader = file.InvokeMethod&lt;StreamReader&gt;("OpenText");
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
279 /// Console.WriteLine(reader.ReadToEnd());
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
280 /// reader.Close();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
281 /// }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
282 /// </code>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
283 /// </example>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
284 public static T GetPropertyValue<T>(this object obj, string propertyName, T defaultValue)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
285 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
286 var type = obj.GetType();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
287 var property = type.GetProperty(propertyName);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
288
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
289 if (property == null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
290 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
291 throw new ArgumentException(string.Format("Property '{0}' not found.", propertyName), propertyName);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
292 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
293
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
294 var value = property.GetValue(obj, null);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
295
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
296 return (value is T ? (T)value : defaultValue);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
297 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
298
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
299 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
300 /// Dynamically sets a property value.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
301 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
302 /// <param name="obj">The object to perform on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
303 /// <param name="propertyName">The Name of the property.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
304 /// <param name="value">The value to be set.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
305 public static void SetPropertyValue(this object obj, string propertyName, object value)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
306 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
307 var type = obj.GetType();
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
308 var property = type.GetProperty(propertyName);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
309
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
310 if (property == null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
311 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
312 throw new ArgumentException(string.Format("Property '{0}' not found.", propertyName), propertyName);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
313 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
314
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
315 property.SetValue(obj, value, null);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
316 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
317
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
318 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
319 /// Gets the first matching attribute defined on the data type.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
320 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
321 /// <typeparam name="T">The attribute type tp look for.</typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
322 /// <param name="obj">The object to look on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
323 /// <returns>The found attribute</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
324 public static T GetAttribute<T>(this object obj) where T : Attribute
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
325 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
326 return GetAttribute<T>(obj, true);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
327 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
328
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
329 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
330 /// Gets the first matching attribute defined on the data type.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
331 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
332 /// <typeparam name="T">The attribute type tp look for.</typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
333 /// <param name="obj">The object to look on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
334 /// <param name="includeInherited">if set to <c>true</c> includes inherited attributes.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
335 /// <returns>The found attribute</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
336 public static T GetAttribute<T>(this object obj, bool includeInherited) where T : Attribute
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
337 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
338 var type = (obj as Type ?? obj.GetType());
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
339 var attributes = type.GetCustomAttributes(typeof(T), includeInherited);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
340
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
341 if ((attributes != null) && (attributes.Length > 0))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
342 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
343 return (attributes[0] as T);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
344 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
345
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
346 return null;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
347 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
348
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
349 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
350 /// Gets all matching attribute defined on the data type.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
351 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
352 /// <typeparam name="T">The attribute type tp look for.</typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
353 /// <param name="obj">The object to look on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
354 /// <returns>The found attributes</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
355 public static IEnumerable<T> GetAttributes<T>(this object obj) where T : Attribute
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
356 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
357 return GetAttributes<T>(obj);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
358 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
359
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
360 /// <summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
361 /// Gets all matching attribute defined on the data type.
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
362 /// </summary>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
363 /// <typeparam name="T">The attribute type tp look for.</typeparam>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
364 /// <param name="obj">The object to look on.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
365 /// <param name="includeInherited">if set to <c>true</c> includes inherited attributes.</param>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
366 /// <returns>The found attributes</returns>
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
367 public static IEnumerable<T> GetAttributes<T>(this object obj, bool includeInherited) where T : Attribute
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
368 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
369 var type = (obj as Type ?? obj.GetType());
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
370
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
371 foreach (var attribute in type.GetCustomAttributes(typeof(T), includeInherited))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
372 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
373 if (attribute is T)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
374 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
375 yield return (T)attribute;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
376 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
377 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
378 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
379
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
380 #endregion
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
381 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
382 }