目錄 枚舉的基本用法回顧 枚舉常見的設計模式運用 介紹 智能枚舉 代碼示例 業務應用 小結 枚舉的基本用法回顧 以下是一個常見的 C# 枚舉(enum)的示例: enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday ...
AccessReverser.GetAccess
獲得相關類的訪問級別
AccessReverser.GetAccess
AccessReverser.GetAccess方法中的參數可以是
屬性名稱 | 說明 |
---|---|
PropertyInfo | 獲取屬性的訪問級別 |
MethodInfo | 獲取方法的訪問級別 |
EventInfo | 獲取事件的訪問級別 |
FieldInfo | 獲取欄位的訪問級別 |
Type | 獲取類型的訪問級別 |
T | 獲取泛型的訪問級別 |
internal class Demo6
{
public String Field = "";
public void Domain()
{
NatashaManagement.Preheating();
//獲取訪問級別
String strAccess = AccessReverser.GetAccess<Object>();
// 獲取Type
Type ty = typeof(Demo6);
// 獲取Demo6的訪問級別
Console.WriteLine($"Type Access is {AccessReverser.GetAccess(ty)}");
// 獲取Field欄位
FieldInfo? fieldInfo = ty.GetField("Field");
if (fieldInfo != null) {
//輸出Field欄位的訪問級別
Console.WriteLine($"MethodText Access is {AccessReverser.GetAccess(fieldInfo)}");
}
// 獲取MethodText欄位
MethodInfo? domain = ty.GetMethod("Domain");
if (domain != null) {
//輸出MethodText欄位的訪問級別
Console.WriteLine($"MethodText Access is {AccessReverser.GetAccess(domain)}");
}
}
}
運行結果:
AvailableNameReverser.GetAvailableName
獲取Type的可用名,參數為Type
// 結果為:namespace+類名
Console.WriteLine($"獲取Type的可用名:{AvailableNameReverser.GetAvailableName(ty)}");
運行結果:
DeclarationReverser.GetMethodDeclaration
展示函數信息,參數為MethodInfo
public void Domain()
{
//MethodInfo? domain = ty.GetMethod("Domain");以下部分修改
// 獲取Test方法
MethodInfo? test = ty.GetMethod("Test");
if (test != null) {
Console.WriteLine($"函數信息: {DeclarationReverser.GetMethodDeclaration(test)}");
//輸出MethodText欄位的訪問級別
Console.WriteLine($"Test Access is {AccessReverser.GetAccess(test)}");
}
}
public String Test(in String arg1, out int arg2, ref float arg3) {
arg2 = 0;
return "";
}
結果截圖:
TypeNatashaExtension
Natasha的類型拓展
類名 | 參數 | 返回 | 說明 |
---|---|---|---|
IsImplementFrom | this Type ,Type iType | bool | 當前類是否實現了某介面,iType為介面類型 |
IsImplementFrom |
this Type | bool | T為介面類型 |
GetRuntimeName | this Type | string | 獲取運行時類名 |
GetDevelopName | this Type | string | 獲取完整類名 |
GetDevelopNameWithoutFlag | this Type | string | 同GetDevelopName |
GetAvailableName | this Type | string | 將類名替換成 文件名可使用的名字 |
IsSimpleType | this Type | bool | 判斷是否為值類型,字元串類型,委托類型,Type類型,及委托的子類型其中之一 |
//////////////////創建的介面
public interface ITest {
public void IClass();
}
internal class Demo6: ITest{
public void testTypeNatashaExtension() {
//初始化
NatashaManagement.Preheating();
bool bImplement = typeof(Demo6).IsImplementFrom(typeof(ITest));
// Class 類的情況
Console.WriteLine("Demo6類的相關結果如下:");
Console.WriteLine($"Demo6 是否實現了 ITest 的介面:{bImplement}");
Console.WriteLine($"GetRuntimeName 結果:\"{typeof(Demo6).GetRuntimeName()}\"");
Console.WriteLine($"GetDevelopName 結果:\"{typeof(Demo6).GetDevelopName()}\"");
Console.WriteLine($"GetDevelopNameWithoutFlag 結果:\"{typeof(Demo6).GetDevelopNameWithoutFlag()}\"");
Console.WriteLine($"GetAvailableName 結果:\"{typeof(Demo6).GetAvailableName()}\"");
Console.WriteLine($"IsSimpleType 結果:\"{typeof(Demo6).IsSimpleType()}\"");
// Dictionary<int, String>的參數
Console.WriteLine("Dictionary<int, String> 的相關結果如下:");
Console.WriteLine($"GetRuntimeName 結果:\"{typeof(Dictionary<int, String>).GetRuntimeName()}\"");
Console.WriteLine($"GetDevelopName 結果:\"{typeof(Dictionary<int, String>).GetDevelopName()}\"");
Console.WriteLine($"GetDevelopNameWithoutFlag 結果:\"{typeof(Dictionary<int, String>).GetDevelopNameWithoutFlag()}\"");
Console.WriteLine($"GetAvailableName 結果:\"{typeof(Dictionary<int, String>).GetAvailableName()}\"");
Console.WriteLine($"IsSimpleType 結果:\"{typeof(Dictionary<int, String>).IsSimpleType()}\"");
// Dictionary<String, HashSet<List<String>>>的參數
Console.WriteLine("Dictionary<String, HashSet<List<String>>> 的相關結果如下:");
Console.WriteLine($"GetRuntimeName 結果:\"{typeof(Dictionary<String, HashSet<List<String>>>).GetRuntimeName()}\"");
Console.WriteLine($"GetDevelopName 結果:\"{typeof(Dictionary<String, HashSet<List<String>>>).GetDevelopName()}\"");
Console.WriteLine($"GetDevelopNameWithoutFlag 結果:\"{typeof(Dictionary<String, HashSet<List<String>>>).GetDevelopNameWithoutFlag()}\"");
Console.WriteLine($"GetAvailableName 結果:\"{typeof(Dictionary<String, HashSet<List<String>>>).GetAvailableName()}\"");
Console.WriteLine($"IsSimpleType 結果:\"{typeof(Dictionary<String, HashSet<List<String>>>).IsSimpleType()}\"");
}
public void IClass()
{
Console.WriteLine("實現ITest的IClass方法");
}
}
結果截圖: