C# 知识点集锦(一)
例子一:已知一个类有两个属性,打印属性名的代码如下。要求屏蔽某个属性。
已知:

class Program { static void Main(string[] args) { PropertyInfo [] info=new Test().GetType().GetProperties(); foreach(PropertyInfo p in info) { Console.WriteLine(p.Name); } Console.ReadLine(); } } public class Test { public string One { get; set; } public string Two { get; set; } } }
解决方案:
class Program { static void Main(string[] args) { PropertyInfo[] info = new Test().GetType().GetFilteredProperties(); foreach(PropertyInfo p in info) { Console.WriteLine(p.Name); } Console.ReadLine(); } } public class SkipPropertyAttribute : Attribute { } public static class TypeExtensions { public static PropertyInfo[] GetFilteredProperties(this Type type) { return type.GetProperties().Where(pi => pi.GetCustomAttributes(typeof(SkipPropertyAttribute), true).Length == 0).ToArray(); } } public class Test { public string One { get; set; } [SkipProperty] public string Two { get; set; } }
或者
public static PropertyInfo[] GetFilteredProperties(this Type type) { return type.GetProperties() .Where(pi => !Attribute.IsDefined(pi, typeof(SkipPropertyAttribute))) .ToArray(); }
例子2:在XML中实现IF ELSE WHILE 等
https://stackoverflow.com/questions/6061470/if-then-else-using-xml
https://stackoverflow.com/questions/34653740/how-to-use-an-if-else-condition-in-a-sapui5-xml-view
https://stackoverflow.com/questions/46630446/how-to-implement-while-like-loop-in-xslt