2006-11-29 Ivan N. Zlatev <contact@i-nz.net>
authorMiguel de Icaza <miguel@gnome.org>
Thu, 30 Nov 2006 03:52:42 +0000 (03:52 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Thu, 30 Nov 2006 03:52:42 +0000 (03:52 -0000)
* MemberDescriptor.cs, ReflectionPropertyDescriptor.cs: 1) The
ReflectionPropretyDescriptor must be able to operate with non
public properties.

2) The current implementation ignores the fact that the component
can be in design mode. In design mode some of the properties (the
design-time ones) are supposed to be redirected to the
designer. The component which should be used to access the
property is retrieved by using MemberDescriptor.GetInvokee
(implemented in the patch). Updated the
ReflectorPropertyDescriptor to use GetInvokee to decide which
component should it use.

Reviewed by: Miguel de Icaza

svn path=/trunk/mcs/; revision=68728

mcs/class/System/System.ComponentModel/ChangeLog
mcs/class/System/System.ComponentModel/MemberDescriptor.cs
mcs/class/System/System.ComponentModel/ReflectionPropertyDescriptor.cs

index cdf62da6e4719114c150491d3b3aec809eacd6ff..8a66a98b8314b8bd161a921461fcc6942b396038 100644 (file)
@@ -1,3 +1,20 @@
+2006-11-29  Ivan N. Zlatev <contact@i-nz.net>
+
+       * MemberDescriptor.cs, ReflectionPropertyDescriptor.cs: 1) The
+       ReflectionPropretyDescriptor must be able to operate with non
+       public properties.
+
+       2) The current implementation ignores the fact that the component
+       can be in design mode. In design mode some of the properties (the
+       design-time ones) are supposed to be redirected to the
+       designer. The component which should be used to access the
+       property is retrieved by using MemberDescriptor.GetInvokee
+       (implemented in the patch). Updated the
+       ReflectorPropertyDescriptor to use GetInvokee to decide which
+       component should it use.
+
+       Reviewed by: Miguel de Icaza
+
 2006-11-28  Miguel de Icaza  <miguel@novell.com>
 
        * TypeDescriptor.cs: This implementation is really from Gonzalo,
index 136a367dff0b4fe00b399d468875e7854f5eeebf..3f84987fb9711ba293dbd5c646a4ebcaab756a5c 100644 (file)
@@ -4,6 +4,7 @@
 // Author:
 //  Miguel de Icaza (miguel@ximian.com)
 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+//  Ivan N. Zlatev <contact@i-nz.net>
 //
 // (C) Ximian, Inc.  http://www.ximian.com
 // (C) 2003 Andreas Nahr
 // distribute, sublicense, and/or sell copies of the Software, and to
 // permit persons to whom the Software is furnished to do so, subject to
 // the following conditions:
-// 
+//
 // The above copyright notice and this permission notice shall be
 // included in all copies or substantial portions of the Software.
-// 
+//
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -34,18 +35,19 @@ using System;
 using System.Collections;
 using System.Reflection;
 using System.Runtime.InteropServices;
