Removed PROPERTYKEY constructor for PropertyDescription and moved to factory method Create since it can return a 'null'. Changed associated code.

pull/25/head
David Hall 2018-11-27 12:07:03 -07:00
parent de61f90621
commit 12c150a15b
4 changed files with 11 additions and 17 deletions

View File

@ -138,7 +138,7 @@ namespace Vanara.Windows.Shell
{ {
value = null; value = null;
if (!ContainsKey(key)) return false; if (!ContainsKey(key)) return false;
value = new ShellAssociation(key); value = ShellAssociation.CreateFromFileExtension(key);
return true; return true;
} }

View File

@ -23,15 +23,6 @@ namespace Vanara.Windows.Shell
/// <summary>Gets the type list.</summary> /// <summary>Gets the type list.</summary>
protected PropertyTypeList typeList; protected PropertyTypeList typeList;
/// <summary>Initializes a new instance of the <see cref="PropertyDescription"/> class.</summary>
/// <param name="propkey">A valid <see cref="PROPERTYKEY"/>.</param>
public PropertyDescription(PROPERTYKEY propkey)
{
key = propkey;
if (PSGetPropertyDescription(propkey, typeof(IPropertyDescription).GUID, out var ppv).Succeeded)
iDesc = (IPropertyDescription)ppv;
}
/// <summary>Initializes a new instance of the <see cref="PropertyDescription"/> class.</summary> /// <summary>Initializes a new instance of the <see cref="PropertyDescription"/> class.</summary>
/// <param name="propertyDescription">The property description.</param> /// <param name="propertyDescription">The property description.</param>
protected internal PropertyDescription(IPropertyDescription propertyDescription) protected internal PropertyDescription(IPropertyDescription propertyDescription)
@ -40,6 +31,11 @@ namespace Vanara.Windows.Shell
key = iDesc.GetPropertyKey(); key = iDesc.GetPropertyKey();
} }
/// <summary>Creates a <see cref="PropertyDescription"/> instance from a specified property key.</summary>
/// <param name="propkey">The property key.</param>
/// <returns>An associated instance of <see cref="PropertyDescription"/> or <see langword="null"/> if the PROPERTYKEY does not exist in the schema subsystem cache.</returns>
public static PropertyDescription Create(PROPERTYKEY propkey) => PSGetPropertyDescription(propkey, typeof(IPropertyDescription).GUID, out var ppv).Succeeded ? new PropertyDescription((IPropertyDescription)ppv) : null;
/// <summary>Gets a value that describes how the property values are displayed when multiple items are selected in the UI.</summary> /// <summary>Gets a value that describes how the property values are displayed when multiple items are selected in the UI.</summary>
public PROPDESC_AGGREGATION_TYPE AggregationType => iDesc?.GetAggregationType() ?? 0; public PROPDESC_AGGREGATION_TYPE AggregationType => iDesc?.GetAggregationType() ?? 0;
@ -201,7 +197,7 @@ namespace Vanara.Windows.Shell
/// <value>The <see cref="PropertyDescription" />.</value> /// <value>The <see cref="PropertyDescription" />.</value>
/// <param name="propkey">The PROPERTYKEY.</param> /// <param name="propkey">The PROPERTYKEY.</param>
/// <returns>The <see cref="PropertyDescription" /> for the specified key.</returns> /// <returns>The <see cref="PropertyDescription" /> for the specified key.</returns>
public virtual PropertyDescription this[PROPERTYKEY propkey] => new PropertyDescription(propkey); public virtual PropertyDescription this[PROPERTYKEY propkey] => PropertyDescription.Create(propkey);
/// <inheritdoc /> /// <inheritdoc />
public virtual void Dispose() public virtual void Dispose()

View File

@ -183,7 +183,7 @@ namespace Vanara.Windows.Shell
{ {
if (!TryGetValue(key, out PROPVARIANT ret)) if (!TryGetValue(key, out PROPVARIANT ret))
throw new ArgumentOutOfRangeException(nameof(key)); throw new ArgumentOutOfRangeException(nameof(key));
return new PropertyDescription(key).FormatForDisplay(ret, flags); return PropertyDescription.Create(key)?.FormatForDisplay(ret, flags);
} }
/// <summary>Gets the PROPVARIANT value for a key.</summary> /// <summary>Gets the PROPVARIANT value for a key.</summary>
@ -283,18 +283,16 @@ namespace Vanara.Windows.Shell
private bool TryGetValue(PROPERTYKEY key, out PROPVARIANT value) private bool TryGetValue(PROPERTYKEY key, out PROPVARIANT value)
{ {
value = new PROPVARIANT();
if (iprops != null) if (iprops != null)
{ {
try try
{ {
var pv = new PROPVARIANT(); iprops.GetValue(key, value);
iprops.GetValue(key, pv);
value = pv;
return true; return true;
} }
catch { } catch { }
} }
value = null;
return false; return false;
} }
} }

View File

@ -8,7 +8,7 @@ namespace Vanara.Windows.Shell
{ {
private IQueryAssociations qassoc; private IQueryAssociations qassoc;
internal ShellAssociation(string ext) { Extension = ext; } private ShellAssociation(string ext) { Extension = ext; }
public static IReadOnlyDictionary<string, ShellAssociation> FileAssociations { get; } = new ShellAssociationDictionary(true); public static IReadOnlyDictionary<string, ShellAssociation> FileAssociations { get; } = new ShellAssociationDictionary(true);