GetProperty() public method

public GetProperty ( String propertyName ) : PropertyInfo
propertyName String
return System.Reflection.PropertyInfo
        /// <summary>
        /// TODO: Summary
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Object GetAttributeValue(String name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            ManagementAttribute attribute = (ManagementAttribute)info.Attributes[name];

            if (attribute == null)
            {
                throw new InvalidOperationException(String.Format("Attribute {0} doesn't exists.", name));
            }

            PropertyInfo property = resolver.GetProperty(attribute.Name);

            if (!property.CanRead)
            {
                throw new InvalidOperationException(String.Format("Attribute {0} can't be read.", name));
            }

            MethodInfo getMethod = property.GetGetMethod();

            return(getMethod.Invoke(instance, null));
        }
Ejemplo n.º 2
0
        private void BuildGetAttributeMethod(CodeTypeDeclaration typeDefinition)
        {
            CodeMemberMethod method = new CodeMemberMethod();

            method.Attributes = MemberAttributes.Public;
            method.Name       = "GetAttributeValue";
            method.ReturnType = new CodeTypeReference(typeof(Object));
            method.Parameters.Add(new CodeParameterDeclarationExpression(
                                      typeof(String), "name"));

            CodeArgumentReferenceExpression nameArgument =
                new CodeArgumentReferenceExpression("name");
            CodeThisReferenceExpression thisRef =
                new CodeThisReferenceExpression();

            foreach (ManagementAttribute attribute in info.Attributes)
            {
                CodeConditionStatement stmt = new CodeConditionStatement();
                stmt.Condition =
                    new CodeMethodInvokeExpression(nameArgument, "Equals",
                                                   new CodeSnippetExpression("\"" + attribute.Name + "\""));

                PropertyInfo property = resolver.GetProperty(attribute.Name);

                if (!property.CanRead)
                {
                    // TODO: Adds throw

                    continue;
                }

                CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement();
                returnStmt.Expression =
                    new CodePropertyReferenceExpression(
                        new CodeFieldReferenceExpression(
                            thisRef, "instance"), attribute.Name);

                stmt.TrueStatements.Add(returnStmt);

                method.Statements.Add(stmt);
            }

            {
                CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement();
                returnStmt.Expression = new CodeSnippetExpression("null");
                method.Statements.Add(returnStmt);
            }

            typeDefinition.Members.Add(method);
        }