Simple impl to MockRailsEngineContext.Url
[castle.git] / Tools / ManagedExtensions / ManagementExtensions / Default / Strategy / ReflectionInvokerStrategy.cs
blob57af7f19c40679a64fe1e48e3f2c801f40364d32
1 // Copyright 2003-2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ManagementExtensions.Default.Strategy
17 using System;
18 using System.Text;
19 using System.Collections;
20 using System.Reflection;
22 /// <summary>
23 /// Summary description for ReflectionInvokerStrategy.
24 /// </summary>
25 public class ReflectionInvokerStrategy : InvokerStrategy
27 public ReflectionInvokerStrategy()
31 #region InvokerStrategy Members
33 public MDynamicSupport Create(Object instance)
35 ManagementInfo info = MInspector.BuildInfoFromStandardComponent(instance);
37 return new ReflectedDynamicSupport(
38 instance, info,
39 new MemberResolver(info, instance.GetType()));
42 #endregion
45 /// <summary>
46 ///
47 /// </summary>
48 class ReflectedDynamicSupport : MDynamicSupport
50 private Object instance;
51 private ManagementInfo info;
52 private MemberResolver resolver;
54 public ReflectedDynamicSupport(Object instance, ManagementInfo info, MemberResolver resolver)
56 this.info = info;
57 this.instance = instance;
58 this.resolver = resolver;
61 #region MDynamicSupport Members
63 /// <summary>
64 /// TODO: Summary
65 /// </summary>
66 /// <param name="action"></param>
67 /// <param name="args"></param>
68 /// <param name="signature"></param>
69 /// <returns></returns>
70 public Object Invoke(String action, Object[] args, Type[] signature)
72 if (action == null)
74 throw new ArgumentNullException("action");
77 ManagementOperation operation = (ManagementOperation) info.Operations[action];
79 if (operation == null)
81 throw new InvalidOperationException(String.Format("Operation {0} doesn't exists.", action));
84 MethodInfo method = resolver.GetMethod(MemberResolver.BuildOperationName(action, signature));
86 if (method == null)
88 foreach(MethodInfo met in resolver.Methods)
90 if (!met.Name.Equals( operation.Name ))
92 continue;
95 ParameterInfo[] parameters = met.GetParameters();
97 if (MemberResolver.Match(parameters, signature))
99 method = met;
100 break;
105 if (method == null)
107 throw new InvalidOperationException(String.Format("Operation {0} doesn't exists for the specified signature.", action));
110 return method.Invoke(instance, args);
113 /// <summary>
114 /// TODO: Summary
115 /// </summary>
116 /// <param name="name"></param>
117 /// <returns></returns>
118 public Object GetAttributeValue(String name)
120 if (name == null)
122 throw new ArgumentNullException("name");
125 ManagementAttribute attribute = (ManagementAttribute) info.Attributes[name];
127 if (attribute == null)
129 throw new InvalidOperationException(String.Format("Attribute {0} doesn't exists.", name));
132 PropertyInfo property = resolver.GetProperty(attribute.Name);
134 if (!property.CanRead)
136 throw new InvalidOperationException(String.Format("Attribute {0} can't be read.", name));
139 MethodInfo getMethod = property.GetGetMethod();
141 return getMethod.Invoke(instance, null);
144 /// <summary>
145 /// TODO: Summary
146 /// </summary>
147 /// <param name="name"></param>
148 /// <param name="value"></param>
149 public void SetAttributeValue(String name, Object value)
151 if (name == null)
153 throw new ArgumentNullException("name");
156 ManagementAttribute attribute = (ManagementAttribute) info.Attributes[name];
158 if (attribute == null)
160 throw new InvalidOperationException(String.Format("Attribute {0} doesn't exists.", name));
163 PropertyInfo property = resolver.GetProperty(attribute.Name);
165 if (!property.CanWrite)
167 throw new InvalidOperationException(String.Format("Attribute {0} is read-only.", name));
170 MethodInfo setMethod = property.GetSetMethod();
172 setMethod.Invoke(instance, new object[] { value } );
175 /// <summary>
176 /// TODO: Summary
177 /// </summary>
178 public ManagementInfo Info
182 return info;
186 #endregion