前言
前段時(shí)間有朋友問道一個(gè)這樣的問題,.NET Core中如何通過Attribute的元數(shù)據(jù)信息來調(diào)用標(biāo)記的對應(yīng)方法。我第一時(shí)間想到的就是通過C#反射獲取帶有Custom Attribute標(biāo)記的類,然后通過依賴注入(DI)的方式獲取對應(yīng)服務(wù)的方法并通過反射動(dòng)態(tài)執(zhí)行類的方法,從而實(shí)現(xiàn)更靈活的編程方式。
C#中反射指的是什么?
開篇之前首先和大家簡單介紹一下反射的概念和作用。
在 C# 中,反射是指在運(yùn)行時(shí)動(dòng)態(tài)地獲取類型的信息并操作對象的能力。使用反射,我們可以在代碼中訪問程序集、模塊、成員等,并且可以操作這些成員的屬性、方法、字段和事件等。
自定義一個(gè)Attribute類型
/// <summary> /// 自定義一個(gè)Attribute類型 /// </summary> [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class CustomAttribute : Attribute { public string TargetMethod { get; set; } public CustomAttribute(string targetMethod) { TargetMethod = targetMethod; } }
定義如下兩個(gè)需要被執(zhí)行的服務(wù),并使用CustomAttribute標(biāo)記
/// <summary> /// 前進(jìn)服務(wù) /// </summary> [Custom("AdvanceWay")] public class AdvanceService { public void AdvanceWay() { Console.WriteLine("On the move!"); } } /// <summary> /// 后退服務(wù) /// </summary> [Custom("RetreatWay")] public class RetreatService { public void RetreatWay() { Console.WriteLine("Be retreating!"); } }
注冊需要注入的服務(wù)
var services = new ServiceCollection(); //注冊需要注入的服務(wù) services.AddTransient<AdvanceService>(); services.AddTransient<RetreatService>();
反射獲取所有帶有CustomAttribute特性的類并調(diào)用對應(yīng)方法
static void Main(string[] args) { var services = new ServiceCollection(); //注冊需要注入的服務(wù) services.AddTransient<AdvanceService>(); services.AddTransient<RetreatService>(); var provider = services.BuildServiceProvider(); #region 反射獲取所有帶有CustomAttribute特性的類并調(diào)用對應(yīng)方法 //反射獲取所有帶有CustomAttribute特性的類 var classes = Assembly.GetExecutingAssembly().GetTypes() .Where(type => type.GetCustomAttributes<CustomAttribute>().Any()); foreach (var clazz in classes) { //獲取標(biāo)記CustomAttribute的實(shí)例 var attr = clazz.GetCustomAttributes<CustomAttribute>().First(); //根據(jù)CustomAttribute元數(shù)據(jù)信息調(diào)用對應(yīng)的方法 var methodInfo = clazz.GetMethod(attr.TargetMethod); if (methodInfo != null) { //instance 對象是通過依賴注入容器獲取的。這是一種常用的實(shí)現(xiàn)方式,可以使用依賴注入解耦程序中各個(gè)組件之間的依賴關(guān)系,方便測試和維護(hù)。 var instance = provider.GetService(clazz); methodInfo.Invoke(instance, null); } } #endregion #region 反射獲取所有帶有CustomAttribute特性的類并調(diào)用指定方法 var executionMethod = "RetreatWay"; foreach (var clazz in classes) { //獲取標(biāo)記CustomAttribute的實(shí)例 var attr = clazz.GetCustomAttributes<CustomAttribute>().First(); if (attr.TargetMethod == executionMethod) { //根據(jù)CustomAttribute元數(shù)據(jù)信息調(diào)用對應(yīng)的方法 var methodInfo = clazz.GetMethod(attr.TargetMethod); if (methodInfo != null) { //instance 對象是通過依賴注入容器獲取的。這是一種常用的實(shí)現(xiàn)方式,可以使用依賴注入解耦程序中各個(gè)組件之間的依賴關(guān)系,方便測試和維護(hù)。 var instance = provider.GetService(clazz); methodInfo.Invoke(instance, null); } } } #endregion Console.ReadLine(); }
輸出如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-413579.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-413579.html
到了這里,關(guān)于.NET Core反射獲取帶有自定義特性的類,通過依賴注入根據(jù)Attribute元數(shù)據(jù)信息調(diào)用對應(yīng)的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!