private void CheckAndAddProperty(PSPropertyInfo propertyInfo, Attribute[] attributes, ref PropertyDescriptorCollection returnValue)
        {
            using (typeDescriptor.TraceScope("Checking property \"{0}\".", propertyInfo.Name))
            {
                // WriteOnly properties are not returned in TypeDescriptor.GetProperties, so we do the same.
                if (!propertyInfo.IsGettable)
                {
                    typeDescriptor.WriteLine("Property \"{0}\" is write-only so it has been skipped.", propertyInfo.Name);
                    return;
                }
                AttributeCollection propertyAttributes = null;
                Type propertyType = typeof(object);
                if (attributes != null && attributes.Length != 0)
                {
                    PSProperty property = propertyInfo as PSProperty;
                    if (property != null)
                    {
                        DotNetAdapter.PropertyCacheEntry propertyEntry = property.adapterData as DotNetAdapter.PropertyCacheEntry;
                        if (propertyEntry == null)
                        {
                            typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it is an adapted property (not a .NET property).", property.Name);
                        }
                        else if (property.isDeserialized)
                        {
                            // At the moment we are not serializing attributes, so we can skip
                            // the attribute check if the property is deserialized.
                            typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it has been deserialized.", property.Name);
                        }
                        else
                        {
                            propertyType       = propertyEntry.propertyType;
                            propertyAttributes = propertyEntry.Attributes;
                            foreach (Attribute attribute in attributes)
                            {
                                if (!propertyAttributes.Contains(attribute))
                                {
                                    typeDescriptor.WriteLine("Property \"{0}\" does not contain attribute \"{1}\" so it has been skipped.", property.Name, attribute);
                                    return;
                                }
                            }
                        }
                    }
                }

                if (propertyAttributes == null)
                {
                    propertyAttributes = new AttributeCollection();
                }

                typeDescriptor.WriteLine("Adding property \"{0}\".", propertyInfo.Name);

                PSObjectPropertyDescriptor propertyDescriptor =
                    new PSObjectPropertyDescriptor(propertyInfo.Name, propertyType, !propertyInfo.IsSettable, propertyAttributes);

                propertyDescriptor.SettingValueException += this.SettingValueException;
                propertyDescriptor.GettingValueException += this.GettingValueException;

                returnValue.Add(propertyDescriptor);
            }
        }
Example #2
0
 private void CheckAndAddProperty(PSPropertyInfo propertyInfo, Attribute[] attributes, ref PropertyDescriptorCollection returnValue)
 {
     using (typeDescriptor.TraceScope("Checking property \"{0}\".", new object[] { propertyInfo.Name }))
     {
         if (!propertyInfo.IsGettable)
         {
             typeDescriptor.WriteLine("Property \"{0}\" is write-only so it has been skipped.", new object[] { propertyInfo.Name });
         }
         else
         {
             AttributeCollection propertyAttributes = null;
             Type propertyType = typeof(object);
             if ((attributes != null) && (attributes.Length != 0))
             {
                 PSProperty property = propertyInfo as PSProperty;
                 if (property != null)
                 {
                     DotNetAdapter.PropertyCacheEntry adapterData = property.adapterData as DotNetAdapter.PropertyCacheEntry;
                     if (adapterData == null)
                     {
                         typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it is an adapted property (not a .NET property).", new object[] { property.Name });
                     }
                     else if (property.isDeserialized)
                     {
                         typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it has been deserialized.", new object[] { property.Name });
                     }
                     else
                     {
                         propertyType = adapterData.propertyType;
                         propertyAttributes = adapterData.Attributes;
                         foreach (Attribute attribute in attributes)
                         {
                             if (!propertyAttributes.Contains(attribute))
                             {
                                 typeDescriptor.WriteLine("Property \"{0}\" does not contain attribute \"{1}\" so it has been skipped.", new object[] { property.Name, attribute });
                                 return;
                             }
                         }
                     }
                 }
             }
             if (propertyAttributes == null)
             {
                 propertyAttributes = new AttributeCollection(new Attribute[0]);
             }
             typeDescriptor.WriteLine("Adding property \"{0}\".", new object[] { propertyInfo.Name });
             PSObjectPropertyDescriptor descriptor = new PSObjectPropertyDescriptor(propertyInfo.Name, propertyType, !propertyInfo.IsSettable, propertyAttributes);
             descriptor.SettingValueException += this.SettingValueException;
             descriptor.GettingValueException += this.GettingValueException;
             returnValue.Add(descriptor);
         }
     }
 }
