|
1 | 1 | using System; |
2 | 2 | using System.Collections; |
| 3 | +using System.Collections.Concurrent; |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.Globalization; |
5 | 6 | using System.Linq; |
| 7 | +using System.Reflection; |
6 | 8 | using System.Text; |
7 | 9 | using System.Threading.Tasks; |
8 | 10 |
|
@@ -100,11 +102,11 @@ private IEnumerable<KeyValuePair<string, string>> FormatAsComplex(object instanc |
100 | 102 | } |
101 | 103 |
|
102 | 104 | return |
103 | | - from p in KeyValueProperty.GetProperties(instance.GetType()) |
| 105 | + from p in PropertyDescriptor.GetProperties(instance.GetType()) |
104 | 106 | where p.IsSupportGet && p.IgnoreSerialized == false |
105 | 107 | let value = p.GetValue(instance) |
106 | 108 | let opt = options.CloneChange(p.DateTimeFormat) |
107 | | - select this.FormatAsSimple(p.Name, value, opt); |
| 109 | + select this.FormatAsSimple(p.AliasName, value, opt); |
108 | 110 | } |
109 | 111 |
|
110 | 112 | /// <summary> |
@@ -157,5 +159,212 @@ private KeyValuePair<string, string> FormatAsSimple(string name, object value, F |
157 | 159 | var dateTime = ((DateTime)value).ToString(options.DateTimeFormat, DateTimeFormatInfo.InvariantInfo); |
158 | 160 | return new KeyValuePair<string, string>(name, dateTime); |
159 | 161 | } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// 表示类型描述 |
| 165 | + /// </summary> |
| 166 | + private class TypeDescriptor |
| 167 | + { |
| 168 | + /// <summary> |
| 169 | + /// 描述缓存 |
| 170 | + /// </summary> |
| 171 | + private static readonly ConcurrentDictionary<Type, TypeDescriptor> descriptorCache; |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// 获取类型是否为简单类型 |
| 175 | + /// </summary> |
| 176 | + public bool IsSimpleType { get; private set; } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// 获取类型是否为可列举类型 |
| 180 | + /// </summary> |
| 181 | + public bool IsEnumerable { get; private set; } |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// 获取类型是否为IDictionaryOf(string,object) |
| 185 | + /// </summary> |
| 186 | + public bool IsDictionaryOfObject { get; private set; } |
| 187 | + |
| 188 | + /// <summary> |
| 189 | + /// 获取类型是否为IDictionaryOf(string,string) |
| 190 | + /// </summary> |
| 191 | + public bool IsDictionaryOfString { get; private set; } |
| 192 | + |
| 193 | + /// <summary> |
| 194 | + /// 类型描述 |
| 195 | + /// </summary> |
| 196 | + /// <param name="type">类型</param> |
| 197 | + private TypeDescriptor(Type type) |
| 198 | + { |
| 199 | + this.IsSimpleType = type.IsSimple(); |
| 200 | + this.IsEnumerable = type.IsInheritFrom<IEnumerable>(); |
| 201 | + this.IsDictionaryOfObject = type.IsInheritFrom<IDictionary<string, object>>(); |
| 202 | + this.IsDictionaryOfString = type.IsInheritFrom<IDictionary<string, string>>(); |
| 203 | + } |
| 204 | + |
| 205 | + /// <summary> |
| 206 | + /// 静态构造器 |
| 207 | + /// </summary> |
| 208 | + static TypeDescriptor() |
| 209 | + { |
| 210 | + descriptorCache = new ConcurrentDictionary<Type, TypeDescriptor>(); |
| 211 | + } |
| 212 | + |
| 213 | + /// <summary> |
| 214 | + /// 获取类型的描述 |
| 215 | + /// </summary> |
| 216 | + /// <param name="type">类型</param> |
| 217 | + /// <returns></returns> |
| 218 | + public static TypeDescriptor GetDescriptor(Type type) |
| 219 | + { |
| 220 | + if (type == null) |
| 221 | + { |
| 222 | + return null; |
| 223 | + } |
| 224 | + return descriptorCache.GetOrAdd(type, (t) => new TypeDescriptor(t)); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + /// <summary> |
| 229 | + /// 表示属性描述 |
| 230 | + /// </summary> |
| 231 | + private class PropertyDescriptor |
| 232 | + { |
| 233 | + /// <summary> |
| 234 | + /// 获取器 |
| 235 | + /// </summary> |
| 236 | + private readonly Getter getter; |
| 237 | + |
| 238 | + /// <summary> |
| 239 | + /// 获取属性别名或名称 |
| 240 | + /// </summary> |
| 241 | + public string AliasName { get; private set; } |
| 242 | + |
| 243 | + /// <summary> |
| 244 | + /// 获取声明的DateTimeFormatAttribute的Format |
| 245 | + /// </summary> |
| 246 | + public string DateTimeFormat { get; private set; } |
| 247 | + |
| 248 | + /// <summary> |
| 249 | + /// 获取是否声明IgnoreSerializedAttribute |
| 250 | + /// </summary> |
| 251 | + public bool IgnoreSerialized { get; private set; } |
| 252 | + |
| 253 | + /// <summary> |
| 254 | + /// 获取是否支持Get操作 |
| 255 | + /// </summary> |
| 256 | + public bool IsSupportGet { get; private set; } |
| 257 | + |
| 258 | + /// <summary> |
| 259 | + /// 属性 |
| 260 | + /// </summary> |
| 261 | + /// <param name="property">属性信息</param> |
| 262 | + private PropertyDescriptor(PropertyInfo property) |
| 263 | + { |
| 264 | + var aliasAs = property.GetAttribute<AliasAsAttribute>(true); |
| 265 | + this.AliasName = aliasAs == null ? property.Name : aliasAs.Name; |
| 266 | + |
| 267 | + if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?)) |
| 268 | + { |
| 269 | + var formatAttribute = property.GetAttribute<DateTimeFormatAttribute>(true); |
| 270 | + this.DateTimeFormat = formatAttribute == null ? null : formatAttribute.Format; |
| 271 | + } |
| 272 | + |
| 273 | + if (property.CanRead == true) |
| 274 | + { |
| 275 | + this.getter = Getter.Create(property); |
| 276 | + } |
| 277 | + |
| 278 | + this.IgnoreSerialized = property.IsDefined(typeof(IgnoreSerializedAttribute)); |
| 279 | + this.IsSupportGet = this.getter != null; |
| 280 | + } |
| 281 | + |
| 282 | + /// <summary> |
| 283 | + /// 获取属性的值 |
| 284 | + /// </summary> |
| 285 | + /// <param name="instance">实例</param> |
| 286 | + /// <exception cref="NotSupportedException"></exception> |
| 287 | + /// <returns></returns> |
| 288 | + public object GetValue(object instance) |
| 289 | + { |
| 290 | + if (this.IsSupportGet == false) |
| 291 | + { |
| 292 | + throw new NotSupportedException(); |
| 293 | + } |
| 294 | + return this.getter.Invoke(instance); |
| 295 | + } |
| 296 | + |
| 297 | + /// <summary> |
| 298 | + /// 类型的属性描述缓存 |
| 299 | + /// </summary> |
| 300 | + private static readonly ConcurrentDictionary<Type, PropertyDescriptor[]> propertyCached = new ConcurrentDictionary<Type, PropertyDescriptor[]>(); |
| 301 | + |
| 302 | + /// <summary> |
| 303 | + /// 从类型的属性获取属性 |
| 304 | + /// </summary> |
| 305 | + /// <param name="classType">类型</param> |
| 306 | + /// <returns></returns> |
| 307 | + public static PropertyDescriptor[] GetProperties(Type classType) |
| 308 | + { |
| 309 | + return propertyCached.GetOrAdd(classType, t => t.GetProperties().Select(p => new PropertyDescriptor(p)).ToArray()); |
| 310 | + } |
| 311 | + |
| 312 | + /// <summary> |
| 313 | + /// 表示属性的Get方法抽象类 |
| 314 | + /// </summary> |
| 315 | + private abstract class Getter |
| 316 | + { |
| 317 | + /// <summary> |
| 318 | + /// 创建属性的Get方法 |
| 319 | + /// </summary> |
| 320 | + /// <param name="property">属性</param> |
| 321 | + /// <returns></returns> |
| 322 | + public static Getter Create(PropertyInfo property) |
| 323 | + { |
| 324 | + var getterType = typeof(GenericGetter<,>).MakeGenericType(property.DeclaringType, property.PropertyType); |
| 325 | + return Activator.CreateInstance(getterType, property) as Getter; |
| 326 | + } |
| 327 | + |
| 328 | + /// <summary> |
| 329 | + /// 执行Get方法 |
| 330 | + /// </summary> |
| 331 | + /// <param name="instance">实例</param> |
| 332 | + /// <returns></returns> |
| 333 | + public abstract object Invoke(object instance); |
| 334 | + |
| 335 | + /// <summary> |
| 336 | + /// 表示属性的Get方法 |
| 337 | + /// </summary> |
| 338 | + /// <typeparam name="TTarget">属性所在的类</typeparam> |
| 339 | + /// <typeparam name="TResult">属性的返回值</typeparam> |
| 340 | + private class GenericGetter<TTarget, TResult> : Getter |
| 341 | + { |
| 342 | + /// <summary> |
| 343 | + /// get方法的委托 |
| 344 | + /// </summary> |
| 345 | + private readonly Func<TTarget, TResult> getFunc; |
| 346 | + |
| 347 | + /// <summary> |
| 348 | + /// 属性的Get方法 |
| 349 | + /// </summary> |
| 350 | + /// <param name="property">属性</param> |
| 351 | + public GenericGetter(PropertyInfo property) |
| 352 | + { |
| 353 | + var getMethod = property.GetGetMethod(); |
| 354 | + this.getFunc = (Func<TTarget, TResult>)getMethod.CreateDelegate(typeof(Func<TTarget, TResult>), null); |
| 355 | + } |
| 356 | + |
| 357 | + /// <summary> |
| 358 | + /// 执行Get方法 |
| 359 | + /// </summary> |
| 360 | + /// <param name="instance">实例</param> |
| 361 | + /// <returns></returns> |
| 362 | + public override object Invoke(object instance) |
| 363 | + { |
| 364 | + return this.getFunc.Invoke((TTarget)instance); |
| 365 | + } |
| 366 | + } |
| 367 | + } |
| 368 | + } |
160 | 369 | } |
161 | 370 | } |
0 commit comments