public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) { PSObject obj2 = instance as PSObject; PSObjectTypeDescriptor descriptor = new PSObjectTypeDescriptor(obj2); descriptor.SettingValueException += this.SettingValueException; descriptor.GettingValueException += this.GettingValueException; return descriptor; }
/// <summary> /// Determines whether the Instance property of <paramref name="obj"/> is equal to the current Instance. /// </summary> /// <param name="obj">The Object to compare with the current Object.</param> /// <returns>true if the Instance property of <paramref name="obj"/> is equal to the current Instance; otherwise, false.</returns> public override bool Equals(object obj) { PSObjectTypeDescriptor other = obj as PSObjectTypeDescriptor; if (other == null) { return(false); } if (this.Instance == null || other.Instance == null) { return(ReferenceEquals(this, other)); } return(other.Instance.Equals(this.Instance)); }
public override bool Equals(object obj) { PSObjectTypeDescriptor objB = obj as PSObjectTypeDescriptor; if (objB == null) { return(false); } if ((this.Instance != null) && (objB.Instance != null)) { return(objB.Instance.Equals(this.Instance)); } return(object.ReferenceEquals(this, objB)); }
private static PSObject GetComponentPSObject(object component) { PSObject obj2 = component as PSObject; if (obj2 != null) { return(obj2); } PSObjectTypeDescriptor descriptor = component as PSObjectTypeDescriptor; if (descriptor == null) { throw PSTraceSource.NewArgumentException("component", "ExtendedTypeSystem", "InvalidComponent", new object[] { "component", typeof(PSObject).Name, typeof(PSObjectTypeDescriptor).Name }); } return(descriptor.Instance); }
private static PSObject GetComponentPSObject(object component) { // If you use the PSObjectTypeDescriptor directly as your object, it will be the component // if you use a provider, the PSObject will be the component. PSObject mshObj = component as PSObject; if (mshObj == null) { PSObjectTypeDescriptor descriptor = component as PSObjectTypeDescriptor; if (descriptor == null) { throw PSTraceSource.NewArgumentException("component", ExtendedTypeSystem.InvalidComponent, "component", typeof(PSObject).Name, typeof(PSObjectTypeDescriptor).Name); } mshObj = descriptor.Instance; } return(mshObj); }
/// <summary> /// Retrieves a <see cref="PSObjectTypeDescriptor"/> to provide information about the properties for an object of the type <see cref="PSObject"/>. /// </summary> /// <param name="objectType">The type of object for which to retrieve the type descriptor. If this parameter is not noll and is not the <see cref="PSObject"/>, the return of this method will be null.</param> /// <param name="instance">An instance of the type. If instance is null or has a type other than <see cref="PSObject"/>, this method returns null.</param> /// <returns>An <see cref="ICustomTypeDescriptor"/> that can provide property information for the /// type <see cref="PSObject"/>, or null if <paramref name="objectType"/> is not null, /// but has a type other than <see cref="PSObject"/>.</returns> public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) { PSObject mshObj = instance as PSObject; #region ReadMe // Instance can be null, in a couple of circumstances: // 1) In one of the many calls to this method caused by setting the SelectedObject // property of a PropertyGrid. // 2) If, by mistake, an object[] or Collection<PSObject> is used instead of an ArrayList // to set the DataSource property of a DataGrid or DatagridView. // // It would be nice to throw an exception for the case 2) instructing the user to use // an ArrayList, but since we have case 1) and maybe others we haven't found we return // an PSObjectTypeDescriptor(null). PSObjectTypeDescriptor's GetProperties // checks for null instance and returns an empty property collection. // All other overrides also check for null and return some default result. // Case 1), which is using a PropertyGrid seems to be unaffected by these results returned // by PSObjectTypeDescriptor overrides when the Instance is null, so we must conclude // that the TypeDescriptor returned by that call where instance is null is not used // for anything meaningful. That null instance PSObjectTypeDescriptor is only one // of the many PSObjectTypeDescriptor's returned by this method in a PropertyGrid use. // Some of the other calls to this method are passing a valid instance and the objects // returned by these calls seem to be the ones used for meaningful calls in the PropertyGrid. // // It might sound strange that we are not verifying the type of objectType or of instance // to be PSObject, but in this PropertyGrid use that passes a null instance (case 1), if // we return null we have an exception flagging the return as invalid. Since we cannot // return null and MSDN has a note saying that we should return null instead of throwing // exceptions, the safest behavior seems to be creating this PSObjectTypeDescriptor with // null instance. #endregion ReadMe PSObjectTypeDescriptor typeDescriptor = new PSObjectTypeDescriptor(mshObj); typeDescriptor.SettingValueException += this.SettingValueException; typeDescriptor.GettingValueException += this.GettingValueException; return(typeDescriptor); }
/// <summary> /// Retrieves a <see cref="PSObjectTypeDescriptor"/> to provide information about the properties for an object of the type <see cref="PSObject"/>. /// </summary> /// <param name="objectType">The type of object for which to retrieve the type descriptor. If this parameter is not noll and is not the <see cref="PSObject"/>, the return of this method will be null.</param> /// <param name="instance">An instance of the type. If instance is null or has a type other than <see cref="PSObject"/>, this method returns null.</param> /// <returns>An <see cref="ICustomTypeDescriptor"/> that can provide property information for the /// type <see cref="PSObject"/>, or null if <paramref name="objectType"/> is not null, /// but has a type other than <see cref="PSObject"/>.</returns> public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) { PSObject mshObj = instance as PSObject; #region ReadMe // Instance can be null, in a couple of circumstances: // 1) In one of the many calls to this method caused by setting the SelectedObject // property of a PropertyGrid. // 2) If, by mistake, an object[] or Collection<PSObject> is used instead of an ArrayList // to set the DataSource property of a DataGrid or DatagridView. // // It would be nice to throw an exception for the case 2) instructing the user to use // an ArrayList, but since we have case 1) and maybe others we haven't found we return // an PSObjectTypeDescriptor(null). PSObjectTypeDescriptor's GetProperties // checks for null instance and returns an empty property collection. // All other overrides also check for null and return some default result. // Case 1), which is using a PropertyGrid seems to be unaffected by these results returned // by PSObjectTypeDescriptor overrides when the Instance is null, so we must conclude // that the TypeDescriptor returned by that call where instance is null is not used // for anything meaningful. That null instance PSObjectTypeDescriptor is only one // of the many PSObjectTypeDescriptor's returned by this method in a PropertyGrid use. // Some of the other calls to this method are passing a valid instance and the objects // returned by these calls seem to be the ones used for meaningful calls in the PropertyGrid. // // It might sound strange that we are not verifying the type of objectType or of instance // to be PSObject, but in this PropertyGrid use that passes a null instance (case 1), if // we return null we have an exception flagging the return as invalid. Since we cannot // return null and MSDN has a note saying that we should return null instead of throwing // exceptions, the safest behavior seems to be creating this PSObjectTypeDescriptor with // null instance. #endregion ReadMe PSObjectTypeDescriptor typeDescriptor = new PSObjectTypeDescriptor(mshObj); typeDescriptor.SettingValueException += this.SettingValueException; typeDescriptor.GettingValueException += this.GettingValueException; return typeDescriptor; }