Example #3
0
 private void CheckAndAddProperty(PSPropertyInfo propertyInfo, Attribute[] attributes, ref PropertyDescriptorCollection returnValue)
 {
     using (typeDescriptor.TraceScope("Checking property \"{0}\".", new object[] { propertyInfo.Name }))
     {
         if (!propertyInfo.IsGettable)
         {
             typeDescriptor.WriteLine("Property \"{0}\" is write-only so it has been skipped.", new object[] { propertyInfo.Name });
         }
         else
         {
             AttributeCollection propertyAttributes = null;
             Type propertyType = typeof(object);
             if ((attributes != null) && (attributes.Length != 0))
             {
                 PSProperty property = propertyInfo as PSProperty;
                 if (property != null)
                 {
                     DotNetAdapter.PropertyCacheEntry adapterData = property.adapterData as DotNetAdapter.PropertyCacheEntry;
                     if (adapterData == null)
                     {
                         typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it is an adapted property (not a .NET property).", new object[] { property.Name });
                     }
                     else if (property.isDeserialized)
                     {
                         typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it has been deserialized.", new object[] { property.Name });
                     }
                     else
                     {
                         propertyType       = adapterData.propertyType;
                         propertyAttributes = adapterData.Attributes;
                         foreach (Attribute attribute in attributes)
                         {
                             if (!propertyAttributes.Contains(attribute))
                             {
                                 typeDescriptor.WriteLine("Property \"{0}\" does not contain attribute \"{1}\" so it has been skipped.", new object[] { property.Name, attribute });
                                 return;
                             }
                         }
                     }
                 }
             }
             if (propertyAttributes == null)
             {
                 propertyAttributes = new AttributeCollection(new Attribute[0]);
             }
             typeDescriptor.WriteLine("Adding property \"{0}\".", new object[] { propertyInfo.Name });
             PSObjectPropertyDescriptor descriptor = new PSObjectPropertyDescriptor(propertyInfo.Name, propertyType, !propertyInfo.IsSettable, propertyAttributes);
             descriptor.SettingValueException += this.SettingValueException;
             descriptor.GettingValueException += this.GettingValueException;
             returnValue.Add(descriptor);
         }
     }
 }
Example #4
0
        public override object GetValue(object component)
        {
            PSObject psObject = component != null?PSObjectPropertyDescriptor.GetComponentPSObject(component) : throw PSObjectPropertyDescriptor.tracer.NewArgumentNullException(nameof(component));

            try
            {
                PSPropertyInfo property = psObject.Properties[this.Name];
                if (property != null)
                {
                    return(property.Value);
                }
                PSObjectTypeDescriptor.typeDescriptor.WriteLine("Could not find property \"{0}\" to get its value.", (object)this.Name);
                ExtendedTypeSystemException e = new ExtendedTypeSystemException("PropertyNotFoundInPropertyDescriptorGetValue", (Exception)null, "ExtendedTypeSystem", "PropertyNotFoundInTypeDescriptor", new object[1]
                {
                    (object)this.Name
                });
                bool   shouldThrow;
                object valueException = this.DealWithGetValueException(e, out shouldThrow);
                if (shouldThrow)
                {
                    throw e;
                }
                return(valueException);
            }
            catch (ExtendedTypeSystemException ex)
            {
                PSObjectTypeDescriptor.typeDescriptor.WriteLine("Exception getting the value of the property \"{0}\": \"{1}\".", (object)this.Name, (object)ex.Message);
                bool   shouldThrow;
                object valueException = this.DealWithGetValueException(ex, out shouldThrow);
                if (!shouldThrow)
                {
                    return(valueException);
                }
                throw;
            }
        }
        private void CheckAndAddProperty(PSPropertyInfo propertyInfo, Attribute[] attributes, ref PropertyDescriptorCollection returnValue)
        {
            using (typeDescriptor.TraceScope("Checking property \"{0}\".", propertyInfo.Name))
            {
                // WriteOnly properties are not returned in TypeDescriptor.GetProperties, so we do the same.
                if (!propertyInfo.IsGettable)
                {
                    typeDescriptor.WriteLine("Property \"{0}\" is write-only so it has been skipped.", propertyInfo.Name);
                    return;
                }
                AttributeCollection propertyAttributes = null;
                Type propertyType = typeof(object);
                if (attributes != null && attributes.Length != 0)
                {
                    PSProperty property = propertyInfo as PSProperty;
                    if (property != null)
                    {
                        DotNetAdapter.PropertyCacheEntry propertyEntry = property.adapterData as DotNetAdapter.PropertyCacheEntry;
                        if (propertyEntry == null)
                        {
                            typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it is an adapted property (not a .NET property).", property.Name);
                        }
                        else if (property.isDeserialized)
                        {
                            // At the moment we are not serializing attributes, so we can skip
                            // the attribute check if the property is deserialized.
                            typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it has been deserialized.", property.Name);
                        }
                        else
                        {
                            propertyType = propertyEntry.propertyType;
                            propertyAttributes = propertyEntry.Attributes;
                            foreach (Attribute attribute in attributes)
                            {
                                if (!propertyAttributes.Contains(attribute))
                                {
                                    typeDescriptor.WriteLine("Property \"{0}\" does not contain attribute \"{1}\" so it has been skipped.", property.Name, attribute);
                                    return;
                                }
                            }
                        }
                    }
                }

                if (propertyAttributes == null)
                {
                    propertyAttributes = new AttributeCollection();
                }

                typeDescriptor.WriteLine("Adding property \"{0}\".", propertyInfo.Name);

                PSObjectPropertyDescriptor propertyDescriptor =
                    new PSObjectPropertyDescriptor(propertyInfo.Name, propertyType, !propertyInfo.IsSettable, propertyAttributes);

                propertyDescriptor.SettingValueException += this.SettingValueException;
                propertyDescriptor.GettingValueException += this.GettingValueException;

                returnValue.Add(propertyDescriptor);
            }
        }