/// <summary>
        /// TODO: Summary
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void SetAttributeValue(String name, Object value)
        {
            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.CanWrite)
            {
                throw new InvalidOperationException(String.Format("Attribute {0} is read-only.", name));
            }

            MethodInfo setMethod = property.GetSetMethod();

            setMethod.Invoke(instance, new object[] { value });
        }
        /// <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));
        }
        private static void SetupManagedAttributes(ManagementInfo info, Object instance)
        {
            PropertyInfo[] properties = instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo minfo in properties)
            {
                if (minfo.IsDefined(typeof(ManagedAttributeAttribute), true))
                {
                    object[] atts = minfo.GetCustomAttributes(typeof(ManagedAttributeAttribute), true);

                    ManagedAttributeAttribute att = (ManagedAttributeAttribute)atts[0];

                    ManagementAttribute attribute = new ManagementAttribute(minfo.Name, att.Description, minfo.PropertyType);

                    info.Attributes.Add(attribute);
                }
            }
        }
Esempio n. 4
0
        private static void SetupManagedAttributes(ManagementInfo info, Object instance)
        {
            PropertyInfo[] properties = instance.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);

            foreach(PropertyInfo minfo in properties)
            {
                if (minfo.IsDefined( typeof(ManagedAttributeAttribute), true ))
                {
                    object[] atts = minfo.GetCustomAttributes( typeof(ManagedAttributeAttribute), true );

                    ManagedAttributeAttribute att = (ManagedAttributeAttribute) atts[0];

                    ManagementAttribute attribute = new ManagementAttribute(minfo.Name, att.Description, minfo.PropertyType);

                    info.Attributes.Add(attribute);
                }
            }
        }