我修改了昨天发布的新版Roslyn附带的示例,以使用动态和ExpandoObject,但我收到编译器错误,我不知道如何修复.错误是:
(7,21):错误CS0656:缺少编译器所需的成员'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
你能否在新的编译器中使用动态?我怎样才能解决这个问题?以下是我更新的示例:
[TestMethod]
public void EndToEndCompileAndRun()
{
var text = @"using System.Dynamic;
public class Calculator
{
public static object Evaluate()
{
dynamic x = new ExpandoObject();
x.Result = 42;
return x.Result;
}
}";
var tree = SyntaxFactory.ParseSyntaxTree(text);
var compilation = CSharpCompilation.Create(
"calc.dll",
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] {tree},
references: new[] {new MetadataFileReference(typeof (object).Assembly.Location), new MetadataFileReference(typeof (ExpandoObject).Assembly.Location)});
Assembly compiledAssembly;
using (var stream = new MemoryStream())
{
var compileResult = compilation.Emit(stream);
compiledAssembly = Assembly.Load(stream.GetBuffer());
}
Type calculator = compiledAssembly.GetType("Calculator");
MethodInfo evaluate …Run Code Online (Sandbox Code Playgroud) 我遇到了一个让我觉得3.0框架中存在错误的问题.当我尝试使用扩展方法时,我收到以下错误:
Missing compiler required member
'System.Runtime.CompilerServices.ExtensionAttribute..ctor'
Run Code Online (Sandbox Code Playgroud)
使用这个简单的代码时:
public static class StringUtils {
static void TestExtension(this String targetString) {
}
}
Run Code Online (Sandbox Code Playgroud)
使编译错误消失的唯一方法是添加以下代码:
namespace System.Runtime.CompilerServices {
public class ExtensionAttribute : Attribute { }
}
Run Code Online (Sandbox Code Playgroud)
我使用扩展方法已经有几个月,但我很确定我不必这样做.还有其他人遇到过这个问题吗?
我想这样做,但得到这个错误:
错误1无法定义新的扩展方法,因为无法找到编译器所需的类型"System.Runtime.CompilerServices.ExtensionAttribute".您是否缺少对System.Core.dll的引用?[剪了一些路径]
我在这里看到一些答案说,你必须自己定义这个属性.
我怎么做?
编辑:这就是我所拥有的:
[AttributeUsage ( AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method )]
public sealed class ExtensionAttribute : Attribute
{
public static int MeasureDisplayStringWidth ( this Graphics graphics, string text )
{
}
}
Run Code Online (Sandbox Code Playgroud) public static class Extensions{
public static void Dump<T>(this T o) { }
public static void Dump<T>(this T o, string s) { }}
Run Code Online (Sandbox Code Playgroud)
这些行允许我将代码从LINQPad复制到VS并运行它而不用.Dump()注释每一行但是这还不够...... http://code.google.com/p/linqpadvisualizer/ - 不太舒服: (
我在VS中搜索LINQPad的最佳结果是这个站点,下面是Pat Kujawa的代码.
using System.Diagnostics;
using System.IO;
public static class Extensions
{
public static void Dump<T>(this T o)
{
string localUrl = Path.GetTempFileName() + ".html";
using (var writer = LINQPad.Util.CreateXhtmlWriter(true))
{
writer.Write(o);
File.WriteAllText(localUrl, writer.ToString());
}
Process.Start(localUrl);
}
}
Run Code Online (Sandbox Code Playgroud)
但错误1当前上下文中不存在名称"LINQPad"
我在网上找不到任何LINQPad.dll
我有一个针对 .Net 4.0 的项目。它曾经建造到昨天。我查看了代码和引用的程序集。我可以看到定义的扩展属性。任何人都对此有任何线索。
我提到了以下问题,但没有帮助 - “缺少编译器所需的成员”错误被多次抛出,几乎没有对代码进行任何更改


* 更新 1: * 我看到 Npgsql 在同一个命名空间中定义 ExtensionAttribute。当我删除并添加旧版本的 Npgsql 时,它构建了。我应该怎么做才能使用最新的 Npgsql?
