示例#1
0
        /// <summary>
        /// Get a value typed property for the specified attribute name.
        /// </summary>
        /// <typeparam name="T">The return value type that should be returned.</typeparam>
        /// <param name="properties">The properties to extract the value from.</param>
        /// <param name="attributeName">The name of the attribute to get the property value for.</param>
        /// <param name="index">The array index of the proeprty we want to return (all properties are multi-valued).</param>
        /// <returns>The typed value of the property at the specified index if it can be found and cast, otherwise null/default.</returns>
        public static T?GetValue <T>(this PropertyCollection properties, string attributeName, int index = 0) where T : struct
        {
            var result = new T?();

            if (properties.Contains(attributeName) && properties[attributeName].Count >= index)
            {
                try
                {
                    if (typeof(T) == typeof(Guid))
                    {
                        var guid = properties.GetReference <string>(attributeName, index);
                        result = (T)(object)new Guid(guid);
                    }
                    else
                    {
                        result = (T)properties[attributeName][index];
                    }
                }
                catch
                {
                    // NOTE: Ignore the error in casting, we will return the default of T?.
                }
            }

            return(result);
        }