• 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; }
    
        }
    }
    View Code

     解决方案:

        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

  • 相关阅读:
    Mybatis plus 多表连接分页查询
    webstorm自动格式化.vue文件并符合Eslint
    Selenium python爬虫
    Cent OS防火墙配置端口开放
    开发Hexo主题(一)
    谷歌开发者主页回归
    个人博客网站
    linux搭建ftp
    putty之pscp上传文件
    送走了最好的兄弟 收到上交复试通知
  • 原文地址:https://www.cnblogs.com/noigel/p/14029474.html
Copyright © 2020-2023  润新知