程序的属性部分中的错误。请帮忙 [英] An error in the attribute section of the program. Please help
本文介绍了程序的属性部分中的错误。请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#define DEBUG
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConditionalAttribute
{
public class MyClass : Attribute
{
[Conditional("DEBUG")]
public static void Message(string msg)
{
Console.WriteLine(msg);
}
}
class Test
{
static void function1()
{
MyClass.Message("In function 1.");
function2();
}
static void function2()
{
MyClass.Message("In function 2:");
}
public static void Main(string[] args)
{
MyClass.Message("In Main function. ");
function1();
Console.ReadKey();
}
}
}
推荐答案
几件事:
不要在C#中使用#define - 它不像在C或C ++中那样工作。 #define值只能在#if等中使用,不会影响普通代码
你导出MyClass
来自抽象类属性
- 但是你没有实现任何必需的方法。
既然你是初学者,只需删除推导:
Couple of things:
Don't use #define in C# - it doesn't work like it does in C or C++. #define values can only be used in #if and suchlike, not to affect "normal" code
You are derivingMyClass
from the abstract classAttribute
- but you aren't implementing any of the required methods.
Since you are a beginner, just remove the derivation:
public class MyClass
{
[Conditional("DEBUG")]
public static void Message(string msg)
{
Console.WriteLine(msg);
}
}
并确保您正在编译代码的调试版本。
自己学习不是一个好的解决方案:拿一本书,或者更好地继续学习 - 并从头到尾跟着它。试着去学习它是一种混淆自己的好方法,因为我认为你在这里。 C#与C和C ++共享许多类似的语法,但它是一种非常不同的语言,你不应该尝试使用C或C ++方法和思考,否则它会变得非常混乱!
And ensure you are compiling the debug version of your code.
"Learning it myself" is not a good solution: get a book, or better go on a course - and follow it from the beginning to the end. Just trying to learn it as you go along is a good way to confuse yourself, as I think you have here. C# shares a lot of similar syntax with C and C++, but it is a very different language under the hood, and you shouldn't try to use C or C++ methodology and thinking or it will get very confusing!
这篇关于程序的属性部分中的错误。请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文