private static (TextRange, IStringLiteralAlterer) FindTokenTextRange(this ICSharpArgument argument, MessageTemplateToken token) { var documentRange = argument.GetDocumentRange(); if (argument.Value is IAdditiveExpression additiveExpression && additiveExpression.ConstantValue.IsString()) { var arguments = new LinkedList <ExpressionArgumentInfo>(); FlattenAdditiveExpression(additiveExpression, arguments); var globalOffset = 0; foreach (var additiveArgument in arguments) { var range = additiveArgument.GetDocumentRange(); var start = range.StartOffset.Offset; var end = range.EndOffset.Offset; // The token index is zero-based and we remove two quotes if (token.StartIndex < end - start - 3 + globalOffset) { var tokenStartIndex = start + token.StartIndex - globalOffset + 1; var tokenEndIndex = tokenStartIndex + token.Length; return(new TextRange(tokenStartIndex, end > tokenEndIndex ? tokenEndIndex : end), StringLiteralAltererUtil.TryCreateStringLiteralByExpression(additiveArgument.Expression)); } globalOffset += end - start - 2; } } var startOffset = documentRange.TextRange.StartOffset + token.StartIndex + 1; // ReSharper disable once AssignNullToNotNullAttribute return(new TextRange(startOffset, startOffset + token.Length), StringLiteralAltererUtil.TryCreateStringLiteralByExpression(argument.Expression)); }
protected override IEnumerable <IComponentRegistration> FromArguments(IInvocationExpression invocationExpression) { if (invocationExpression.Arguments.Count != 2) { yield break; } ICSharpArgument arg1 = invocationExpression.Arguments.First(); ITypeElement typeElement = arg1.With(f => f.Value as ITypeofExpression) .With(f => f.ArgumentType as IDeclaredType) .With(f => f.GetTypeElement()); if (typeElement == null) { yield break; } ICSharpArgument arg2 = invocationExpression.Arguments.Last(); IEnumerable <ITypeElement> concreteTypes = arg2.With(f => f.Value) .With(f => f.GetRegisteredTypes()).ToList(); if (concreteTypes.Any()) { yield return(CreateRegistrations(invocationExpression, typeElement, concreteTypes)); } }
public TestCaseArgumentInfo(ICSharpArgument argument = null, IExpressionInitializer arrayExpressionInitializer = null) { Assertion.Assert((argument == null) ^ (arrayExpressionInitializer == null), "argument XOR arrayExpressionInitializer"); Argument = argument; ArrayExpressionInitializer = arrayExpressionInitializer; }
/// <summary> /// The execute inner. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="textControl"> /// The text control. /// </param> public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl) { IAttributesOwnerDeclaration declaration = Utils.GetTypeClosestToTextControl <IAttributesOwnerDeclaration>(solution, textControl); if (declaration != null) { string rulesNamespace = this.Rule.Namespace; string ruleText = string.Format("{0}:{1}", this.Rule.CheckId, this.Rule.Name); IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solution); string justificationText = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.SuppressStyleCopAttributeJustificationText); CSharpElementFactory factory = CSharpElementFactory.GetInstance(declaration.GetPsiModule()); ITypeElement typeElement = Utils.GetTypeElement(declaration, "System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"); IAttribute attribute = factory.CreateAttribute(typeElement); ICSharpArgument newArg1 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), rulesNamespace), null); ICSharpArgument newArg2 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), ruleText), newArg1); IPropertyAssignment propertyAssignment = factory.CreatePropertyAssignment( "Justification", factory.CreateExpression("\"$0\"", justificationText)); attribute.AddPropertyAssignmentAfter(propertyAssignment, null); declaration.AddAttributeAfter(attribute, null); } }
protected static Message ExtractMessage(ICSharpArgument argument) { Contract.Requires(argument != null); Contract.Ensures(Contract.Result<Message>() != null); return argument.Expression.With(Message.Create); }
private static (PropertyToken token, int index) GetSelectedToken( IPsiView psiView, ICSharpArgument templateArgument, PropertyToken[] properties) { var documentRange = templateArgument.GetDocumentRange(); var selectedTreeRange = psiView.GetSelectedTreeRange(templateArgument); var startSelectedIndex = selectedTreeRange.StartOffset.Offset - documentRange.StartOffset.Offset; var endSelectedIndex = selectedTreeRange.EndOffset.Offset - documentRange.StartOffset.Offset - 1; var propertyIndex = 0; foreach (var property in properties) { if (property.StartIndex < startSelectedIndex && property.StartIndex + property.Length >= endSelectedIndex) { return(property, propertyIndex); } propertyIndex++; } return(null, -1); }
/// <summary> /// Initializes a new instance of the <see cref="CSharpFeatureCall"/> class. /// </summary> /// <param name="context">The creation context.</param> /// <param name="source">Details of the feature call.</param> public CSharpFeatureCall(ICSharpContext context, IFeatureCall source) { foreach (IParameter Item in source.ParameterList) { ICSharpClass Owner = context.GetClass(Item.ResolvedParameter.Location.EmbeddingClass); ICSharpParameter NewParameter = CSharpParameter.Create(context, Item, Owner); ParameterList.Add(NewParameter); } foreach (IParameter Item in source.ResultList) { ICSharpClass Owner = context.GetClass(Item.ResolvedParameter.Location.EmbeddingClass); ICSharpParameter NewParameter = CSharpParameter.Create(context, Item, Owner); ResultList.Add(NewParameter); } foreach (IArgument Item in source.ArgumentList) { ICSharpArgument NewArgument = CSharpArgument.Create(context, Item); ArgumentList.Add(NewArgument); } Debug.Assert(ParameterList.Count >= ArgumentList.Count); Count = ParameterList.Count; ArgumentStyle = source.TypeArgumentStyle; }
public INamespace GetNamespace <T>(T element, out bool includeSubnamespaces) { var arguments = element as ICSharpArgument[]; if (arguments == null) { includeSubnamespaces = false; return(null); } INamespace namespaceElement = null; if (arguments.Length > 0) { namespaceElement = GetNamespaceDeclaration(arguments[0].Value); } includeSubnamespaces = false; if (arguments.Length == 2) { ICSharpArgument boolArgument = arguments[1]; if (boolArgument.Value.ConstantValue != null && boolArgument.Value.ConstantValue.IsBoolean()) { includeSubnamespaces = Convert.ToBoolean(boolArgument.Value.ConstantValue.Value); } } return(namespaceElement); }
protected static Message ExtractMessage(ICSharpArgument argument) { Contract.Requires(argument != null); Contract.Ensures(Contract.Result <Message>() != null); return(argument.Expression.With(Message.Create)); }
private static void CheckMessageArgument( ICSharpExpression conditionExpression, ConditionType conditionType, ICSharpArgument nextToConditionArgument, IHighlightingConsumer consumer) { if (nextToConditionArgument.Value is ICSharpLiteralExpression literalExpression) { if (literalExpression.ConstantValue.Value is string message) { var nullabilityEqualityPostfixLength = MatchNullabilityEqualityPostfix(message); if (nullabilityEqualityPostfixLength.HasValue) { int messagePostfixLength = 0; if (conditionType == ConditionType.NullEqualityCheck) { // For not null/null assertions we strip the postfix away. messagePostfixLength = nullabilityEqualityPostfixLength.Value; } CheckMessageExpression(conditionExpression, literalExpression, message, messagePostfixLength, consumer); } } } }
public static MessageTemplateTokenInformation GetTokenInformation(this ICSharpArgument argument, MessageTemplateToken token) { var(tokenTextRange, tokenArgument) = FindTokenTextRange(argument, token); var tokenDocument = argument.GetDocumentRange().Document; var documentRange = new DocumentRange(tokenDocument, tokenTextRange); return(new MessageTemplateTokenInformation(documentRange, tokenArgument)); }
public static IType GetArgumentTypeValue([NotNull] this IAttribute attribute, int argumentPosition) { if (argumentPosition < attribute.Arguments.Count) { ICSharpArgument argument = attribute.Arguments[argumentPosition]; return((argument.Value as ITypeofExpression)?.ArgumentType); } return(null); }
public static object GetArgumentConstantValue([NotNull] this IAttribute attribute, int argumentPosition) { if (argumentPosition < attribute.Arguments.Count) { ICSharpArgument argument = attribute.Arguments[argumentPosition]; return(argument.Value?.ConstantValue.Value); } return(null); }
/// <summary> /// Gets the argument text. /// </summary> /// <param name="paramName">Name of the parameter.</param> /// <returns> /// The text of the argument with that parameter name. /// </returns> private string GetArgumentText(string paramName) { ICSharpArgument argument = GetArgumentByName(paramName); if (argument != null) { return(ExtractArgumentValue(argument)); } return(string.Empty); }
public static string TryGetTemplateText(this ICSharpArgument argument) { if (argument.Value is IAdditiveExpression additiveExpression && additiveExpression.ConstantValue.IsString()) { var linkedList = new LinkedList <ExpressionArgumentInfo>(); FlattenAdditiveExpression(additiveExpression, linkedList); return(string.Join(string.Empty, linkedList.Select(l => l.Expression.GetExpressionText()))); } return(argument.Value.GetExpressionText()); }
protected override void AddStub(ICSharpArgument cSharpArgument, IArgumentsOwner reference, string recommendedName) { var referenceExpression = cSharpArgument.Value as IReferenceExpression; var anchor = reference.GetContainingNode<IBlock>(); var containingMethod = anchor.Parent as IMethodDeclaration; if (containingMethod == null) return; if (!containingMethod.Attributes.Select(x => x.Name.QualifiedName).Any(x => x == "SetUp" || x == "Test" || x == "TestCase")) return; var addStub = new AddRhinoStub(recommendedName, referenceExpression, anchor); addStub.Execute(); }
public static bool IsBurstPossibleArgumentString([NotNull] ICSharpArgument argument) { var unused = argument.Expression; // for debug var expressionType = argument.GetExpressionType(); var type = expressionType.ToIType(); // if expression is type A -> then everything that returns from it is A. // if in burst context there are managed variables(like string) -> it will be highlighted // assume there are no // then there are only IStringLiteralOwner, fixedString and IInvocationExpression return(type.IsString() || IsFixedString(type)); }
public static string TryGetTemplateText(this ICSharpArgument argument) { if (argument.Value is IAdditiveExpression additiveExpression && additiveExpression.ConstantValue.IsString()) { var linkedList = new LinkedList <ExpressionArgumentInfo>(); FlattenAdditiveExpression(additiveExpression, linkedList); return(string.Join(string.Empty, linkedList.Select(l => StringLiteralAltererUtil .TryCreateStringLiteralByExpression(l.Expression) ?.Expression.GetUnquotedText()))); } return(StringLiteralAltererUtil.TryCreateStringLiteralByExpression(argument.Value)?.Expression.GetUnquotedText()); }
public ParameterizedTestMissingParameterHighlighting( IMethodDeclaration methodDeclaration, IAttribute attribute, bool isFirstMissingParameter, ICSharpExpression argumentExpression, [CanBeNull] ICSharpArgument argument) : base(argumentExpression, string.Format(Message)) { MethodDeclaration = methodDeclaration; Attribute = attribute; IsFirstMissingParameter = isFirstMissingParameter; ArgumentExpression = argumentExpression; Argument = argument; }
public PreferAddressByIdToGraphicsParamsQuickFix(PreferAddressByIdToGraphicsParamsWarning warning) { myInvocationExpression = warning.InvocationMethod; myArgument = warning.Argument; myFieldName = myGraphicsPropertyName = warning.Literal; if (!ValidityChecker.IsValidIdentifier(myFieldName)) { myFieldName = "Property"; } myArgumentExpression = warning.ArgumentExpression; myMapFunction = warning.MapFunction; myTypeName = warning.TypeName; }
/// <summary> /// The execute inner. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="textControl"> /// The text control. /// </param> public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl) { IDeclaration declaration = Utils.GetDeclarationClosestToTextControl(solution, textControl); if (declaration != null) { string rulesNamespace = this.Rule.Namespace; string ruleText = string.Format("{0}:{1}", this.Rule.CheckId, this.Rule.Name); IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solution); string justificationText = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.SuppressStyleCopAttributeJustificationText); IAttributesOwnerDeclaration attributesOwnerDeclaration = declaration as IAttributesOwnerDeclaration; if (attributesOwnerDeclaration != null) { CSharpElementFactory factory = CSharpElementFactory.GetInstance(declaration.GetPsiModule()); ITypeElement typeElement = Utils.GetTypeElement(declaration, "System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"); IAttribute attribute = factory.CreateAttribute(typeElement); ICSharpArgument newArg1 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), rulesNamespace), null); ICSharpArgument newArg2 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), ruleText), newArg1); attribute.AddArgumentAfter(Utils.CreateArgumentValueExpression(declaration.GetPsiModule(), "Justification = \"" + justificationText + "\""), newArg2); attributesOwnerDeclaration.AddAttributeAfter(attribute, null); IFile file = declaration.GetContainingFile(); if (file != null) { LanguageService languageService = CSharpLanguage.Instance.LanguageService(); if (languageService != null) { ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)languageService.CodeFormatter; if (codeFormatter != null) { codeFormatter.FormatFile(file, CodeFormatProfile.DEFAULT, NullProgressIndicator.Instance); } } } } } }
public static IStringLiteralAlterer TryCreateLastTemplateFragmentExpression(this ICSharpArgument argument) { if (argument.Value is IAdditiveExpression additiveExpression && additiveExpression.ConstantValue.IsString()) { var argumentInfo = additiveExpression.Arguments.Last(); if (argumentInfo is ExpressionArgumentInfo expressionArgumentInfo) { return(StringLiteralAltererUtil.TryCreateStringLiteralByExpression(expressionArgumentInfo.Expression)); } return(null); } return(StringLiteralAltererUtil.TryCreateStringLiteralByExpression(argument.Value)); }
private static (TextRange, IStringLiteralAlterer) FindTokenTextRange(this ICSharpArgument argument, MessageTemplateToken token) { if (argument.Value is IAdditiveExpression additiveExpression && additiveExpression.ConstantValue.IsString()) { var arguments = new LinkedList <ExpressionArgumentInfo>(); FlattenAdditiveExpression(additiveExpression, arguments); var globalOffset = 0; foreach (var additiveArgument in arguments) { var range = additiveArgument.GetDocumentRange(); var start = range.StartOffset.Offset; var end = range.EndOffset.Offset; // Usually there are two quotes in the string expression // But if it's a verbatim string, we should count @ symbol as well var isVerbatimString = additiveArgument.Expression.IsVerbatimString(); var nonTemplateTokenCount = isVerbatimString ? 3 : 2; // The token index is zero-based so we need to subtract 1 if (token.StartIndex < end - start - 1 - nonTemplateTokenCount + globalOffset) { var tokenStartIndex = start + token.StartIndex - globalOffset + 1; if (isVerbatimString) { tokenStartIndex++; } var tokenEndIndex = tokenStartIndex + token.Length; return(new TextRange(tokenStartIndex, end > tokenEndIndex ? tokenEndIndex : end), StringLiteralAltererUtil.TryCreateStringLiteralByExpression(additiveArgument.Expression)); } globalOffset += end - start - nonTemplateTokenCount; } } var startOffset = argument.GetDocumentRange().TextRange.StartOffset + token.StartIndex + 1; if (argument.Expression.IsVerbatimString()) { startOffset++; } // ReSharper disable once AssignNullToNotNullAttribute return(new TextRange(startOffset, startOffset + token.Length), StringLiteralAltererUtil.TryCreateStringLiteralByExpression(argument.Expression)); }
protected override void AddStub(ICSharpArgument cSharpArgument, IArgumentsOwner reference, string recommendedName) { var referenceExpression = cSharpArgument.Value as IReferenceExpression; var anchor = reference.GetContainingNode <IBlock>(); var containingMethod = anchor.Parent as IMethodDeclaration; if (containingMethod == null) { return; } if (!containingMethod.Attributes.Select(x => x.Name.QualifiedName).Any(x => x == "SetUp" || x == "Test" || x == "TestCase")) { return; } var addStub = new AddRhinoStub(recommendedName, referenceExpression, anchor); addStub.Execute(); }
/// <summary> /// Extracts the argument value from literal or nameof expressions. /// </summary> /// <param name="argument">The argument.</param> /// <returns> /// The argument value as a string. /// </returns> private string ExtractArgumentValue(ICSharpArgument argument) { var literal = argument.Value as ICSharpLiteralExpression; if (literal != null) { return(literal.ConstantValue.Value?.ToString() ?? string.Empty); } var expression = argument.Value as IInvocationExpression; if ((expression != null) && @"nameof".Equals(expression.InvocationExpressionReference.GetName())) { return(expression.ConstantValue.Value?.ToString() ?? string.Empty); } return(string.Empty); }
private static void HighlightTemplate( IHighlightingConsumer consumer, ICSharpArgument templateArgument, MessageTemplate messageTemplate) { foreach (var token in messageTemplate.Tokens) { if (!(token is PropertyToken)) { continue; } consumer.AddHighlighting( new StringEscapeCharacterHighlighting( templateArgument.GetTokenInformation(token).DocumentRange, DefaultLanguageAttributeIds.FORMAT_STRING_ITEM)); } }
public static object GetConstantValue(ICSharpArgument argument) { if (argument.Kind == ParameterKind.UNKNOWN) { return(IntrospectionPresenter.UnknownValue); } var literalExpression = argument.Value as ILiteralExpression; if (literalExpression == null) { return(IntrospectionPresenter.UnknownValue); } var constantValue = literalExpression.ConstantValue.Value.NotNull(); return(constantValue.ToString().Trim('"')); }
/// <summary> /// Creates a new C# argument. /// </summary> /// <param name="context">The creation context.</param> /// <param name="source">The Easly argument from which the C# argument is created.</param> public static ICSharpArgument Create(ICSharpContext context, IArgument source) { ICSharpArgument Result = null; switch (source) { case IPositionalArgument AsPositionalArgument: Result = CSharpPositionalArgument.Create(context, AsPositionalArgument); break; case IAssignmentArgument AsAssignmentArgument: Result = CSharpAssignmentArgument.Create(context, AsAssignmentArgument); break; } Debug.Assert(Result != null); return(Result); }
private static void HighlightByArgument( HighlightingsConsumer consumer, ICSharpArgument selectedArgument, ICSharpArgument templateArgument, MessageTemplate messageTemplate) { var argumentIndex = selectedArgument.IndexOf() - templateArgument.IndexOf() - 1; var namedProperties = messageTemplate.NamedProperties; var positionalProperties = messageTemplate.PositionalProperties; if (namedProperties != null && argumentIndex < namedProperties.Length) { var property = namedProperties[argumentIndex]; consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, templateArgument.GetTokenInformation(property).DocumentRange); } else if (positionalProperties != null) { foreach (var property in positionalProperties) { if (!property.TryGetPositionalValue(out int position)) { continue; } if (position != argumentIndex) { continue; } consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, templateArgument.GetTokenInformation(property).DocumentRange); } } consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, selectedArgument.GetDocumentRange()); }
private static INamespace ExtractNamespaceElement(ICSharpArgument[] arguments, out bool includeSubnamespaces) { INamespace namespaceElement = null; if (arguments.Length > 0) { namespaceElement = PsiExtensions.GetNamespaceDeclaration(arguments[0].Value); } includeSubnamespaces = false; if (arguments.Length == 2) { ICSharpArgument boolArgument = arguments[1]; if (boolArgument.Value.ConstantValue.IsBoolean()) { includeSubnamespaces = Convert.ToBoolean(boolArgument.Value.ConstantValue.Value); } } return(namespaceElement); }
private static void HighlightUnusedArguments( IInvocationExpression element, IHighlightingConsumer consumer, ICSharpArgument templateArgument, MessageTemplate messageTemplate) { var templateArgumentIndex = templateArgument.IndexOf(); foreach (var argument in element.ArgumentList.Arguments) { var argumentIndex = argument.IndexOf(); if (argumentIndex <= templateArgumentIndex) { continue; } var argumentPosition = argumentIndex - templateArgumentIndex; if (messageTemplate.NamedProperties != null) { if (messageTemplate.NamedProperties.Length < argumentPosition) { consumer.AddHighlighting(new TemplateFormatStringArgumentIsNotUsedWarning(argument)); } } else if (messageTemplate.PositionalProperties != null) { if (!messageTemplate.PositionalProperties.Any( p => p.TryGetPositionalValue(out int position) && position == argumentPosition - 1)) { consumer.AddHighlighting(new TemplateFormatStringArgumentIsNotUsedWarning(argument)); } } else { consumer.AddHighlighting(new TemplateFormatStringArgumentIsNotUsedWarning(argument)); } } }
/// <summary> /// The execute inner. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="textControl"> /// The text control. /// </param> public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl) { IDeclaration declaration = Utils.GetDeclarationClosestToTextControl(solution, textControl); if (declaration != null) { string rulesNamespace = this.Rule.Namespace; string ruleText = string.Format("{0}:{1}", this.Rule.CheckId, this.Rule.Name); string justificationText = StyleCopOptions.Instance.SuppressStyleCopAttributeJustificationText; IAttributesOwnerDeclaration attributesOwnerDeclaration = declaration as IAttributesOwnerDeclaration; if (attributesOwnerDeclaration != null) { CSharpElementFactory factory = CSharpElementFactory.GetInstance(declaration.GetPsiModule()); ITypeElement typeElement = Utils.GetTypeElement(declaration, "System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"); IAttribute attribute = factory.CreateAttribute(typeElement); ICSharpArgument newArg1 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), rulesNamespace), null); ICSharpArgument newArg2 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), ruleText), newArg1); attribute.AddArgumentAfter(Utils.CreateArgumentValueExpression(declaration.GetPsiModule(), "Justification = \"" + justificationText + "\""), newArg2); attributesOwnerDeclaration.AddAttributeAfter(attribute, null); IFile file = declaration.GetContainingFile(); CSharpFormatterHelper.FormatterInstance.FormatFile( file, SolutionCodeStyleSettings.GetInstance(solution).CodeStyleSettings, CodeFormatProfile.DEFAULT, NullProgressIndicator.Instance); } } }
public void Add(ExpressionKind expressionKind, ICSharpArgument argument) { AddAny(expressionKind, argument); }
public RedundantArgumentValueWarning(IParameter parameter, ICSharpArgument argument) { Parameter = parameter; Argument = argument; ToolTip = string.Format("Redundant value of parameter {0}", Parameter.ShortName); }
protected virtual void AddStub(ICSharpArgument cSharpArgument, IArgumentsOwner reference, string recommendedName) { }
/// <summary> /// Gets the name of the parameter of the given argument. /// </summary> /// <param name="argument">The argument.</param> /// <returns> /// The name of the parameter. /// </returns> private string GetParameterName(ICSharpArgument argument) { return argument.MatchingParameter?.Element.ShortName ?? string.Empty; }
private object GetConstantValue(ICSharpArgument argument) { if (argument.Kind == ParameterKind.UNKNOWN) return IntrospectionPresenter.UnknownValue; var literalExpression = argument.Value as ILiteralExpression; if (literalExpression == null) return IntrospectionPresenter.UnknownValue; var constantValue = literalExpression.ConstantValue.Value.AssertNotNull(); return constantValue.ToString().Trim('"'); }
/// <summary> /// Extracts the argument value from literal or nameof expressions. /// </summary> /// <param name="argument">The argument.</param> /// <returns> /// The argument value as a string. /// </returns> private string ExtractArgumentValue(ICSharpArgument argument) { var literal = argument.Value as ICSharpLiteralExpression; if (literal != null) { return literal.ConstantValue.Value?.ToString() ?? string.Empty; } var expression = argument.Value as IInvocationExpression; if ((expression != null) && @"nameof".Equals(expression.InvocationExpressionReference.GetName())) { return expression.ConstantValue.Value?.ToString() ?? string.Empty; } return string.Empty; }