示例#1
0
 private static void ExtractAttributes(ContextAttributes contextAttributes, Dictionary <string, string[]> attributes)
 {
     try
     {
         ExtractAttributes(contextAttributes.HighPriorityAttributes, attributes);
         if (contextAttributes != null)
         {
             contextAttributes.Refresh();
             foreach (ContextAttribute attr in contextAttributes)
             {
                 var attrCollection = attr.Values as ICollection;
                 if (attrCollection != null)
                 {
                     string[] values = new string[attrCollection.Count];
                     int      i      = 0;
                     foreach (string value in attrCollection)
                     {
                         values[i++] = value;
                     }
                     attributes.Add(attr.Name, values);
                 }
             }
         }
     }
     catch { }
 }
示例#2
0
 private static void ExtractAttributes(ContextAttributes contextAttributes, Dictionary<string, string[]> attributes)
 {
     try
     {
         ExtractAttributes(contextAttributes.HighPriorityAttributes, attributes);
         if (contextAttributes != null)
         {
             contextAttributes.Refresh();
             foreach (ContextAttribute attr in contextAttributes)
             {
                 var attrCollection = attr.Values as ICollection;
                 if (attrCollection != null)
                 {
                     string[] values = new string[attrCollection.Count];
                     int i = 0;
                     foreach (string value in attrCollection)
                     {
                         values[i++] = value;
                     }
                     attributes.Add(attr.Name, values);
                 }
             }
         }
     }
     catch { }
 }
