⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multipleattributes.cs

📁 vc的原码例子12 vc的原码例子12
💻 CS
字号:
using System;
using System.Reflection;

class FileandDirectoryAttribute: Attribute
{
	public FileandDirectoryAttribute(string directory, string file)
	{ 
		this.Directory = directory;
		this.File = file;
	}

	protected string directory;
	public String Directory
	{
		get { return(directory); }
		set { directory = value; }
	}

	protected string file;
	public String File
	{
		get { return(file); }
		set { file = value; }
	}
}


class VersionInformationAttribute: Attribute
{
	public VersionInformationAttribute(string version)
	{ 
		this.version = version;
	}

	public string version;
}

class ProgrammerInformationAttribute: Attribute
{
	public ProgrammerInformationAttribute(string name, string date)
	{ 
		this.name = name;
		this.date = date;
	}

	public string name;
	public string date;
}


[VersionInformation("1.0A")]
[ProgrammerInformation("Jamsa", "9/30/02")]
class SomeClass
{
	public SomeClass(string value)
	{
		SessionFile = value;
	}
	[FileandDirectory("USER_SESSION", "SESSION_ID.txt")]
	public string SessionFile;
}


public class MultipleAttribute
{
	public static void Main()  
	{
		SomeClass Demo = new SomeClass("C:\\USER_SESSION\\SESSION_ID.txt");

		Type type = typeof(SomeClass);

		foreach (Attribute attr in type.GetCustomAttributes(true))
		{
			if (attr is ProgrammerInformationAttribute)
			{
				Console.WriteLine("{0} {1}", attr, ((ProgrammerInformationAttribute)attr).name);
				Console.WriteLine("{0} {1}", attr, ((ProgrammerInformationAttribute)attr).date);
			}  
			else if (attr is VersionInformationAttribute)
				Console.WriteLine("{0} {1}", attr, ((VersionInformationAttribute)attr).version);
		}  

		foreach(FieldInfo field in type.GetFields())
		{
			foreach (Attribute attr in field.GetCustomAttributes(true))
			{
				if (attr is FileandDirectoryAttribute)
				{
					Console.WriteLine("{0} {1}", attr, ((FileandDirectoryAttribute)attr).Directory);
					Console.WriteLine("{0} {1}", attr, ((FileandDirectoryAttribute)attr).File);
				}
			}  
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -