Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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
C# 消除预处理器命令的方法_C# - Fatal编程技术网

C# 消除预处理器命令的方法

C# 消除预处理器命令的方法,c#,C#,我需要一种方法来显示这个C#3.0代码: [TestMethod] #if NUNIT [Moled] #else [HostType("Moles")] #endif public void TestSomething() 在每个方法上不使用/不需要预处理器命令 这可能吗 在与同事交谈时,我们推测可能有一种方法可以创建一个具有2个构造函数(一个具有零参数,一个具有1个字符串)的属性类。然后在文件的顶部,我们在那里执行条件语句,如下所示: #if NUNIT

我需要一种方法来显示这个C#3.0代码:

[TestMethod]
#if NUNIT
        [Moled]
#else
        [HostType("Moles")]
#endif
public void TestSomething()
在每个方法上不使用/不需要预处理器命令

这可能吗


在与同事交谈时,我们推测可能有一种方法可以创建一个具有2个构造函数(一个具有零参数,一个具有1个字符串)的属性类。然后在文件的顶部,我们在那里执行条件语句,如下所示:

#if NUNIT
   Moled = MyNamespace.MyNewAttribute;
#else
   HostType = MyNamespace.MyNewAttribute;
#endif
[MyAttrib( "attrib arg" )]  
public void TestSomething()  
#if !NUNIT
using Moled = MyNameSpace.DummyAttribute;
#else
using HostType = MyNameSpace.DummyAttribute;
#endif
MyNewAttribute类将被设置为不执行任何操作,以便我可以使用以下方法进行编译:

[TestMethod]
[Moled]
[HostType("Moles")]
public void TestSomething()

这样行吗?如何编写此类?

当然,将其拆分为两个文件:

文件1:

[TestMethod]  
[Moled]  
public void TestSomething()
文件2:

[TestMethod]  
[HostType("Moles")]  
public void TestSomething()
使用适当的文件

更新:
啊,我想我终于明白你的目的了。
您并不是真的想按照原始问题的要求“消除预处理器指令”,实际上您想要做的是基于预处理器指令统一方法上的属性

是的,这可以通过一个神奇的
您可以将感兴趣的两个属性类重命名为相同的名称,如下所示:

#if NUNIT  
using MyAttrib = System.Diagnostics.ConditionalAttribute;  
#else  
using MyAttrib = System.ObsoleteAttribute;  
#endif  
然后你把所有的方法都装饰成这样:

#if NUNIT
   Moled = MyNamespace.MyNewAttribute;
#else
   HostType = MyNamespace.MyNewAttribute;
#endif
[MyAttrib( "attrib arg" )]  
public void TestSomething()  
#if !NUNIT
using Moled = MyNameSpace.DummyAttribute;
#else
using HostType = MyNameSpace.DummyAttribute;
#endif

当一个项目要通过Microsoft way进行单元测试时,我自己也做了同样的事情,但直到我们有了支持它的Visual Studio版本。

您更新的问题提供了一个有趣且(在我看来)可行的解决方案

要完成它,只需声明
MyNewAttribute
,这应该很简单:

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class MyNewAttribute : Attribute
{
    public MyNewAttribute() { }
    public MyNewAttribute(string dummy) { }
}
#if
技巧中,您必须使用完整的类名:

#if NUNIT
   using MoledAttribute = MyNamespace.MyNewAttribute;
#else
   using HostTypeAttribute = MyNamespace.MyNewAttribute;
#endif
注意:我不知道该怎么解决这些问题——你的问题在这一点上自相矛盾。请记住,您需要重新定义要禁用的,而不是要启用的

此外,这些使用语句的
必须在名称空间中的第一个,在所有类型声明之前。我尝试了这个方法,即使
MyNewAttribute
在同一个文件中进一步声明,它也能工作

由于这是一个非常不寻常的技巧,我强烈建议对
#if
构造进行解释性注释,以便您的代码的未来读者能够理解它的含义、为什么存在以及它是如何工作的

顺便说一句,由于自定义属性名称如此之短,并且很少或没有构造函数参数,我个人认为将它们放在一行更容易阅读:

[TestMethod, Moled, HostType("Moles")]
public void TestSomething()

你接近正确的轨道了。您只需要创建一个虚拟属性类,该类可以代替您尝试切换的属性:

public sealed class DummyAttribute : Attribute
{
    public DummyAttribute()
    {}

    pulic DummyAttribute(string dummy)
    {}
}
这两个构造函数是必需的,因为您尝试替换的属性之一接受字符串作为参数。设置虚拟构造函数允许您忽略正在替换的属性的实际行为

那么文件顶部的预处理器块应该如下所示:

#if NUNIT
   Moled = MyNamespace.MyNewAttribute;
#else
   HostType = MyNamespace.MyNewAttribute;
#endif
[MyAttrib( "attrib arg" )]  
public void TestSomething()  
#if !NUNIT
using Moled = MyNameSpace.DummyAttribute;
#else
using HostType = MyNameSpace.DummyAttribute;
#endif

我不明白你为什么需要别名。只需在一个文件中执行此操作:

#if NUNIT

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class HostTypeAttribute : Attribute
{
    public HostTypeAttribute(string dummy) { }
}

#else

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class MoledAttribute : Attribute
{
    public MoledAttribute() { }
}

#endif

然后,您可以在任何文件中同时使用这两个变量,除此处外,任何地方都没有预处理器定义。

您希望预处理器变量(如
NUNIT
)的“已定义”/“未定义”状态从何处读取?我们将其放入测试项目的条件编译设置中。我们确实在文件的开头使用预处理器命令,但我们不希望在每个方法之前都使用预处理器命令。这没有什么意义。决定要使用哪个单元测试工具。它们不是喷气背包。挑一个。谢谢你的回复,但我不知道这会有什么帮助。测试是相同的(TestSomething),只是测试上的属性不同。这不会让我重复我的测试吗?我希望这会导致很多功能的重复,并且需要更多的测试。是的,会的。避免这种情况的方法是使用#if#else和#endif,就像你在问题中所做的那样……请看一下我更新的问题。这就是我感兴趣的方向(尽管我不知道这是否可能)。@Vaccano:是的,那次更新让它更清晰了。我最初以为您正试图出于任何原因消除所有预处理器指令。现在它变得更有意义了。