我有几个不需要任何州的课程.从组织的角度来看,我想把它们放到层次结构中.
但似乎我无法为静态类声明继承.
像这样的东西:
public static class Base
{
}
public static class Inherited : Base
{
}
Run Code Online (Sandbox Code Playgroud)
不管用.
为什么语言的设计者会关闭这种可能性?
可以将扩展方法应用于类吗?
例如,扩展DateTime以包含可以调用的Tomorrow()方法,如:
DateTime.Tomorrow();
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用
static DateTime Tomorrow(this Datetime value) { //... }
Run Code Online (Sandbox Code Playgroud)
要么
public static MyClass {
public static Tomorrow() { //... }
}
Run Code Online (Sandbox Code Playgroud)
对于类似的结果,但我如何扩展DateTime以便我可以调用DateTime.Tomorrow?
Visual Studio Test可以使用ExpectedException属性检查预期的异常.您可以传递这样的异常:
[TestMethod]
[ExpectedException(typeof(CriticalException))]
public void GetOrganisation_MultipleOrganisations_ThrowsException()
Run Code Online (Sandbox Code Playgroud)
您还可以检查ExpectedException中包含的消息,如下所示:
[TestMethod]
[ExpectedException(typeof(CriticalException), "An error occured")]
public void GetOrganisation_MultipleOrganisations_ThrowsException()
Run Code Online (Sandbox Code Playgroud)
但是在测试I18N应用程序时,我会使用资源文件来获取该错误消息(如果我愿意,任何人甚至可能决定测试错误消息的不同本地化,但Visual Studio不允许我这样做:
[TestMethod]
[ExpectedException(typeof(CriticalException), MyRes.MultipleOrganisationsNotAllowed)]
public void GetOrganisation_MultipleOrganisations_ThrowsException()
Run Code Online (Sandbox Code Playgroud)
编译器将给出以下错误:
属性参数必须是属性的常量表达式,typeof表达式或数组创建表达式
有人知道如何测试具有资源文件消息的异常吗?
我考虑过的一个选项是使用自定义异常类,但基于经常听到的建议,例如:
"如果您有一个错误条件,可以以与任何其他现有异常不同的方式以编程方式处理,则创建并抛出自定义异常.否则,抛出一个现有异常." 资源
我不希望在正常流程中以不同方式处理异常(这是一个关键的例外,所以我无论如何都要进入恐慌模式)并且我认为为每个测试用例创建一个例外是不对的.任何意见?
在查看这个问题及其答案时,我认为为System.Console包含所需功能的扩展方法编写扩展方法是个好主意.
但是,当我尝试它时,我得到了这个编译错误
System.Console':静态类型不能用作参数
这是代码:
using System;
using System.Runtime.CompilerServices;
namespace ConsoleApplication1
{
public static class ConsoleExtensions
{
[Extension]
public static string TestMethod(this Console console, string testValue)
{
return testValue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否有另一种为静态类型创建扩展方法的方法?或者这是不可能的?
我目前正在为我的产品的DLL开发一个C#P/invoke包装器.我没有C#的经验,这是我做过的第一个重要的C#编码.我敏锐地意识到我对语言的细节和习语缺乏了解.
我的问题涉及我正在编写的单元测试,我使用的是NUnit.我需要比较double[]变量的值.如果我Assert.AreEqual(...)这样做,那么比较值是否完全相等.但是,我想比较宽容.AreEqual()允许delta参数的标量实数值存在重载.但是,我无法找到数组的等价物.我错过了一些明显的事吗?
目前我用以下代码解决了这个问题:
class Assert : NUnit.Framework.Assert
{
public static void AreEqual(double[] expected, double[] actual, double delta)
{
AreEqual(expected.Length, actual.Length);
for (int i = 0; i < expected.Length; i++)
{
AreEqual(expected[i], actual[i], delta);
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这似乎有效,但我想知道是否有更清洁的解决方案.特别值得我担心的是,对于我的派生类使用相同的名称,不仅风格很差,而且可能会导致未预料到的问题.
我想要使用扩展方法,但我知道它们只适用于扩展时有类的实例.当然,我只在Assert类上调用静态方法.
我很抱歉,如果这看起来有点模糊,但我的直觉告诉我,我不是这样做的最佳方式,我想知道如何做对.
可能重复:
我可以向现有静态类添加扩展方法吗?
我知道我可以通过以下方式扩展课程.我有一个静态类,我想扩展.我怎么能这样做?我想写ClassName.MyFunc()
static public class SomeName
{
static public int HelperFunction(this SomeClass v)
Run Code Online (Sandbox Code Playgroud) 我正在关注MVC音乐商店教程,但我在第5 部分第5部分中遇到了Html Helper .
到目前为止,我似乎已经正确地遵循它(如果我错了请纠正我:))...但是我收到以下错误:
'musicStoreMVC.Helpers.HtmlHelper':静态类型不能用作参数
这是我的应用程序的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace musicStoreMVC.Helpers
{
public static class HtmlHelper
{
public static string Truncate(this HtmlHelper helper, string input, int length)
{
if (input.Length <= length)
{
return input;
}
else
{
return input.Substring(0, length) + "...";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人能看到我做错了什么,或者如果需要更多信息,我会很感激指针!谢谢.
我想知道我是否可以使用扩展方法或其他技术扩展静态类
System.Net.Mime.MediaTypeNames.Image,它的类型比我需要的少.
c# ×9
.net ×1
asp.net-mvc ×1
console ×1
exception ×1
html-helper ×1
inheritance ×1
linq ×1
static ×1
unit-testing ×1