[mapguide-commits] r5249 - in sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI: . ObjectModels Resource

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Wed Oct 6 02:08:52 EDT 2010


Author: jng
Date: 2010-10-06 06:08:52 +0000 (Wed, 06 Oct 2010)
New Revision: 5249

Added:
   sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/ObjectModels/IDynamicInvokable.cs
Modified:
   sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj
   sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/IResource.cs
Log:
Add new interface for dynamic invocation. This will be used for resource upgrades


Modified: sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj
===================================================================
--- sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj	2010-10-06 04:54:26 UTC (rev 5248)
+++ sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj	2010-10-06 06:08:52 UTC (rev 5249)
@@ -189,6 +189,7 @@
     <Compile Include="Mapping\RuntimeMapBase.cs" />
     <Compile Include="ObjectModels\ApplicationDefinition.cs" />
     <Compile Include="ObjectModels\DrawingSource.cs" />
+    <Compile Include="ObjectModels\IDynamicInvokable.cs" />
     <Compile Include="ObjectModels\Envelope.cs" />
     <Compile Include="ObjectModels\FeatureSource.cs" />
     <Compile Include="ObjectModels\LayerDefinition.cs" />

Added: sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/ObjectModels/IDynamicInvokable.cs
===================================================================
--- sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/ObjectModels/IDynamicInvokable.cs	                        (rev 0)
+++ sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/ObjectModels/IDynamicInvokable.cs	2010-10-06 06:08:52 UTC (rev 5249)
@@ -0,0 +1,133 @@
+#region Disclaimer / License
+// Copyright (C) 2010, Jackie Ng
+// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie at gmail.com
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// 
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+// 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+// 
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Reflection;
+using System.ComponentModel;
+
+namespace OSGeo.MapGuide.MaestroAPI.ObjectModels
+{
+    /// <summary>
+    /// Interface allowing for dynamic invocation of public properties and methods.
+    /// </summary>
+    public interface IDynamicInvokable
+    {
+        //Nothing here. All done through extension methods baby!
+    }
+
+    public static class DynamicInvokableExtensions
+    {
+        /// <summary>
+        /// Gets the value of the specified property
+        /// </summary>
+        /// <param name="idi"></param>
+        /// <param name="name"></param>
+        /// <returns></returns>
+        /// <exception cref="MissingMemberException">Thrown if the specified property could not be found</exception>
+        public static object InvokeGetProperty(this IDynamicInvokable idi, string name)
+        {
+            var prop = idi.GetType().GetProperty(name);
+            if (prop != null)
+                return prop.GetValue(idi, null);
+
+            throw new MissingMemberException(name);
+        }
+
+        /// <summary>
+        /// Attempts to get the value of the specified property. Returns the specified default value if the named
+        /// property could not be found
+        /// </summary>
+        /// <param name="idi"></param>
+        /// <param name="name"></param>
+        /// <param name="defaultValue"></param>
+        /// <returns></returns>
+        public static object TryInvokeGetProperty(this IDynamicInvokable idi, string name, object defaultValue)
+        {
+            var prop = idi.GetType().GetProperty(name);
+            if (prop != null)
+                return prop.GetValue(idi, null);
+
+            return defaultValue;
+        }
+
+        /// <summary>
+        /// Sets the value of the specified property
+        /// </summary>
+        /// <param name="idi"></param>
+        /// <param name="name"></param>
+        /// <param name="value"></param>
+        /// <exception cref="MissingMemberException">thrown if the specified property could not be found</exception>
+        public static void InvokeSetProperty(this IDynamicInvokable idi, string name, object value)
+        {
+            var prop = idi.GetType().GetProperty(name);
+            if (prop != null)
+                prop.SetValue(idi, value, null);
+            
+            throw new MissingMemberException(name);
+        }
+
+        /// <summary>
+        /// Attempts to set the value of the specified property. Does nothing if the named property
+        /// could not be found
+        /// </summary>
+        /// <param name="idi"></param>
+        /// <param name="name"></param>
+        /// <param name="value"></param>
+        public static void TryInvokeSetProperty(this IDynamicInvokable idi, string name, object value)
+        {
+            var prop = idi.GetType().GetProperty(name);
+            if (prop != null)
+                prop.SetValue(idi, value, null);
+        }
+
+        /// <summary>
+        /// Invokes a method of the specified name with the specified arguments. If the method returns
+        /// a value, it is discarded. If you require the return value, use <see cref="InvokeMethodWithReturnValue"/>
+        /// instead
+        /// </summary>
+        /// <param name="idi"></param>
+        /// <param name="name"></param>
+        /// <param name="args"></param>
+        public static void InvokeMethod(this IDynamicInvokable idi, string name, params object[] args)
+        {
+            var method = idi.GetType().GetMethod(name);
+            if (method != null)
+                method.Invoke(idi, args);
+
+            throw new MissingMethodException(name);
+        }
+
+        /// <summary>
+        /// Invokes a method of the specified name with the specified arguments.
+        /// </summary>
+        /// <param name="idi"></param>
+        /// <param name="name"></param>
+        /// <param name="args"></param>
+        public static object InvokeMethodWithReturnValue(this IDynamicInvokable idi, string name, params object[] args)
+        {
+            var method = idi.GetType().GetMethod(name);
+            if (method != null)
+                return method.Invoke(idi, args);
+
+            throw new MissingMethodException(name);
+        }
+    }
+}

Modified: sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/IResource.cs
===================================================================
--- sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/IResource.cs	2010-10-06 04:54:26 UTC (rev 5248)
+++ sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/IResource.cs	2010-10-06 06:08:52 UTC (rev 5249)
@@ -24,10 +24,11 @@
 using System.IO;
 using System.ComponentModel;
 using OSGeo.MapGuide.ObjectModels.Common;
+using OSGeo.MapGuide.MaestroAPI.ObjectModels;
 
 namespace OSGeo.MapGuide.MaestroAPI.Resource
 {
-    public interface IResource : IVersionedEntity, ICloneable, INotifyPropertyChanged
+    public interface IResource : IVersionedEntity, ICloneable, INotifyPropertyChanged, IDynamicInvokable
     {
         IServerConnection CurrentConnection { get; set; }
 



More information about the mapguide-commits mailing list