Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Reflection;
  7.  
  8. namespace WinFormsDatabinding
  9. {
  10.   class ViewAnything : ICustomTypeDescriptor
  11.   {
  12.     /// <summary>
  13.     /// Sets a given exposed property to a given value.
  14.     /// </summary>
  15.     /// <typeparam name="T">The type of data to set the value to.</typeparam>
  16.     /// <param name="name">Name of the property</param>
  17.     /// <param name="value">Value to set.</param>
  18.     public void SetValue<T>(string name, T value)
  19.     {
  20.      
  21.       foreach(var desc in descriptors)
  22.       {
  23.         if(desc.Name == name)
  24.         {
  25.           desc.SetValue(this, value);
  26.           return;
  27.         }
  28.       }
  29.  
  30.       throw new InvalidOperationException(String.Format("No property matching \"{0}\"", name));
  31.     }
  32.  
  33.     /// <summary>
  34.     /// Gets a given property's value.
  35.     /// </summary>
  36.     /// <typeparam name="T">Type of data to get</typeparam>
  37.     /// <param name="name">Name of the property</param>
  38.     /// <param name="value">Value to write</param>
  39.     public void GetValue<T>(string name, out T value)
  40.     {
  41.       foreach(var desc in descriptors)
  42.       {
  43.         if(desc.Name == name)
  44.         {
  45.           value = (T)desc.GetValue(this) ;
  46.           return;
  47.         }
  48.       }
  49.       throw new InvalidOperationException(String.Format("No property matching \"{0}\"", name));
  50.     }
  51.  
  52.     #region boilerplate craps
  53.     public AttributeCollection GetAttributes()
  54.     {
  55.       return TypeDescriptor.GetAttributes(this, true);
  56.     }
  57.  
  58.         string ICustomTypeDescriptor.GetClassName()
  59.         {
  60.             return TypeDescriptor.GetClassName(this, true);
  61.         }
  62.  
  63.         string ICustomTypeDescriptor.GetComponentName()
  64.         {
  65.             return TypeDescriptor.GetComponentName(this, true);
  66.         }
  67.  
  68.         TypeConverter ICustomTypeDescriptor.GetConverter()
  69.         {
  70.             return TypeDescriptor.GetConverter(this, true);
  71.         }
  72.  
  73.         EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
  74.         {
  75.             return TypeDescriptor.GetDefaultEvent(this, true);
  76.         }
  77.  
  78.     public PropertyDescriptor GetDefaultProperty()
  79.     {
  80.       return null;
  81.     }
  82.  
  83.         object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
  84.         {
  85.             return TypeDescriptor.GetEditor(this, editorBaseType, true);
  86.         }
  87.  
  88.         EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
  89.         {
  90.             return TypeDescriptor.GetEvents(this, true);
  91.         }
  92.  
  93.         EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
  94.         {
  95.             return TypeDescriptor.GetEvents(this, attributes, true);
  96.         }
  97.  
  98.         PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
  99.         {
  100.             return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
  101.         }
  102.  
  103.     public object GetPropertyOwner(PropertyDescriptor pd)
  104.     {
  105.       return this;
  106.     }
  107.     #endregion
  108.  
  109.     List<CustomPropDescriptor> descriptors = new List<CustomPropDescriptor>();
  110.     public ViewAnything(object o)
  111.     {
  112.       Type paramType = o.GetType();
  113.  
  114.       // Scan for all public properties
  115.       for(var curType = paramType; curType != typeof(object); curType = curType.BaseType)
  116.       {
  117.         foreach(var property in curType.GetProperties(BindingFlags.Instance |BindingFlags.DeclaredOnly | BindingFlags.Public))
  118.         {
  119.           descriptors.Add(new CustomPropDescriptor(this, o, property));
  120.         }
  121.       }
  122.     }
  123.  
  124.     public PropertyDescriptorCollection GetProperties()
  125.     {
  126.       return GetProperties(new Attribute[0]);
  127.     }
  128.  
  129.         public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  130.         {
  131.             return new PropertyDescriptorCollection((PropertyDescriptor[])descriptors.ToArray());
  132.         }
  133.  
  134.     #region CustomPropDescriptor class
  135.    
  136.     class CustomPropDescriptor : PropertyDescriptor
  137.     {
  138.       PropertyInfo info;
  139.       object obj;
  140.       ViewAnything parent;
  141.  
  142.       MethodInfo getFunc;
  143.       MethodInfo setFunc;
  144.  
  145.       public CustomPropDescriptor(ViewAnything parent, object obj, PropertyInfo info) : base(info.Name, new Attribute[0])
  146.       {
  147.         this.parent = parent;
  148.         this.obj = obj;
  149.         this.info = info;
  150.         getFunc = info.GetGetMethod();
  151.         setFunc = info.GetSetMethod();
  152.       }
  153.  
  154.       public override bool CanResetValue(object component)
  155.       {
  156.         return false;
  157.       }
  158.  
  159.       public override void ResetValue(object component)
  160.       {
  161.       }
  162.  
  163.       public override bool ShouldSerializeValue(object component)
  164.       {
  165.         return false;
  166.       }
  167.       public override Type  ComponentType
  168.       {
  169.           get { return typeof(ViewAnything); }
  170.       }
  171.  
  172.       public override bool  IsReadOnly
  173.       {
  174.           get { return !info.CanWrite; }
  175.       }
  176.  
  177.       public override Type  PropertyType
  178.       {
  179.           get { return info.PropertyType; }
  180.       }
  181.  
  182.       public override object  GetValue(object component)
  183.       {
  184.           return getFunc.Invoke(obj, new object[0]);
  185.       }
  186.  
  187.       public override void  SetValue(object component, object value)
  188.       {
  189.           setFunc.Invoke(obj, new object[] { value });
  190.         //parent.OnPropertyChanged(info.Name);
  191.         OnValueChanged(component, new EventArgs());
  192.       }
  193.     }
  194.  
  195.     #endregion
  196.   }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement