Jypeli 10
The simple game programming library
AdvPropertyDescriptor.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26using System;
27using System.ComponentModel;
28using System.Reflection;
29
30namespace AdvanceMath.Design
31{
32#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
33 public class AdvPropertyDescriptor : PropertyDescriptor, ICustomTypeDescriptor, IEquatable<AdvPropertyDescriptor>
34 {
35 private static Attribute[] GetAttributes(MemberInfo property)
36 {
37 if (property == null) { throw new ArgumentNullException("property"); }
38 return (Attribute[])property.GetCustomAttributes(typeof(Attribute), true);
39 }
40 MemberInfo info;
41 FieldInfo field;
42 PropertyInfo property;
44 public AdvPropertyDescriptor(FieldInfo field)
45 : this(field.Name, field)
46 { }
47 public AdvPropertyDescriptor(string name, FieldInfo field)
48 : base(name, GetAttributes(field))
49 {
50 this.info = field;
51 this.field = field;
52 this.description = base.Description;
53 }
54 public AdvPropertyDescriptor(PropertyInfo property)
55 : this(property.Name, property)
56 { }
57 public AdvPropertyDescriptor(string name, PropertyInfo property)
58 : base(name, GetAttributes(property))
59 {
60 this.info = property;
61 this.property = property;
62 this.description = base.Description;
63 }
64 public override Type ComponentType
65 {
66 get { return info.DeclaringType; }
67 }
68 public override object GetValue(object component)
69 {
70 if (component is PropertyDescriptor)
71 {
72 Object result = ((PropertyDescriptor)component).GetValue(null);
73 if (result.GetType() != ComponentType) { return result; }
74 }
75 if (field == null)
76 {
77 return property.GetValue(component, null);
78 }
79 return field.GetValue(component);
80 }
81 public override bool IsReadOnly
82 {
83 get { return property != null && !property.CanWrite; }
84 }
85 public override Type PropertyType
86 {
87 get
88 {
89 if (field == null)
90 {
91 return property.PropertyType;
92 }
93 return field.FieldType;
94 }
95 }
96 public override bool CanResetValue(object component) { return false; }
97 public override void ResetValue(object component)
98 {
99 throw new NotSupportedException();
100 }
101 public override void SetValue(object component, object value)
102 {
103 if (field == null)
104 {
105 property.SetValue(component, value, null);
106 }
107 else
108 {
109 field.SetValue(component, value);
110 }
111 this.OnValueChanged(component, EventArgs.Empty);
112 }
113 public override bool ShouldSerializeValue(object component)
114 {
115 return true;
116 }
117 public override int GetHashCode()
118 {
119 return info.GetHashCode();
120 }
121 public override string Description
122 {
123 get
124 {
125 return description;
126 }
127 }
128 public void SetDescription(string value)
129 {
130 description = value;
131 }
132 public override bool Equals(object obj)
133 {
135 }
136 public bool Equals(AdvPropertyDescriptor other)
137 {
138 return info.Equals(other.info);
139 }
140
141
142 #region ICustomTypeDescriptor Members
143 AttributeCollection ICustomTypeDescriptor.GetAttributes()
144 {
145 return TypeDescriptor.GetAttributes(ComponentType);
146 }
147 string ICustomTypeDescriptor.GetClassName()
148 {
149 return ComponentType.Name;
150 }
151 string ICustomTypeDescriptor.GetComponentName()
152 {
153 return null;
154 }
155 TypeConverter ICustomTypeDescriptor.GetConverter()
156 {
157 return null;
158 }
159 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
160 {
161 return null;
162 }
163 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
164 {
165 return this;
166 }
167 object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
168 {
169 return null;
170 }
171
172 EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
173 {
174 return EventDescriptorCollection.Empty;
175 }
176 EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
177 {
178 return EventDescriptorCollection.Empty;
179 }
180
181 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
182 {
183 return TypeDescriptor.GetConverter(ComponentType).GetProperties(null);
184 }
185 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
186 {
187 return TypeDescriptor.GetConverter(ComponentType).GetProperties(null, null, attributes);
188 }
189 object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
190 {
191 return this;
192 }
193 #endregion
194 }
195#endif
196}
AttributeCollection ICustomTypeDescriptor. GetAttributes()
static Attribute[] GetAttributes(MemberInfo property)
AdvPropertyDescriptor(string name, PropertyInfo property)
AdvPropertyDescriptor(string name, FieldInfo field)
bool Equals(AdvPropertyDescriptor other)
override void SetValue(object component, object value)
override bool CanResetValue(object component)
override object GetValue(object component)
override void ResetValue(object component)
override bool ShouldSerializeValue(object component)