Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ComponentModel;
- using System.Reflection;
- namespace WinFormsDatabinding
- {
- class ViewAnything : ICustomTypeDescriptor
- {
- /// <summary>
- /// Sets a given exposed property to a given value.
- /// </summary>
- /// <typeparam name="T">The type of data to set the value to.</typeparam>
- /// <param name="name">Name of the property</param>
- /// <param name="value">Value to set.</param>
- public void SetValue<T>(string name, T value)
- {
- foreach(var desc in descriptors)
- {
- if(desc.Name == name)
- {
- desc.SetValue(this, value);
- return;
- }
- }
- throw new InvalidOperationException(String.Format("No property matching \"{0}\"", name));
- }
- /// <summary>
- /// Gets a given property's value.
- /// </summary>
- /// <typeparam name="T">Type of data to get</typeparam>
- /// <param name="name">Name of the property</param>
- /// <param name="value">Value to write</param>
- public void GetValue<T>(string name, out T value)
- {
- foreach(var desc in descriptors)
- {
- if(desc.Name == name)
- {
- value = (T)desc.GetValue(this) ;
- return;
- }
- }
- throw new InvalidOperationException(String.Format("No property matching \"{0}\"", name));
- }
- #region boilerplate craps
- public AttributeCollection GetAttributes()
- {
- return TypeDescriptor.GetAttributes(this, true);
- }
- string ICustomTypeDescriptor.GetClassName()
- {
- return TypeDescriptor.GetClassName(this, true);
- }
- string ICustomTypeDescriptor.GetComponentName()
- {
- return TypeDescriptor.GetComponentName(this, true);
- }
- TypeConverter ICustomTypeDescriptor.GetConverter()
- {
- return TypeDescriptor.GetConverter(this, true);
- }
- EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
- {
- return TypeDescriptor.GetDefaultEvent(this, true);
- }
- public PropertyDescriptor GetDefaultProperty()
- {
- return null;
- }
- object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
- {
- return TypeDescriptor.GetEditor(this, editorBaseType, true);
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
- {
- return TypeDescriptor.GetEvents(this, true);
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
- {
- return TypeDescriptor.GetEvents(this, attributes, true);
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
- {
- return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
- }
- public object GetPropertyOwner(PropertyDescriptor pd)
- {
- return this;
- }
- #endregion
- List<CustomPropDescriptor> descriptors = new List<CustomPropDescriptor>();
- public ViewAnything(object o)
- {
- Type paramType = o.GetType();
- // Scan for all public properties
- for(var curType = paramType; curType != typeof(object); curType = curType.BaseType)
- {
- foreach(var property in curType.GetProperties(BindingFlags.Instance |BindingFlags.DeclaredOnly | BindingFlags.Public))
- {
- descriptors.Add(new CustomPropDescriptor(this, o, property));
- }
- }
- }
- public PropertyDescriptorCollection GetProperties()
- {
- return GetProperties(new Attribute[0]);
- }
- public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
- {
- return new PropertyDescriptorCollection((PropertyDescriptor[])descriptors.ToArray());
- }
- #region CustomPropDescriptor class
- class CustomPropDescriptor : PropertyDescriptor
- {
- PropertyInfo info;
- object obj;
- ViewAnything parent;
- MethodInfo getFunc;
- MethodInfo setFunc;
- public CustomPropDescriptor(ViewAnything parent, object obj, PropertyInfo info) : base(info.Name, new Attribute[0])
- {
- this.parent = parent;
- this.obj = obj;
- this.info = info;
- getFunc = info.GetGetMethod();
- setFunc = info.GetSetMethod();
- }
- public override bool CanResetValue(object component)
- {
- return false;
- }
- public override void ResetValue(object component)
- {
- }
- public override bool ShouldSerializeValue(object component)
- {
- return false;
- }
- public override Type ComponentType
- {
- get { return typeof(ViewAnything); }
- }
- public override bool IsReadOnly
- {
- get { return !info.CanWrite; }
- }
- public override Type PropertyType
- {
- get { return info.PropertyType; }
- }
- public override object GetValue(object component)
- {
- return getFunc.Invoke(obj, new object[0]);
- }
- public override void SetValue(object component, object value)
- {
- setFunc.Invoke(obj, new object[] { value });
- //parent.OnPropertyChanged(info.Name);
- OnValueChanged(component, new EventArgs());
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement