示例#1
0
 static IEnumerable <AttributeValue> PositionParameters(this IAttributeInstance source)
 {
     for (int i = 0; i < source.PositionParameterCount; i++)
     {
         yield return(source.PositionParameter(i));
     }
 }
        public TValue GetPropertyValue <TValue>(string propertyName)
        {
            // Uh-oh. We're trying to get a property, but ReSharper only gives us
            // access to the constructor args that are set (what the property is
            // set to depends on the code in the constructor). If the constructor
            // is using named args, we're ok, because the name is the property name.
            // If we're not, and we're using positional parameters, we pretty much
            // have to guess. We can make a reasonable assumption that the parameter
            // name is going to match the property name, but probably in a different
            // case. This isn't ideal, but it's the only way we're going to get
            // Traits working
            var attributeValue = attribute.NamedParameter(propertyName);

            if (attributeValue.IsBadValue && attribute.Constructor != null)
            {
                var parameters = attribute.Constructor.Parameters;
                for (int i = 0; i < parameters.Count; i++)
                {
                    if (string.Compare(parameters[i].ShortName, propertyName, StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        attributeValue = attribute.PositionParameter(i);
                        break;
                    }
                }
            }
            if (attributeValue.IsConstant)
            {
                return((TValue)attributeValue.ConstantValue.Value);
            }
            return(default(TValue));
        }
 private static int? GetTagFromProtoMember(IAttributeInstance attribute)
 {
     var tagParameter = attribute.PositionParameter(0);
     if (!tagParameter.IsConstant)
         return null;
     return Convert.ToInt32(tagParameter.ConstantValue.Value);
 }
示例#4
0
        private static bool IsValidAttributeInstance([NotNull] IAttributeInstance instance)
        {
            if (instance.PositionParameterCount != 2)
            {
                return(false);
            }

            var categoryParam = instance.PositionParameter(0);

            if (!categoryParam.IsConstant || !categoryParam.ConstantValue.IsString() || (string)categoryParam.ConstantValue.Value != "ReSharper")
            {
                return(false);
            }

            var idParam = instance.PositionParameter(1);

            return(idParam.IsConstant && idParam.ConstantValue.IsString());
        }
示例#5
0
        private static string TryExtractProjectNameFromAssemblyMetadataAttribute([NotNull] IAttributeInstance attributeInstance)
        {
            const string testProjectKey = "ReSharperHelpers.TestProject";

            if (attributeInstance.PositionParameterCount != 2)
            {
                return(null);
            }

            var key = attributeInstance.PositionParameter(0);

            if (key.IsConstant && key.ConstantValue.IsString() && string.Equals((string)key.ConstantValue.Value, testProjectKey, StringComparison.Ordinal))
            {
                var value = attributeInstance.PositionParameter(1);
                if (value.IsConstant && value.ConstantValue.IsString())
                {
                    return((string)value.ConstantValue.Value);
                }
            }

            return(null);
        }
示例#6
0
        private AttributeValue GetAttributeValueFromParameters(string propertyName)
        {
            if (attribute.Constructor == null)
            {
                return(AttributeValue.BAD_VALUE);
            }

            var parameters = attribute.Constructor.Parameters;

            for (var i = 0; i < parameters.Count; i++)
            {
                if (string.Compare(parameters[i].ShortName, propertyName, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    return(attribute.PositionParameter(i));
                }
            }

            return(AttributeValue.BAD_VALUE);
        }
        private ConstantValue? GetAttributePositionParameter(IAttributeInstance attributeHandle, int index)
        {
#if RESHARPER_31
            ConstantValue2 rawValue = GetAttributePositionParameterHack(attributeHandle, index);
            return rawValue.IsBadValue() ? (ConstantValue?)null : ConvertConstantValue(rawValue.Value);
#else
            AttributeValue rawValue = attributeHandle.PositionParameter(index);
            return rawValue.IsBadValue ? (ConstantValue?) null : ConvertConstantValue(rawValue);
#endif
        }
        private ConstantValue2 GetAttributePositionParameterHack(IAttributeInstance attributeInstance, int index)
        {
            IAttribute attribute = GetCSharpAttributeHack(attributeInstance);
            if (attribute != null)
            {
                IList<ICSharpArgument> arguments = attribute.Arguments;
                if (index >= arguments.Count)
                    return ConstantValue2.BAD_VALUE;

                ICSharpExpression expression = arguments[index].Value;

                IList<IParameter> parameters = attributeInstance.Constructor.Parameters;
                int lastParameterIndex = parameters.Count - 1;
                if (index >= lastParameterIndex && parameters[lastParameterIndex].IsParameterArray)
                    return GetCSharpConstantValueHack(expression, ((IArrayType)parameters[lastParameterIndex].Type).ElementType);

                return GetCSharpConstantValueHack(arguments[index].Value, parameters[index].Type);
            }

            return attributeInstance.PositionParameter(index);
        }