1 // Copyright 2003-2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using System
.Collections
;
20 using System
.Reflection
;
23 /// Summary description for ReflectionInvokerStrategy.
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(
39 new MemberResolver(info
, instance
.GetType()));
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
)
57 this.instance
= instance
;
58 this.resolver
= resolver
;
61 #region MDynamicSupport Members
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
)
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
));
88 foreach(MethodInfo met
in resolver
.Methods
)
90 if (!met
.Name
.Equals( operation
.Name
))
95 ParameterInfo
[] parameters
= met
.GetParameters();
97 if (MemberResolver
.Match(parameters
, signature
))
107 throw new InvalidOperationException(String
.Format("Operation {0} doesn't exists for the specified signature.", action
));
110 return method
.Invoke(instance
, args
);
116 /// <param name="name"></param>
117 /// <returns></returns>
118 public Object
GetAttributeValue(String name
)
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);
147 /// <param name="name"></param>
148 /// <param name="value"></param>
149 public void SetAttributeValue(String name
, Object
value)
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 }
);
178 public ManagementInfo Info