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));
        }
예제 #2
0
        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)
            {
                attributeValue = GetAttributeValueFromParameters(propertyName);
                if (attributeValue.IsBadValue)
                {
                    attributeValue = GetAttributeValueFromBaseInitialiser(propertyName);
                }
            }

            if (!attributeValue.IsBadValue && attributeValue.IsConstant)
            {
                return((TValue)attributeValue.ConstantValue.Value);
            }

            return(default(TValue));
        }
        private ConstantValue? GetAttributeNamedParameter(IAttributeInstance attributeHandle, ITypeMember memberHandle)
        {
#if RESHARPER_31
            ConstantValue2 rawValue = GetAttributeNamedParameterHack(attributeHandle, memberHandle);
            return rawValue.IsBadValue() ? (ConstantValue?)null : ConvertConstantValue(rawValue.Value);
#else
            AttributeValue rawValue = attributeHandle.NamedParameter(memberHandle);
            return rawValue.IsBadValue ? (ConstantValue?) null : ConvertConstantValue(rawValue);
#endif
        }
        private ConstantValue2 GetAttributeNamedParameterHack(IAttributeInstance attributeInstance, ITypeMember typeMember)
        {
            IAttribute attribute = GetCSharpAttributeHack(attributeInstance);
            if (attribute != null)
            {
                foreach (IPropertyAssignmentNode propertyAssignmentNode in attribute.ToTreeNode().PropertyAssignments)
                {
                    IPropertyAssignment propertyAssignment = propertyAssignmentNode;
                    if (propertyAssignment.Reference.Resolve().DeclaredElement == typeMember)
                    {
                        IType propertyType = ((ITypeOwner)typeMember).Type;
                        ICSharpExpression expression = propertyAssignment.Source;
                        return GetCSharpConstantValueHack(expression, propertyType);
                    }
                }

                return ConstantValue2.BAD_VALUE;
            }

            return attributeInstance.NamedParameter(typeMember);
        }