标签:Console string C# System 特性 WriteLine using
特性是一种我们向程序的程序集增加元数据的语言结构。它是用于保存程序结构信息的某种特殊类型的类
将应用了特性的程序结构叫做目标
设计用来获取和使用元数据的程序(对象浏览器)叫做特性的消费者
#define IsShow
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace _13_特性
{
class Program
{
[Obsolete("这个方法已经被弃用了,请使用NewTest方法",false)]//特性:弃用 ,使用""能提示开发人员,后面true代表完全不能使用该方法,false是还能继续使用
static void Test()
{
Console.WriteLine("test");
}
static void NewTest()
{
Console.WriteLine("new test");
}
[Conditional("IsShow")]//使用该方法要先定义宏,当宏存在时就可以使用该函数,当宏不存在该函数也不会被使用
static void Show(string str)
{
Console.WriteLine(str);
}
//[CallerLineNumber]该特性是输出哪一行会调用这个方法
//[CallerFilePath]该特性是返回调用该函数的文件名字
//[CallerMemberName]该特性是返回的哪个函数用的这个方法
[DebuggerStepThrough]//该特性代表调试的时候会跳过这个函数内部
static void ShowLine(string str,[CallerLineNumber]int lineNumber = 0,[CallerFilePath]string file=null,[CallerMemberName]string main=null)
{
Console.WriteLine(str);
Console.WriteLine(lineNumber);
Console.WriteLine(file);
Console.WriteLine(main);
}
static void Main(string[] args)
{
Test();
Show("Start");
Console.WriteLine("Work");
Show("Emd");
ShowLine("123");
}
}
}
自定义特性
using System;
using System.Collections.Generic;
using System.Text;
namespace _14_自定义特性
{
[AttributeUsage(AttributeTargets.Class)]//运用系统自带的特性,表面这个我们自己写的特性只作用于类的
internal sealed class MyAttribute:Attribute
{
//开发者
public string developer;
//版本
public string version;
//描述主要作用
public string description;
public MyAttribute(string developer, string version, string description)
{
this.developer = developer;
this.version = version;
this.description = description;
}
}
}
using System;
namespace _14_自定义特性
{
[My("yuan","v1.1","测试")]
class Program
{
static void Main(string[] args)
{
Type t = typeof(Program);
bool result = t.IsDefined(typeof(MyAttribute), false);
object[] att = t.GetCustomAttributes(false);//用该方法得到一个所有特性的元素
}
}
}
标签:Console,string,C#,System,特性,WriteLine,using 来源: https://blog.csdn.net/m0_51743362/article/details/122630083
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。