comparison Source/ComponentModel/MemberPropertyDescriptor.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.ComponentModel;
3
4 using BLToolkit.Reflection;
5
6 namespace BLToolkit.ComponentModel
7 {
8 public class MemberPropertyDescriptor : PropertyDescriptor
9 {
10 public MemberPropertyDescriptor(Type componentType, string memberName)
11 : base(memberName, null)
12 {
13 _componentType = componentType;
14 _memberAccessor = TypeAccessor.GetAccessor(componentType)[memberName];
15 }
16
17 private readonly Type _componentType;
18 public override Type ComponentType
19 {
20 get { return _componentType; }
21 }
22
23 public override Type PropertyType
24 {
25 get { return _memberAccessor.Type; }
26 }
27
28 private readonly MemberAccessor _memberAccessor;
29 public MemberAccessor MemberAccessor
30 {
31 get { return _memberAccessor; }
32 }
33
34 public override bool CanResetValue(object component)
35 {
36 if (PropertyType.IsValueType)
37 return TypeAccessor.GetNullValue(PropertyType) != null;
38 return PropertyType == typeof(string);
39 }
40
41 public override void ResetValue(object component)
42 {
43 SetValue(component, TypeAccessor.GetNullValue(PropertyType));
44 }
45
46 public override object GetValue(object component)
47 {
48 return component != null? _memberAccessor.GetValue(component): null;
49 }
50
51 public override void SetValue(object component, object value)
52 {
53 if (component != null)
54 _memberAccessor.SetValue(component, value);
55 }
56
57 public override bool IsReadOnly
58 {
59 get { return !_memberAccessor.HasSetter; }
60 }
61
62 public override bool ShouldSerializeValue(object component)
63 {
64 return false;
65 }
66
67 private AttributeCollection _attributes;
68 public override AttributeCollection Attributes
69 {
70 get
71 {
72 if (_attributes == null)
73 {
74 object[] memberAttrs = _memberAccessor.GetAttributes();
75 Attribute[] attrs = new Attribute[memberAttrs == null? 0: memberAttrs.Length];
76
77 if (memberAttrs != null)
78 memberAttrs.CopyTo(attrs, 0);
79
80 _attributes = new AttributeCollection(attrs);
81 }
82
83 return _attributes;
84 }
85 }
86 }
87 }