Why can’t I instantiate a class from Linq

Asked

Viewed 52 times

0

I’m trying to instantiate the following class from the English library

using System.Reflection;

namespace System.Linq.Expressions
{
    //
    // Summary:
    //     Represents accessing a field or property.
    public class MemberExpression : Expression
    {
        //
        // Summary:
        //     Gets the containing object of the field or property.
        //
        // Returns:
        //     An System.Linq.Expressions.Expression that represents the containing object of
        //     the field or property.
        public Expression Expression { get; }
        //
        // Summary:
        //     Gets the field or property to be accessed.
        //
        // Returns:
        //     The System.Reflection.MemberInfo that represents the field or property to be
        //     accessed.
        public MemberInfo Member { get; }
        //
        // Summary:
        //     Returns the node type of this System.Linq.Expressions.MemberExpression.Expression.
        //
        // Returns:
        //     The System.Linq.Expressions.ExpressionType that represents this expression.
        public sealed override ExpressionType NodeType { get; }

        //
        // Summary:
        //     Creates a new expression that is like this one, but using the supplied children.
        //     If all of the children are the same, it will return this expression.
        //
        // Parameters:
        //   expression:
        //     The System.Linq.Expressions.MemberExpression.Expression property of the result.
        //
        // Returns:
        //     This expression if no children are changed or an expression with the updated
        //     children.
        public MemberExpression Update(Expression expression);
        //
        // Summary:
        //     Dispatches to the specific visit method for this node type. For example, System.Linq.Expressions.MethodCallExpression
        //     calls the System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(System.Linq.Expressions.MethodCallExpression).
        //
        // Parameters:
        //   visitor:
        //     The visitor to visit this node with.
        //
        // Returns:
        //     The result of visiting this node.
        protected internal override Expression Accept(ExpressionVisitor visitor);
    }
}

The way I’m trying to instantiate is this:

public class Exemple
{
  public void ExampleMethod()
  {
     var memberExpression = new MemberExpression(); 
  }
}

inserir a descrição da imagem aqui If it is a class that is not abstract why it cannot instantiate?

1 answer

2


The message already describes the problem:

'Memberexpression' does not contain a constructor that takes 0 Arguments

That is, there is no constructor with 0 arguments

If you look at the source code here: https://github.com/microsoft/referencesource/blob/master/System.Core/Microsoft/Scripting/Ast/MemberExpression.cs

You’ll find that:

public class MemberExpression : Expression {
        private readonly Expression _expression;

        /// <summary>
        /// Gets the field or property to be accessed.
        /// </summary>
        public MemberInfo Member {
            get { return GetMember(); }
        }

        /// <summary>
        /// Gets the containing object of the field or property.
        /// </summary>
        public Expression Expression {
            get { return _expression; }
        }

        // param order: factories args in order, then other args
        internal MemberExpression(Expression expression) {

            _expression = expression;
        }
}

That is, the class has a constructor that requires an object of type Expression, and which is still internal, that is, as it is not public does not have access to it, the idea is to prevent it from being built outside the Assembly.

If you need to create an instance, in the class documentation Expression have examples: https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression?view=net-5.0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.