示例#3
0
        private void InitializeNullableFlagsAndOriginalNullability(ref int nullableFlagsIndex, IEnumerable <dynamic> customAttributeProviders)
        {
            try
            {
                if (_nullableFlags == null)
                {
                    var nullableAttribute = ContextAttributes.FirstOrDefault(a => a.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
                    if (nullableAttribute != null)
                    {
                        _nullableFlags = GetFlagsFromNullableAttribute(nullableAttribute);
                    }
                    else if (customAttributeProviders != null)
                    {
                        _nullableFlags = GetFlagsFromCustomAttributeProviders(customAttributeProviders);
                    }
                    else
                    {
                        _nullableFlags = new byte[] { 0 };
                    }
                }
            }
            catch
            {
                _nullableFlags = new byte[] { 0 };
#if DEBUG
                throw;
#endif
            }

            if (OriginalType.GetTypeInfo().IsValueType)
            {
                OriginalNullability = Nullability.NotNullable;
            }
            else
            {
                var nullableFlag = _nullableFlags.Length > nullableFlagsIndex ? _nullableFlags[nullableFlagsIndex] : _nullableFlags.Last();
                nullableFlagsIndex++;

                OriginalNullability = nullableFlag == 0 ? Nullability.Unknown :
                                      nullableFlag == 1 ? Nullability.NotNullable :
                                      nullableFlag == 2 ? Nullability.Nullable :
                                      Nullability.Unknown;
            }
        }
        private void InitializeNullableFlagsAndOriginalNullability(ref int nullableFlagsIndex, IEnumerable <dynamic> customAttributeProviders)
        {
            try
            {
                if (_nullableFlags == null)
                {
                    var nullableAttribute = ContextAttributes.FirstOrDefault(a => a.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
                    if (nullableAttribute != null)
                    {
                        _nullableFlags = GetFlagsFromNullableAttribute(nullableAttribute);
                    }
                    else if (customAttributeProviders != null)
                    {
                        _nullableFlags = GetFlagsFromCustomAttributeProviders(customAttributeProviders);
                    }
                    else
                    {
                        _nullableFlags = new byte[] { 0 };
                    }
                }
            }
            catch
            {
                _nullableFlags = new byte[] { 0 };
#if DEBUG
                throw;
#endif
            }

            if (OriginalType.GetTypeInfo() is var typeInfo && typeInfo.IsValueType)
            {
                if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() != typeof(Nullable <>))
                {
                    nullableFlagsIndex++;
                }

                OriginalNullability = Nullability.NotNullable;
            }
示例#5
0
        private void InitializeNullableFlagsAndOriginalNullability(ref int nullableFlagsIndex, IEnumerable <dynamic>?customAttributeProviders)
        {
            var typeInfo = OriginalType.GetTypeInfo();

            try
            {
                if (_nullableFlags == null)
                {
                    var nullableAttribute = ContextAttributes.FirstOrDefault(a => a.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
                    if (nullableAttribute is not null)
                    {
                        _nullableFlags = GetFlagsFromNullableAttribute(nullableAttribute);
                    }
                    else if (typeInfo.IsGenericParameter)
                    {
                        nullableAttribute = typeInfo.GetCustomAttributes().FirstOrDefault(a => a.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
                        if (nullableAttribute is not null)
                        {
                            _nullableFlags = GetFlagsFromNullableAttribute(nullableAttribute);
                        }
                        else
                        {
                            // Default nullability (NullableContextAttribute) from the context
                            _nullableFlags = GetFlagsFromCustomAttributeProviders(typeInfo.DeclaringType.IsNested ? new dynamic[] { typeInfo.DeclaringType, typeInfo.DeclaringType.DeclaringType } : new dynamic[] { typeInfo.DeclaringType });
                        }
                    }
                    else if (customAttributeProviders is not null)
                    {
                        // Default nullability (NullableContextAttribute) from the context
                        _nullableFlags = GetFlagsFromCustomAttributeProviders(customAttributeProviders);
                    }
                    else
                    {
                        _nullableFlags = new byte[] { 0 }; // Unknown
                    }
                }
            }
            catch
            {
                _nullableFlags = new byte[] { 0 };
#if DEBUG
                throw;
#endif
            }

            if (typeInfo.IsValueType)
            {
                if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() != typeof(Nullable <>))
                {
                    nullableFlagsIndex++;
                }

                OriginalNullability = Nullability.NotNullable;
            }
            else
            {
                var nullableFlag = _nullableFlags.Length > nullableFlagsIndex ? _nullableFlags[nullableFlagsIndex] : _nullableFlags.Last();
                nullableFlagsIndex++;

                OriginalNullability = nullableFlag == 0 ? Nullability.Unknown :
                                      nullableFlag == 1 ? Nullability.NotNullable :
                                      nullableFlag == 2 ? Nullability.Nullable :
                                      Nullability.Unknown;
            }
        }
示例#6
0
 /// <summary>
 /// Gets the attributes of the given type which are defined on the context or on the type.
 /// </summary>
 /// <typeparam name="T">The attribute type.</typeparam>
 /// <returns>The attributes.</returns>
 public IEnumerable <T> GetAttributes <T>()
 {
     return(ContextAttributes.OfType <T>().Concat(TypeAttributes.OfType <T>()));
 }
示例#7
0
 /// <summary>
 /// Gets an attribute of the given type which is defined on the context or on the type.
 /// </summary>
 /// <typeparam name="T">The attribute type.</typeparam>
 /// <returns>The attribute or null.</returns>
 public T?GetAttribute <T>()
 {
     return(ContextAttributes.OfType <T>().Concat(TypeAttributes.OfType <T>()).FirstOrDefault());
 }
示例#8
0
 /// <summary>
 /// Gets an attribute of the given type which is defined on the context (property, field, parameter or contextual generic argument type).
 /// </summary>
 /// <typeparam name="T">The attribute type.</typeparam>
 /// <returns>The attribute or null.</returns>
 public T?GetContextAttribute <T>()
 {
     return(ContextAttributes.OfType <T>().SingleOrDefault());
 }
示例#9
0
 /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
 /// <param term='commandName'>The name of the command to execute.</param>
 /// <param term='executeOption'>Describes how the command should be run.</param>
 /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
 /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
 /// <param term='handled'>Informs the caller if the command was handled or not.</param>
 /// <seealso class='Exec' />
 public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
 {
     if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
     {
         if (commandName == "ReplaceF1.Connect.ReplaceF1")
         {
             // Get a reference to Solution Explorer.
             Window            activeWindow      = _applicationObject.ActiveWindow;
             ContextAttributes contextAttributes = activeWindow.DTE.ContextAttributes;
             contextAttributes.Refresh();
             List <string> attributes = new List <string>();
             try
             {
                 ContextAttributes highPri = contextAttributes == null ? null : contextAttributes.HighPriorityAttributes;
                 highPri.Refresh();
                 if (highPri != null)
                 {
                     foreach (ContextAttribute CA in highPri)
                     {
                         List <string> values = new List <string>();
                         foreach (string value in (ICollection)CA.Values)
                         {
                             values.Add(value);
                         }
                         string attribute = CA.Name + "=" + String.Join(";", values.ToArray());
                         attributes.Add(CA.Name + "=");
                     }
                 }
             }
             catch (System.Runtime.InteropServices.COMException e)
             {
                 // ignore this exception-- means there's no High Pri values here
                 string x = e.Message;
             }
             catch (System.Reflection.TargetInvocationException e)
             {
                 // ignore this exception-- means there's no High Pri values here
                 string x = e.Message;
             }
             catch (System.Exception e)
             {
                 System.Windows.Forms.MessageBox.Show(e.Message);
                 // ignore this exception-- means there's no High Pri values here
                 string x = e.Message;
             }
             // fetch context attributes that are not high-priority
             foreach (ContextAttribute CA in contextAttributes)
             {
                 List <string> values = new List <string>();
                 foreach (string value in (ICollection)CA.Values)
                 {
                     values.Add(value);
                 }
                 string attribute = CA.Name + "=" + String.Join(";", values.ToArray());
                 attributes.Add(attribute);
             }
             // Replace this call with whatever you want to do with the help context info
             HelpHandler.HandleF1(attributes);
         }
     }
 }