Tag: 编译器错误

C#enum – 为什么*隐式*从0开始工作?

拿这段代码: enum En { val1, val2, } void Main() { En plop = 1; //error: Cannot implicitly convert type ‘int’ to ‘En’ En woop = 0; //no error } 当然,在为enum类型变量赋值1时会失败。 (打一个明确的演员,它会起作用。) 我的问题是:为什么分配0时不会失败?

Visual Studio注册表捕获实用程序已停止工作,在Windows7中编译C#项目时出错

Visual Studio注册表捕获实用程序已停止工作…. http://easycaptures.com/fs/uploaded/256/9376236710.png Windows 7每次编译项目时都会显示如下所示的构建错误。

密封关键字会影响编译器对强制转换的意见

我有一种情况,我希望编译器的行为解释。 给出一点代码: interface IFoo { T Get(); } class FooGetter : IFoo { public int Get() { return 42; } } 以下编译并运行: static class FooGetterGetter { public static IFoo Get() { return (IFoo)new FooGetter(); } } 如果我们更改Foo类的签名并添加sealed关键字: sealed class FooGetter : IFoo // etc 然后我在以下行得到编译器错误: return (IFoo)new FooGetter(); 的: 无法将类型’MyNamespace.FooGetter’转换为’MyNamespace.IFoo ‘ 有人可以解释一下有关sealed关键字的问题吗? 这是针对Visual Studio 2010中的.NET […]

如果将委托定义放在另一个项目中,则编译失败?

更新:我已经将此作为Microsoft Connect上的问题提交,如果您可以重现这一点和/或希望看到此修复,请帮助在那里投票解决问题。 我一直试图解决这个问题好几个小时了。 非常感谢您能想到的任何想法/建议。 首先,我有3个文件Class.cs Definitions.cs和Program.cs 。 我已经在http://pastie.org/1049492粘贴了文件内容供您试用。 问题是,如果在同一控制台应用程序项目中有所有3个文件。 应用程序编译并运行得很好。 但是,如果我在一个“库”项目中有Class.cs和Definitions.cs ,它从只有Program.cs文件的主控制台应用程序项目中引用,则编译失败: 代表Act没有2个论点。 无法将lambda表达式转换为委托类型’DC.Lib.Produce’,因为块中的某些返回类型不能隐式转换为委托返回类型… 这是一个包含3个项目的完整解决方案 – 1个将所有文件组合在一起,另一个项目放在另一个项目中: http://dl.dropbox.com/u/149124/DummyConsole.zip 我正在使用VS2010 RTW专业版。

在c#中获取关于不引用System.XML的编译错误,是System.XML正在引用

我正在尝试为我编写的旧程序添加新function。 但是,当试图让程序在VS express中构建时,它会向我发回错误消息。 错误1类型’System.Xml.Serialization.IXmlSerializable’在未引用的程序集中定义。 您必须添加对程序集’System.Xml,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089’的引用。 C:\ Path \ To \ File \ summaryForm.cs 101 18 SerialController 然而事情是在cs文件的顶部,它有 using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Xml; 任何想法为什么不识别XML引用?

‘method’没有重载匹配委托’System.EventHandler’

我正在尝试构建一个程序,一旦按下按钮,每5秒钟将执行该function(OnTimed)。 以下是目前的代码: private void bntCapture_Click(object sender, RoutedEventArgs e) { DispatcherTimer t1 = new DispatcherTimer(); t1.Interval = TimeSpan.FromMilliseconds(5000); t1.IsEnabled = true; t1.Tick += new EventHandler(OnTimed); t1.Start(); } void OnTimed(object sender, ElapsedEventArgs e) { imgCapture.Source = imgVideo.Source; System.Threading.Thread.Sleep(1000); Helper.SaveImageCapture((BitmapSource)imgCapture.Source); } 当我运行代码时,它显示错误: “’方法’没有重载匹配委托’System.EventHandler’

C#genericsinheritance问题

我想将从generics中派生的不同类型的对象添加到基类型列表中。 我得到这个编译错误 Error 2 Argument 1: cannot convert from ‘ConsoleApplication1.Stable’ to ‘ConsoleApplication1.ShelterBase’ C:\Users\ysn\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs 43 26 ConsoleApplication1 我看不出问题你能为我提供另一种做这种事情的方法吗? abstract class AnimalBase { public int SomeCommonProperty;} abstract class ShelterBase where T : AnimalBase { public abstract List GetAnimals(); public abstract void FeedAnimals(List animals); } class Horse : AnimalBase { } class Stable : ShelterBase { public override […]

为什么.NET委托不能被声明为静态?

当我尝试编译以下内容时: public static delegate void MoveDelegate (Actor sender, MoveDirection args); 我收到的是一个错误:“modifer’static’对此项无效。” 我在一个单例中实现它,有一个单独的类调用委托。 问题是,当我在另一个类中使用单例实例来调用委托时(来自标识符,而不是类型),我无论出于何种原因都不能这样做,即使我声明委托是非静态的。 显然,当且仅当委托是静态的时候,我才能直接通过类型引用它。 这背后的原因是什么? 我使用的是MonoDevelop 2.4.2。 更新 使用以下代码尝试其中一个建议后: public void Move(MoveDirection moveDir) { ProcessMove(moveDir); } public void ProcessMove(MoveDirection moveDir) { Teleporter.MoveMethod mm = new Teleporter.MoveMethod(Move); moveDelegate(this, moveDir); } 我收到了一个处理错误,指出MoveMethod必须是一个类型,而不是一个标识符。

如果多个成员具有相同的属性,如何抛出编译器错误

简单的问题,如何强制C#编译器抛出编译错误。 更新:也许最好使用Assert.Fail()代替? 我有一个自定义属性,只应该应用于一个类的一个成员。 在我的另一个类的静态方法中,如果有多个成员应用了该属性,它会查找该成员并希望它失败(不抛出exception)。 public class Foo { [MyCustomAttribute] public String FooString { get; set; } [MyCustomAttribute] public String OtherFooString { get; set; } } public class Bar where T : class, new() { static Bar() { //If more than one member of type Foo has MyCustomAttribute //applied to it compile error or Assert.Fail()? } […]

为什么C#限制可以声明为const的类型集?

编译器错误CS0283表示只能将基本POD类型(以及字符串,枚举和空引用)声明为const 。 有没有人有关于这种限制的理由的理论? 例如,能够声明其他类型的const值(例如IntPtr)会很好。 我相信const的概念实际上是C#中的语法糖,它只是用文字值替换了名称的任何用法。 例如,给定以下声明,在编译时对Foo的任何引用都将替换为“foo”。 const string Foo = “foo”; 这将排除任何可变类型,因此他们可能选择此限制而不必在编译时确定给定类型是否可变?