Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:00:02+00:00 2026-05-31T17:00:02+00:00

I have a toolkit that has many methods often taking Expression<Func<T,TProperty>> as parameters. Some

  • 0

I have a toolkit that has many methods often taking Expression<Func<T,TProperty>> as parameters. Some can be single-level only (o=>o.Name), while some can be multi-level (o=>o.EmployeeData.Address.Street).

I want to develop something (MSBuild Task? Visual Studio Plugin? hopefully the first) that reads all the user’s .cs files, and gives build errors if the given parameter is not a property-expression (but something like o=>o.Contains("foo")), or if a multi-level expression is given where only a single-level is allowed.

I tried looking at compiled IL code first but since the expression trees are a C# compiler “trick”, in IL all I see is creating expression instances and such, and while I could check each if only MemberExpressions (and the correct number of them) are created, it is not so great.

Then Roslyn came to my mind. Is it possible to write something like this with Roslyn?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:00:05+00:00Added an answer on May 31, 2026 at 5:00 pm

    Yes, I think Roslyn and its code issues are exactly the right tool for this. With them, you can analyze the code while you type and create errors (or warnings) that are shown as other errors in Visual Studio.

    I have tried to create such code issue:

    [ExportSyntaxNodeCodeIssueProvider("PropertyExpressionCodeIssue", LanguageNames.CSharp, typeof(InvocationExpressionSyntax))]
    class PropertyExpressionCodeIssueProvider : ICodeIssueProvider
    {
        [ImportingConstructor]
        public PropertyExpressionCodeIssueProvider()
        {}
    
        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var invocation = (InvocationExpressionSyntax)node;
    
            var semanticModel = document.GetSemanticModel(cancellationToken);
    
            var semanticInfo = semanticModel.GetSemanticInfo(invocation, cancellationToken);
    
            var methodSymbol = (MethodSymbol)semanticInfo.Symbol;
    
            if (methodSymbol == null)
                yield break;
    
            var attributes = methodSymbol.GetAttributes();
    
            if (!attributes.Any(a => a.AttributeClass.Name == "PropertyExpressionAttribute"))
                yield break;
    
            var arguments = invocation.ArgumentList.Arguments;
            foreach (var argument in arguments)
            {
                var lambdaExpression = argument.Expression as SimpleLambdaExpressionSyntax;
                if (lambdaExpression == null)
                    continue;
    
                var parameter = lambdaExpression.Parameter;
                var memberAccess = lambdaExpression.Body as MemberAccessExpressionSyntax;
                if (memberAccess != null)
                {
                    var objectIdentifierSyntax = memberAccess.Expression as IdentifierNameSyntax;
    
                    if (objectIdentifierSyntax != null
                        && objectIdentifierSyntax.PlainName == parameter.Identifier.ValueText
                        && semanticModel.GetSemanticInfo(memberAccess, cancellationToken).Symbol is PropertySymbol)
                        continue;
                }
    
                yield return
                    new CodeIssue(
                        CodeIssue.Severity.Error, argument.Span,
                        string.Format("Has to be simple property access of '{0}'", parameter.Identifier.ValueText));
            }
        }
    
        #region Unimplemented ICodeIssueProvider members
    
        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxToken token, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    
        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxTrivia trivia, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    }
    

    The usage would be like this:

    [AttributeUsage(AttributeTargets.Method)]
    class PropertyExpressionAttribute : Attribute
    { }
    
    …
    
    [PropertyExpression]
    static void Foo<T>(Expression<Func<SomeType, T>> expr)
    { }
    
    …
    
    Foo(x => x.P);   // OK
    Foo(x => x.M()); // error
    Foo(x => 42);    // error
    

    The code above has several issues:

    1. It’s completely unoptimized.
    2. It probably needs some more error checking.
    3. It does not work. At least in the current CTP. The expression semanticModel.GetSemanticInfo(memberAccess, cancellationToken).Symbol near the end always returns null. This is because semantics of expressions trees is among the currently unimplemented features.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.net page. That has an Ajax Toolkit Tab Control. That has
I have a Google Web Toolkit application that I am deploying to Google App
I have an ASP.NET page that uses the ASP.NET Ajax Control Toolkit TabContainer .
We have a fairly graphical intensive application that uses the FOX toolkit and OpenSceneGraph,
I have a user control that contains a WPF toolkit DataGrid . This control
Hi I have a datagrid that has a number of datagridtemplate columns that are
I have a Silverlight App that has worked well in Debug and Release modes
I have googled some time now trying to find a good sample application that
I have a Silverlight app that has a bunch of styles that are referenced
I know there have been many posts on jquery vs ajax toolkit and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.