Attributes
參考網站
using OnLineGame;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CustomizedAttributesDemo
{
class Program
{
static void Main(string[] args)
{
AttributeLinqSample();
Console.ReadLine();
}
static void AttributeLinqSample()
{
// Get all the Types which apply GamerB1Attribute
// IEnumerable 是一個 Collection
IEnumerable<System.Type> types = from t in Assembly.GetExecutingAssembly().GetTypes()
//where t.GetCustomAttributes<GamerB1Attribute>().Any() //這兩種寫法的意思是一樣的
where t.GetCustomAttributes<GamerB1Attribute>().Count() > 0
select t;
Console.WriteLine("****** 以下列出所有套用 GamerB1Attribute 的 Class ******");
foreach (Type t in types)
{
Console.WriteLine("Namespace:" + t.Namespace + "底下的:" + t.Name);
Console.WriteLine("包含所有的 Property 如下:");
foreach (PropertyInfo propertyInfo in t.GetProperties())
{
Console.WriteLine("型別:" + propertyInfo.PropertyType.Name + ", Property名稱:" + propertyInfo.Name);
}
Console.WriteLine("");
Console.WriteLine("包含所有的 Method 如下:");
foreach (MethodInfo methodInfo in t.GetMethods())
{
Console.WriteLine("型別:" + methodInfo.ReturnType.Name + ", Method名稱:" + methodInfo.Name);
}
Console.WriteLine("");
Console.WriteLine("以上這些 Property 中,僅有Apply GamerB1Attribute 的 Property 如下:");
IEnumerable<PropertyInfo> gamerB1AttributePropertyInfo = from pInfo in t.GetProperties()
where pInfo.GetCustomAttributes<GamerB1Attribute>().Any()
select pInfo;
foreach (PropertyInfo propertyInfo in gamerB1AttributePropertyInfo)
{
Console.WriteLine("型別:" + propertyInfo.PropertyType.Name + ", Method名稱:" + propertyInfo.Name);
}
Console.WriteLine("");
Console.WriteLine("以上這些 Method 中,僅有Apply GamerB1Attribute 的 Method 如下:");
IEnumerable<MethodInfo> gamerB1AttributeMethodInfo = from mInfo in t.GetMethods()
where mInfo.GetCustomAttributes<GamerB1Attribute>().Any()
select mInfo;
foreach (MethodInfo methodInfo in gamerB1AttributeMethodInfo)
{
Console.WriteLine("型別:" + methodInfo.ReturnType.Name + ", Method名稱:" + methodInfo.Name);
}
Console.WriteLine("");
}
}
}
}
namespace OnLineGame
{
//[AttributeUsage(AttributeTargets.All)] <-- 這行其實可以不用特別寫出來,因為預設就是這個
//[AttributeUsage(AttributeTargets.All)] 代表的意思是可以被用在任何地方
//例如:AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method | ...etc.)]
[AttributeUsage(AttributeTargets.All)]
public class GamerA1Attribute : Attribute
{
}
//也可以寫成 [GamerA1Attribute],但是就多此一舉
[GamerA1]
public class GamerA
{
#region Properties
[GamerA1]
public int GameScore { get; set; }
[GamerA1]
public string Name { get; set; }
#endregion
#region Methods
[GamerA1]
public override string ToString()
{
return $"GameScore : {GameScore} ; Name : {Name}";
}
public void NoAttributeMethod()
{
}
#endregion
}
// GamerB1Attribute 這個 Attribute 只能被用在 Class、Property、Method
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method)]
public class GamerB1Attribute : Attribute
{
#region Property
public string Name { get; set; }
public double Version { get; set; }
#endregion
}
// GamerB 這個 Class 套用了 GamerB1Attribute 這個 Attribute
// 同時宣告了2個 Attribute 的值
[GamerB1(Name = "GamerB", Version = 1.0)]
public class GamerB
{
#region Property
[GamerB1(Name = "GamerBMethod", Version = 1.0)]
public int GameScore { get; set; }
[GamerA1]
public string Name { get; set; }
#endregion
#region Methods
[GamerB1]
public override string ToString()
{
return $"GameScore : {GameScore} ; Name : {Name}";
}
public void NoAttributeMethod()
{
}
#endregion
}
[GamerB1(Name = "GamerB2", Version = 1.0)]
public class GamerB2
{
#region Property
[GamerB1(Name = "GamerB2Method", Version = 1.0)]
public int GameScore { get; set; }
[GamerA1]
public string Name { get; set; }
#endregion
#region Methods
[GamerB1]
public override string ToString()
{
return $"GameScore : {GameScore} ; Name : {Name}";
}
#endregion
}
}