Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error CS0012: The type 'Expression<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. #52729

Open
Systo01 opened this issue Apr 19, 2021 · 2 comments
Labels
Area-Interactive Resolution-Answered The question has been answered
Milestone

Comments

@Systo01
Copy link

Systo01 commented Apr 19, 2021

I'm trying to dynamically create types, but instead only get some stupid generic errors, although I'm adding all required references.

Version Used:
Visual Studio 2019, 16.9.4
Microsoft.CodeAnalysis.CSharp 3.9.0

Steps to Reproduce:

  1. Call MappersCreator.CreateType:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using DFM = Dapper.FluentMap.Mapping;

namespace Systo.ORM.Dapper
{
    public class MappersCreator
    {
        private Type CreateType(Type type, string name, Dictionary<string,string> mappings)
        {
            var assemblyName = type.Assembly.FullName;
            var nspace = type.Namespace;
            var sb = new StringBuilder();
            foreach (var kv in mappings) {
                sb.Append($"            Map(m => m.{kv.Key}).ToColumn(\"{kv.Value}\");\n");
            }
            var code = $@"using Dapper.FluentMap.Mapping;

namespace {nspace}
{{
    public class {name}Mapper : EntityMap<{name}>
    {{
        public {name}Mapper()
        {{
{sb.ToString()}
        }}
    }}
}}";
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                new[] { syntaxTree },
                new MetadataReference[] {
                    MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                    MetadataReference.CreateFromFile(type.Assembly.Location),
                    MetadataReference.CreateFromFile(typeof(DFM.EntityMap<Type>).Assembly.Location),
                    MetadataReference.CreateFromFile(typeof(Expression<Type>).Assembly.Location),
                    MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
                },
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
            );
            using (var memoryStream = new MemoryStream()) {
                EmitResult result = compilation.Emit(memoryStream);
                if (result.Success) {
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    Assembly assembly = Assembly.Load(memoryStream.ToArray());
                    Type ret = assembly.GetType($"{nspace}.{name}");
                    return ret;
                } else {
                    foreach (var diagnostic in result.Diagnostics) {
                        Console.WriteLine(diagnostic);
                    }
                    return null;
                }
            }
        }
    }
}

Expected Behavior:
The type being compiled

Actual Behavior:
(5,32): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
(9,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
(9,13): error CS0012: The type 'Expression<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
(9,30): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
(10,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
(10,13): error CS0012: The type 'Expression<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
(10,33): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Dapper" Version="2.0.78" />
    <PackageReference Include="Dapper.FluentMap" Version="2.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="System.Linq" />
    <Reference Include="System.Linq.Expressions" />
    <Reference Include="netstandard" />
  </ItemGroup>
</Project>
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 19, 2021
@sharwell
Copy link
Member

... MetadataReference.CreateFromFile(typeof(object).Assembly.Location) ...

typeof(T).Assembly.Location will almost never produce a correct reference location for compilation purposes. Compilations use reference assemblies, while typeof(T).Assembly produces an implementation assembly from a different framework and in a different location. MetadataReference targets should come from a reference assembly resolution approach that matches the behavior of MSBuild and/or NuGet.

⚠️ This example is not intended for use in production/shipping code. ⚠️
https://github.com/dotnet/roslyn-sdk/blob/main/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/ReferenceAssemblies.cs

@jinujoseph jinujoseph added Resolution-Answered The question has been answered and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 20, 2021
@jinujoseph jinujoseph added this to the Backlog milestone Apr 20, 2021
@Systo01
Copy link
Author

Systo01 commented Apr 20, 2021

Ok, so what would I have to use instead of these two

typeof(object).Assembly
typeof(Expression<Type>).Assembly

and how would I specify the references to the "type" argument and to DFM.EntityMap?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Interactive Resolution-Answered The question has been answered
Projects
None yet
Development

No branches or pull requests

3 participants