在編寫lambda-method語法時,我想要一個函數,讓所有具有屬性的類打印每個類名稱及其屬性。C#在linq中創建本地字符串變量 - lambda表達式
var x1 = Assembly.GetExecutingAssembly()
.GetTypes()
.Select(type => type
.GetCustomAttributes(false)
.Select(attribute => type.Name + " - " + attribute.ToString())
.ToList())
.ToList();
x1.ForEach(type => type.ForEach(str => Console.WriteLine(str)));
輸出:
<ClassName> - <ProgramName>.<AttributeName>
<ClassName> - <ProgramName>.<AttributeName>
<>c__DisplayClass6 - System.Runtime.CompilerServices.CompilerGeneratedAttribute
據我所知,在輸出中的最後一行,是因爲使用本地lambda表達式變量。
如何避免顯示最後一行?
你可以檢查if(type.IsDefined(typeof(CompilerGeneratedAttribute),false))並跳過它。 – riteshmeher
謝謝!有用。 –
很高興爲您效勞! – riteshmeher