Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当类继承Dictionary时,C#RoslynCompiler失败<;T1、T2>;_C#_Roslyn - Fatal编程技术网

当类继承Dictionary时,C#RoslynCompiler失败<;T1、T2>;

当类继承Dictionary时,C#RoslynCompiler失败<;T1、T2>;,c#,roslyn,C#,Roslyn,由于某种原因,当访问从字典继承的类时,.NETCore上的Roslyn似乎抛出了一个错误 CS0012:在未引用的程序集中定义了类型“Dictionary”。必须添加对程序集“System.Collections,Version=4.1.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用 我的语法树如下所示: using System; using System.Collections.Generic; using System.IO;

由于某种原因,当访问从
字典继承的类时,.NETCore上的Roslyn似乎抛出了一个错误

CS0012:在未引用的程序集中定义了类型“Dictionary”。必须添加对程序集“System.Collections,Version=4.1.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用

我的语法树如下所示:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using JQL.Core;

namespace RoslynCompileSample
{
    public class Writer
    {
        public object LinqIt(string outKey, Dictionary<string

            IEnumerable<TestClass>> db)
        {
            return db[outKey] = ({$linq
            });
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
运用系统反思;
使用System.Runtime.CompilerServices;
使用JQL.Core;
名称空间RoslynCompileSample
{
公开课作家
{
公共对象LinqIt(字符串输出键,字典数据库)
{
返回db[outKey]=({$linq
});
}
}
}
以及我的推荐人:

MetadataReference[] references = new MetadataReference[]
{
    MetadataReference.CreateFromFile(typeof(JQLQuery).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(System.Runtime.AssemblyTargetedPatchBandAttribute).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(DynamicAttribute).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(System.Collections.Generic.Dictionary<,>).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(System.Collections.Generic.Dictionary<string,object>).Assembly.Location)
};
MetadataReference[]references=新的MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(JQLQuery).Assembly.Location),
MetadataReference.CreateFromFile(typeof(object.Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Runtime.AssemblyTargetedPatchBandAttribute.Assembly.Location),
MetadataReference.CreateFromFile(typeof(DynamicAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Collections.Generic.Dictionary).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Collections.Generic.Dictionary).Assembly.Location)
};
我的班级

    public class TestClass : Dictionary<string, object>
    {
        public int age { get; set; }

        public TestClass()
        {
            this.age = 10;
        }
    }
公共类TestClass:Dictionary
{
公共整数{get;set;}
公共测试类()
{
这个年龄=10岁;
}
}
如果对象是从Roslyn SyntaxTree中定义的,则字典可以工作。如果传入任何字典或继承的对象,我就会得到错误


你知道这是什么原因吗?我的collections版本实际上是4.3.0,因此不确定它为什么要求更低的版本。

我最终通过手动引用系统解决了问题。它要求的collections dll如下:

string longName = "System.Collections, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";

Assembly assemCollec = Assembly.Load(longName);

MetadataReference[] references = new MetadataReference[]
{
    MetadataReference.CreateFromFile(assemCollec.Location),
    // Other Assemblies . . .

};

希望这能帮助其他遇到此问题的人。

在动态编译程序集(.Net Core 3.1)时也遇到同样的问题

代码如下:

public IEnumerable<object> GetMetadata()
{
    return _objects.Select(GetMetadata).ToList();
}
已返回System.Private.CoreLib.dll的位置

所以


提供了引用。

看起来像是您的软件包文件夹,或者您使用的程序集正在引用或查找错误的程序集。有时清除您的packages文件夹会有所帮助,好运
字典{K,T}
存在于
System.Private.CoreLib.dll
,因此
typeof(System.Collections.Generic.Dictionary).Assembly.Location
返回它的路径,当然,它不是
System.Collections.dll
。因此,您需要在
System.Collections.dll
上手动添加引用。但我有点困惑Roslyn想要系统的参考资料收集。。。你能从Roslyn发一条相应的诊断信息吗?这是我从Roslyn收到的诊断信息。CS0012:类型“Dictionary”是在未引用的程序集中定义的。您必须添加对程序集“System.Collections,Version=4.1.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。\nCS0021:无法将带[]的索引应用于类型为“TestClass”的表达式。\n我获取索引[]错误,因为它没有将我的类视为继承字典,这才是真正的问题。:\不幸的是,我的错误仍然存在:\“它没有将我的类视为继承字典“实际上,您在问题中没有显示必须从字典继承的类。请提供一个@GeorgeAlexandria,这是一个非常基本的类。只继承dictionary并有一个变量。这里发生的事情不多。有什么想法吗?
The type 'List<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
typeof(System.Collections.IList).Assembly
const string longName = "System.Collections, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";

var systemCollectionsAssembly = Assembly.Load(longName);