+using System.ComponentModel.Design;
 
 namespace System.ComponentModel
 {
 
     [ComVisible (true)]
-    public abstract class MemberDescriptor 
+    public abstract class MemberDescriptor
     {
 
         private string name;
         private Attribute [] attrs;
         private AttributeCollection attrCollection;
-               
+
         protected MemberDescriptor (string name, Attribute [] attrs)
         {
             this.name = name;
@@ -69,15 +71,15 @@ namespace System.ComponentModel
             attrs = reference.AttributeArray;
         }
 
-        protected virtual Attribute [] AttributeArray 
+        protected virtual Attribute [] AttributeArray
         {
-            get 
+            get
             {
-                               if (attrs == null) 
+                               if (attrs == null)
                                {
                                        ArrayList list = new ArrayList ();
                                        FillAttributes (list);
-                                       
+
                                        ArrayList filtered = new ArrayList ();
                                        foreach (Attribute at in list) {
                                                bool found = false;
@@ -88,11 +90,11 @@ namespace System.ComponentModel
                                        }
                                        attrs = (Attribute[]) filtered.ToArray (typeof(Attribute));
                                }
-                               
+
                 return attrs;
             }
 
-            set 
+            set
             {
                 attrs = value;
             }
@@ -105,7 +107,7 @@ namespace System.ComponentModel
 
         public virtual AttributeCollection Attributes
         {
-            get 
+            get
             {
                 if (attrCollection == null)
                     attrCollection = CreateAttributeCollection ();
@@ -117,18 +119,18 @@ namespace System.ComponentModel
         {
             return new AttributeCollection (AttributeArray);
         }
-                       
-        public virtual string Category 
+
+        public virtual string Category
         {
-            get 
+            get
             {
                 return ((CategoryAttribute) Attributes [typeof (CategoryAttribute)]).Category;
             }
         }
 
-        public virtual string Description 
+        public virtual string Description
         {
-            get 
+            get
             {
                 foreach (Attribute attr in AttributeArray)
                 {
@@ -139,9 +141,9 @@ namespace System.ComponentModel
             }
         }
 
-        public virtual bool DesignTimeOnly 
+        public virtual bool DesignTimeOnly
         {
-            get 
+            get
             {
                 foreach (Attribute attr in AttributeArray)
                 {
@@ -153,25 +155,25 @@ namespace System.ComponentModel
             }
         }
 
-        public virtual string DisplayName 
+        public virtual string DisplayName
         {
-            get 
+            get
             {
                 return name;
             }
         }
 
-        public virtual string Name 
+        public virtual string Name
         {
-            get 
+            get
             {
                 return name;
             }
         }
 
-        public virtual bool IsBrowsable 
+        public virtual bool IsBrowsable
         {
-            get 
+            get
             {
                 foreach (Attribute attr in AttributeArray)
                 {
@@ -183,15 +185,15 @@ namespace System.ComponentModel
             }
         }
 
-        protected virtual int NameHashCode 
+        protected virtual int NameHashCode
         {
-            get 
+            get
             {
                 return name.GetHashCode ();
             }
         }
 
-        public override int GetHashCode() 
+        public override int GetHashCode()
         {
             return base.GetHashCode ();
         }
@@ -200,7 +202,7 @@ namespace System.ComponentModel
         {
                        MemberDescriptor other = obj as MemberDescriptor;
             if (other == null) return false;
-                       
+
             return other.name == name;
         }
 
@@ -212,15 +214,21 @@ namespace System.ComponentModel
                 return null;
         }
 
-        [MonoTODO]
         protected static object GetInvokee(Type componentClass, object component)
         {
-            // FIXME WHAT should that do???
-                       
-                       // Lluis: Checked with VS.NET and it always return the component, even if
-                       // it has its own designer set with DesignerAttribute. So, no idea
-                       // what this should do.
-            return component;
+               if (component is IComponent) {
+                       ISite site = ((IComponent) component).Site;
+                       if (site != null && site.DesignMode) {
+                               IDesignerHost host = site.GetService (typeof (IDesignerHost)) as IDesignerHost;
+                               if (host != null) {
+                                       IDesigner designer = host.GetDesigner ((IComponent) component);
+                                       if (designer != null && componentClass.IsInstanceOfType (designer)) {
+                                               component = designer;
+                                       }
+                               }
+                       }
+               }
+               return component;
         }
 
         protected static MethodInfo FindMethod(Type componentClass, string name, 
index 1cb8047be1c172123252a95eb15e0f110b486e25..33ff63c211f6b91e2522805c89553337f6674a3a 100644 (file)
@@ -3,8 +3,8 @@
 //
 // Author:
 //  Lluis Sanchez Gual (lluis@ximian.com)
-//
-// (C) Novell, Inc.  
+//  Ivan N. Zlatev (contact i-nZ.net)
+// (C) Novell, Inc.
 //
 
 //
 // distribute, sublicense, and/or sell copies of the Software, and to
 // permit persons to whom the Software is furnished to do so, subject to
 // the following conditions:
-// 
+//
 // The above copyright notice and this permission notice shall be
 // included in all copies or substantial portions of the Software.
-// 
+//
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -40,42 +40,43 @@ namespace System.ComponentModel
        {
                PropertyInfo _member;
                Type _componentType;
-               
+
                public ReflectionPropertyDescriptor (Type componentType, PropertyDescriptor oldPropertyDescriptor, Attribute [] attributes)
                : base (oldPropertyDescriptor, attributes)
                {
                        _componentType = componentType;
                }
-                                                        
+
                public ReflectionPropertyDescriptor (Type componentType, string name, Type type, Attribute [] attributes)
                : base (name, attributes)
                {
                        _componentType = componentType;
                }
-                                                        
+
                public ReflectionPropertyDescriptor (PropertyInfo info)
                : base (info.Name, (Attribute[])info.GetCustomAttributes (true))
                {
                        _member = info;
                        _componentType = _member.DeclaringType;
                }
-               
+
                PropertyInfo GetPropertyInfo ()
                {
                        if (_member == null) {
-                               _member = _componentType.GetProperty (Name);
+                               _member = _componentType.GetProperty (Name, BindingFlags.GetProperty |  BindingFlags.NonPublic |
+                                                                       BindingFlags.Public | BindingFlags.Instance);
                                if (_member == null)
                                        throw new ArgumentException ("Accessor methods for the " + Name + " property are missing");
                        }
                        return _member;
-               }               
+               }
 
-               public override Type ComponentType 
-               { 
+               public override Type ComponentType
+               {
                        get { return _componentType; }
                }
 
-               public override bool IsReadOnly 
+               public override bool IsReadOnly
                {
                        get
                        {
@@ -89,36 +90,37 @@ namespace System.ComponentModel
                        }
                }
 
-               public override Type PropertyType 
+               public override Type PropertyType
                {
                        get
                        {
                                return GetPropertyInfo ().PropertyType;
                        }
                }
-               
+
                public override object GetValue (object component)
                {
+                       component = MemberDescriptor.GetInvokee (_componentType, component);                    
                        return GetPropertyInfo ().GetValue (component, null);
                }
-               
+
                DesignerTransaction CreateTransaction (object obj)
                {
                        IComponent com = obj as IComponent;
                        if (com == null || com.Site == null)
                                return null;
-                       
+
                        IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
                        if (dh == null)
                                return null;
-                       
+
                        DesignerTransaction tran = dh.CreateTransaction ();
                        IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
                        if (ccs != null)
                                ccs.OnComponentChanging (com, this);
                        return tran;
                }
-               
+
                void EndTransaction (object obj, DesignerTransaction tran, object oldValue, object newValue, bool commit)
                {
                        if (tran == null) {
@@ -126,7 +128,7 @@ namespace System.ComponentModel
                                OnValueChanged (obj, new PropertyChangedEventArgs (Name));
                                return;
                        }
-                       
+
                        if (commit) {
                                IComponent com = obj as IComponent;
                                IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
@@ -138,14 +140,16 @@ namespace System.ComponentModel
                        } else
                                tran.Cancel ();
                }
-               
+
                public override void SetValue (object component, object value)
                {
                        DesignerTransaction tran = CreateTransaction (component);
-                       object old = GetValue (component);
                        
+                       object propertyHolder = MemberDescriptor.GetInvokee (_componentType, component);
+                       object old = GetValue (propertyHolder);
+
                        try {
-                               GetPropertyInfo ().SetValue (component, value, null);
+                               GetPropertyInfo ().SetValue (propertyHolder, value, null);
                                EndTransaction (component, tran, old, value, true);
                        } catch {
                                EndTransaction (component, tran, old, value, false);
@@ -155,26 +159,30 @@ namespace System.ComponentModel
 
                public override void ResetValue (object component)
                {
-                       DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
-                       if (attrib != null) 
-                               SetValue (component, attrib.Value); 
+                       object propertyHolder = MemberDescriptor.GetInvokee (_componentType, component);
                        
+                       DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
+                       if (attrib != null)
+                               SetValue (propertyHolder, attrib.Value);
+
                        DesignerTransaction tran = CreateTransaction (component);
-                       object old = GetValue (component);
-                       
+                       object old = GetValue (propertyHolder);
+
                        try {
-                               MethodInfo mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
+                               MethodInfo mi = propertyHolder.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
                                if (mi != null)
-                                       mi.Invoke (component, null);
-                               EndTransaction (component, tran, old, GetValue (component), true);
+                                       mi.Invoke (propertyHolder, null);
+                               EndTransaction (component, tran, old, GetValue (propertyHolder), true);
                        } catch {
-                               EndTransaction (component, tran, old, GetValue (component), false);
+                               EndTransaction (component, tran, old, GetValue (propertyHolder), false);
                                throw;
                        }
                }
 
                public override bool CanResetValue (object component)
                {
+                       component = MemberDescriptor.GetInvokee (_componentType, component);
+                       
                        DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
                        if (attrib != null) {
                                object current = GetValue (component);
@@ -197,6 +205,8 @@ namespace System.ComponentModel
 
                public override bool ShouldSerializeValue (object component)
                {
+                       component = MemberDescriptor.GetInvokee (_componentType, component);
+                       
                        DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
                        if (attrib != null) {
                                object current = GetValue (component);