forked from Artfunkel/Datamodel.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAttributeList_ICustomTypeDescriptor.cs
More file actions
125 lines (103 loc) · 3.62 KB
/
AttributeList_ICustomTypeDescriptor.cs
File metadata and controls
125 lines (103 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Datamodel
{
public partial class AttributeList
{
#region Boring pass-throughs
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return new AttributeCollection();
}
string ICustomTypeDescriptor.GetClassName()
{
return null;
}
string ICustomTypeDescriptor.GetComponentName()
{
return null;
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return null;
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return null;
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return null;
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return null;
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return new EventDescriptorCollection(null);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(System.Attribute[] attributes)
{
return new EventDescriptorCollection(null);
}
public PropertyDescriptorCollection GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties(null);
}
#endregion
class AttributePropertyDescriptor : PropertyDescriptor
{
private Type AttributeType;
public AttributePropertyDescriptor(string key, Type type) : base(key, null)
{
AttributeType = type;
}
public override Type ComponentType { get { return typeof(AttributeList); } }
public override bool IsReadOnly { get { return false; } }
public override Type PropertyType { get { return AttributeType; } }
public override bool CanResetValue(object component)
{
return true;
}
public override object GetValue(object component)
{
return ((AttributeList)component)[Name];
}
public override void ResetValue(object component)
{
((AttributeList)component)[Name] = AttributeType.IsValueType ? Activator.CreateInstance(AttributeType) : null;
}
public override void SetValue(object component, object value)
{
((AttributeList)component)[Name] = value;
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}
public PropertyDescriptorCollection GetProperties(System.Attribute[] attributes)
{
var properties = TypeDescriptor.GetProperties(GetType(), attributes).Cast<PropertyDescriptor>().ToList();
var reserved_names = new HashSet<string>(properties.Select(pd => pd.Name));
foreach (Attribute attr in Inner.Values)
{
if (reserved_names.Contains(attr.Name))
continue; // you'll have to use the "this[key]" binding syntax for these.
properties.Add(new AttributePropertyDescriptor(attr.Name, attr.ValueType));
}
return new PropertyDescriptorCollection(properties.ToArray());
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
}
}