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
18 using System
.Reflection
;
20 public enum ComponentType
28 /// Summary description for MInspector.
30 public class MInspector
39 /// <param name="instance"></param>
40 /// <returns></returns>
41 public static ComponentType
Inspect(Object instance
)
43 return Inspect( instance
.GetType() );
46 public static ComponentType
Inspect(Type target
)
48 if (typeof(MDynamicSupport
).IsAssignableFrom(target
))
50 return ComponentType
.Dynamic
;
54 if (target
.IsDefined( typeof(ManagedComponentAttribute
), true ))
56 return ComponentType
.Standard
;
60 return ComponentType
.None
;
66 /// <param name="instance"></param>
67 /// <returns></returns>
68 public static ManagementInfo
BuildInfoFromStandardComponent(Object instance
)
70 ManagementInfo info
= new ManagementInfo();
72 SetupManagedComponent(info
, instance
);
73 SetupManagedOperations(info
, instance
);
74 SetupManagedAttributes(info
, instance
);
79 private static void SetupManagedComponent(ManagementInfo info
, Object instance
)
81 Object
[] componentAtt
=
82 instance
.GetType().GetCustomAttributes(
83 typeof(ManagedComponentAttribute
), true );
85 if (componentAtt
== null || componentAtt
.Length
== 0)
87 throw new StandardComponentException("Standard component must use ManagedComponentAttribute attribute.");
90 ManagedComponentAttribute compAtt
= componentAtt
[0] as ManagedComponentAttribute
;
92 info
.Description
= compAtt
.Description
;
95 private static void SetupManagedOperations(ManagementInfo info
, Object instance
)
97 MethodInfo
[] methods
= instance
.GetType().GetMethods(BindingFlags
.Public
|BindingFlags
.Instance
);
99 foreach(MethodInfo minfo
in methods
)
101 if (minfo
.IsDefined( typeof(ManagedOperationAttribute
), true ))
103 object[] atts
= minfo
.GetCustomAttributes( typeof(ManagedOperationAttribute
), true );
105 ManagedOperationAttribute att
= (ManagedOperationAttribute
) atts
[0];
107 ParameterInfo
[] parameters
= minfo
.GetParameters();
109 Type
[] arguments
= new Type
[ parameters
.Length
];
111 for(int i
=0 ; i
< parameters
.Length
; i
++ )
113 arguments
[i
] = parameters
[i
].ParameterType
;
116 ManagementOperation operation
= new ManagementOperation(minfo
.Name
, att
.Description
, arguments
);
118 info
.Operations
.Add(operation
);
123 private static void SetupManagedAttributes(ManagementInfo info
, Object instance
)
125 PropertyInfo
[] properties
= instance
.GetType().GetProperties(BindingFlags
.Public
|BindingFlags
.Instance
);
127 foreach(PropertyInfo minfo
in properties
)
129 if (minfo
.IsDefined( typeof(ManagedAttributeAttribute
), true ))
131 object[] atts
= minfo
.GetCustomAttributes( typeof(ManagedAttributeAttribute
), true );
133 ManagedAttributeAttribute att
= (ManagedAttributeAttribute
) atts
[0];
135 ManagementAttribute attribute
= new ManagementAttribute(minfo
.Name
, att
.Description
, minfo
.PropertyType
);
137 info
.Attributes
.Add(attribute
);