Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not use 'System.Runtime.CompilerServices.DynamicAttribute'. Use the 'dynamic' keyword instead

Tags:

c#

webmatrix

I copy and paste source of WebMatrix.WebData.IDatabase but VStudio mark Dynamic words and said

Do not use 'System.Runtime.CompilerServices.DynamicAttribute'. Use the 'dynamic' keyword instead.

Why i get this error when original .net framework used it?

The source code is here:

namespace WebMatrix.WebData
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;

    internal interface IDatabase : IDisposable
    {
        int Execute(string commandText, params object[] args);

        [return: Dynamic(new bool[] { false, true })]
        IEnumerable<object> Query(string commandText, params object[] parameters);

        [return: Dynamic]
        object QuerySingle(string commandText, params object[] args);

        [return: Dynamic]
        object QueryValue(string commandText, params object[] parameters);
    }
}

and I get same error here:

namespace WebMatrix.WebData
{
    internal class DatabaseWrapper : IDatabase, IDisposable
    {
        private readonly Database _database;

        public DatabaseWrapper(Database database)
        {
            this._database = database;
        }

        public void Dispose()
        {
            this._database.Dispose();
        }

        public int Execute(string commandText, params object[] parameters)
        {
            return this._database.Execute(commandText, parameters);
        }

        [return: Dynamic(new bool[] { false, true })]
        public IEnumerable<object> Query(string commandText, params object[] parameters)
        {
            return this._database.Query(commandText, parameters);
        }

        [return: Dynamic]
        public object QuerySingle(string commandText, params object[] parameters)
        {
            return this._database.QuerySingle(commandText, parameters);
        }

        [return: Dynamic]
        public object QueryValue(string commandText, params object[] parameters)
        {
            return this._database.QueryValue(commandText, parameters);
        }
    }
}
like image 627
Mehdi Yeganeh Avatar asked May 14 '13 09:05

Mehdi Yeganeh


1 Answers

That maps to:

IEnumerable<dynamic> Query(string commandText, params object[] parameters);
dynamic QuerySingle(string commandText, params object[] args);
dynamic QueryValue(string commandText, params object[] parameters);

The only reason I can think of to use the alternative syntax is for compiling it on a down-level compiler that doesn't know about the dynamic keyword. However, [DynamicAttribute] has only existed since .NET 4.0, which includes a compiler with dynamic support. Thoughts:

  • is it perhaps using the asp.net compiler, which might not necessarily be configured to use vLatest
  • does this perhaps relate to early development against CTP builds, when dynamic was incomplete

If you look at the current source, it uses dynamic, supporting the hypothesis that any [Dynamic] usage was purely transitional.

like image 68
Marc Gravell Avatar answered Sep 30 '22 23:09

Marc Gravell