C#高级语法 Attribute特性详解和类型,方法,变量附加特性讲解

news2025/5/26 1:14:27

前言

Attribute特性是一种高级的语法,在C#和Java中用的比较多。如果你想要了解特性,就需要先了解反射。

相关资料

【C#进阶】C# 特性

C# 官方文档 创建自定义特性

C# 官方文档 使用反射访问特性

C#基础教程 Attribute 特性与反射案例详解,自动化识别与使用类型!

C#基础教程 Reflection应用,简单使用反射,打破常规!

Attribute特性

Attribute是一个简单的语法,一般来说都是放在类/变量/函数的前面,当然也可以放在参数里面。不过我们这里主要讨论常用的三种情况:

  • 变量
  • 方法

个人原理理解

找到带有Attribute特性的类
Assembly程序集,存放所有的编译信息
所有的Class类名
带有Attribute特性的类
Type
Attribute特性
...等等
MethodInfo方法属性
Attribute
PropertyInfo变量属性

特性的声明与使用

【C#进阶】C# 特性

在这里插入图片描述

简单的特性声明

namespace NetCore.Models
{

    /// <summary>
    /// 特性需要以MyAttributeTestAttribute结尾,这个是约定
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    
    public class MyAttributeTestAttribute:Attribute
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public MyAttributeTestAttribute(string name) {
            Name = name;
        }

        public MyAttributeTestAttribute()
        {


        }
    }
}

在这里插入图片描述

为了方便后面的讲解,我们这里声明三个特性

namespace NetCore.Models
{
    /// <summary>
    /// 类型特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class MyClassAttribute:Attribute
    {
        public string Name { get; set; }
        public MyClassAttribute(string name) {
            Name = name;
        }
        public MyClassAttribute() { }
    }
}


namespace NetCore.Models
{
    /// <summary>
    /// 方法特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class MyMethodAttribute:Attribute
    {
        public string Name { get; set; }

        public MyMethodAttribute(string name) {
            Name = name;
        }
        public MyMethodAttribute() { }
    }
}

namespace NetCore.Models
{
    /// <summary>
    /// 参数特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class MyPropertyAttribute:Attribute
    {
        public string Name { get; set; }
        public MyPropertyAttribute(string name) {
            Name = name;
        }
		public MyPropertyAttribute() { }
    }
}


类型特性

我们声明三个类,去获取这三个类的MyClass特性
其它两个设置差不多

在这里插入图片描述
在这里插入图片描述

  static void Main(string[] args)
 {
     var list = GetAllTypes<MyClassAttribute>();
     for (var i = 0; i < list.Count; i++)
     {
         Console.WriteLine($"ClassName:[{list[i].Name}],Attribute.Name[{GetAttribute<MyClassAttribute>(list[i]).Name}]");
     }
     Console.WriteLine("运行完成!");
     Console.ReadKey();
 }

 /// <summary>
 /// 获取所有有T特性的类型
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static List<Type> GetAllTypes<T>() where T : Attribute
 {
     var res = new List<Type>();
     //Assembly存放所有的程序集
     res = Assembly.GetExecutingAssembly()
         .GetTypes()
         .Where(t => t.GetCustomAttributes(typeof(T), false).Any())//我们找到所有程序集中带有T特性的Type类型
         .ToList();
     return res;
 }
 /// <summary>
 /// 获取
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="model"></param>
 /// <returns></returns>
 public static T GetAttribute<T>(Type model) where T : Attribute, new()
 {
     var res = new T();
     res = model.GetCustomAttribute<T>();
     return res;
 }

运行结果:

在这里插入图片描述

找到类的Attribute属性

如果只是找到带有Attribute的属性,那么意义就不大了,那么目前就只有一个标记的功能。这里我们将对应Attribute属性取到

 static void Main(string[] args)
 {
     var list = GetAllTypes<MyClassAttribute>();
     for (var i = 0; i < list.Count; i++)
     {
         Console.WriteLine($"ClassName:[{list[i].Name}],Attribute.Name[{GetAttribute<MyClassAttribute>(list[i]).Name}]");
     }
     Console.WriteLine("运行完成!");
     Console.ReadKey();
 }

 /// <summary>
 /// 获取所有有T特性的类型
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static List<Type> GetAllTypes<T>() where T : Attribute
 {
     var res = new List<Type>();
     //Assembly存放所有的程序集
     res = Assembly.GetExecutingAssembly()
         .GetTypes()
         .Where(t => t.GetCustomAttributes(typeof(T), false).Any())//我们找到所有程序集中带有T特性的Type类型
         .ToList();
     return res;
 }
 /// <summary>
 /// 获取
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="model"></param>
 /// <returns></returns>
 public static T GetAttribute<T>(Type model) where T : Attribute, new()
 {
     var res = new T();
     res = model.GetCustomAttribute<T>();
     return res;
 }

在这里插入图片描述

方法特性和变量特性

现在会了类型特性,那么方法特性和变量特性也差不多了。我们这里直接将对应的代码封装一下

代码封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NetCore.Utils
{
    public static class MyAttributeHelper
    {

        /// <summary>
        /// 获取该类型下所有的带Attribute的方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<MethodInfo> GetAllMethods<T>(Type type) where T : class, new()
        {
            var res = new List<MethodInfo>();
            res = type.GetMethods().Where(t => t.GetCustomAttributes(typeof(T), false).Any()).ToList();
            return res;
        }

        /// <summary>
        /// 获取该类型下所有的带Attribute的属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<PropertyInfo> GetAllPropertys<T>(Type type) where T : class, new()
        {
            var res = new List<PropertyInfo>();
            res = type.GetProperties().Where(t => t.GetCustomAttributes(typeof(T), false).Any()).ToList();
            return res;
        }
        /// <summary>
        /// 获取程序集所有有T特性的类型class
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<Type> GetAllTypes<T>() where T : Attribute
        {
            var res = new List<Type>();
            //Assembly存放所有的程序集
            res = Assembly.GetExecutingAssembly()
                .GetTypes()
                .Where(t => t.GetCustomAttributes(typeof(T), false).Any())//我们找到所有程序集中带有T特性的Type类型
                .ToList();
            return res;
        }
        /// <summary>
        /// 获取特性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T GetAttribute<T>(Type type) where T : Attribute, new()
        {
            var res = new T();
            res = type.GetCustomAttribute<T>();
            return res;
        }

        /// <summary>
        /// 获取特性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T GetAttribute<T>(MethodInfo type) where T : Attribute, new()
        {
            var res = new T();
            res = type.GetCustomAttribute<T>();
            return res;
        }

        /// <summary>
        /// 获取特性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T GetAttribute<T>(PropertyInfo type) where T : Attribute, new()
        {
            var res = new T();
            res = type.GetCustomAttribute<T>();
            return res;
        }
    }
}

测试类

TestService1

namespace NetCore.Services
{
    [MyClass("TestServiceAttribute1")]
    public class TestService1
    {

        [MyProperty("TestService1的Property")]
        public string Name { get; set; }

        public int Id { get; set; }

        [MyMethod("TestService1的Method")]
        public void Test()
        {

        }

        public void Send()
        {

        }
    }
}
TestService2
using NetCore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCore.Services
{
    [MyClass("TestServiceAttribute2")]
    public class TestService2
    {
    }
}

TestService3
using NetCore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCore.Services
{
    [MyClass("TestServiceAttribute3")]
    public class TestService3
    {
    }
}

测试代码


        static void Main(string[] args)
        {
            //找到所有带MyClassAttribute特性的类
            var list = MyAttributeHelper.GetAllTypes<MyClassAttribute>();
            for (var i = 0; i < list.Count; i++)
            {
                Console.WriteLine($"ClassName:[{list[i].Name}],Attribute.Name[{MyAttributeHelper.GetAttribute<MyClassAttribute>(list[i]).Name}]");
                //找到所有带MyMethodAttribute的方法
                var methods = MyAttributeHelper.GetAllMethods<MyMethodAttribute>(list[i]);
                //找到所有带MyPropertyAttribute的方法
                var propertis = MyAttributeHelper.GetAllPropertys<MyPropertyAttribute>(list[i]);

                //对代码进行打印
                foreach (var item in methods)
                {
                    var att = MyAttributeHelper.GetAttribute<MyMethodAttribute>(item);
                    Console.WriteLine($"ClassName:[{list[i].Name}],Method:[{item.Name}],Attribute.Name[{att.Name}]");
                }
                foreach (var item in propertis)
                {
                    var att = MyAttributeHelper.GetAttribute<MyPropertyAttribute>(item);

                    Console.WriteLine($"ClassName:[{list[i].Name}],Property:[{item.Name}],Attribute.Name[{att.Name}]");
                }
            }
            Console.WriteLine("运行完成!");
            Console.ReadKey();
        }

运行结果

在这里插入图片描述

对封装的代码进行优化

我们获取带有Attribute的类的时候,肯定希望一个函数直接返回两个结果:

  • Type:那个带Attribute的类
  • Attribute:Attribute本身的属性

一个函数返回多个变量有许多种解决方案,我这里觉得使用ValueTuple更加的合适。

C# 元祖,最佳的临时变量。

封装代码

......其它代码
 /// <summary>
 /// 返回带有Attribute的类型元祖列表
 /// </summary>
 /// <typeparam name="Att"></typeparam>
 /// <returns></returns>
 public static List<(Type type, Att att)> GetAll_TypeAndAtt<Att>() where Att : Attribute, new()
 {
     var res = new List<(Type type, Att att)> ();
     var typeLists = GetAllTypes<Att>();
     foreach (var item in typeLists)
     {
         var att = GetAttribute<Att>(item);
         res.Add((item, att));   
     }
     return res;
 }

 /// <summary>
 /// 返回带有Attribute的变量元祖列表
 /// </summary>
 /// <typeparam name="Att"></typeparam>
 /// <param name="type"></param>
 /// <returns></returns>
 public static List<(PropertyInfo property, Att att)> GetAll_PropertyAndAtt<Att>(Type type) where Att : Attribute, new()
 {
     var res = new List<(PropertyInfo type, Att att)>();
     var typeLists = GetAllPropertys<Att>(type);
     foreach (var item in typeLists)
     {
         var att = GetAttribute<Att>(item);
         res.Add((item, att));
     }
     return res;
 }

 /// <summary>
 /// 返回带有Attribute的方法元祖列表
 /// </summary>
 /// <typeparam name="Att"></typeparam>
 /// <param name="type"></param>
 /// <returns></returns>
 public static List<(MethodInfo method, Att att)> GetAll_MethodAndAtt<Att>(Type type) where Att : Attribute, new()
 {
     var res = new List<(MethodInfo type, Att att)>();
     var typeLists = GetAllMethods<Att>(type);
     foreach (var item in typeLists)
     {
         var att = GetAttribute<Att>(item);
         res.Add((item, att));
     }
     return res;
 }

测试代码


            var lists = MyAttributeHelper.GetAll_TypeAndAtt<MyClassAttribute>();
            lists.ForEach(item1 =>
            {
                Console.WriteLine($"ClassName:[{item1.type.Name}],Attribute.Name[{item1.att.Name}]");
                var methods = MyAttributeHelper.GetAll_MethodAndAtt<MyMethodAttribute>(item1.type);
                var properties = MyAttributeHelper.GetAll_PropertyAndAtt<MyPropertyAttribute>(item1.type);
                methods.ForEach(item2 => { Console.WriteLine($"ClassName:[{item1.type.Name}],Method:[{item2.method.Name}],Attribute.Name[{item2.att.Name}]"); });
                properties.ForEach(item2 => { Console.WriteLine($"ClassName:[{item1.type.Name}],Method:[{item2.property.Name}],Attribute.Name[{item2.att.Name}]"); });

            });

运行结果(和上次的一致)

在这里插入图片描述

最后封装好的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NetCore.Utils
{
    public static class MyAttributeHelper
    {

        /// <summary>
        /// 获取该类型下所有的带Attribute的方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<MethodInfo> GetAllMethods<T>(Type type) where T : class, new()
        {
            var res = new List<MethodInfo>();
            res = type.GetMethods().Where(t => t.GetCustomAttributes(typeof(T), false).Any()).ToList();
            return res;
        }

        /// <summary>
        /// 获取该类型下所有的带Attribute的属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<PropertyInfo> GetAllPropertys<T>(Type type) where T : class, new()
        {
            var res = new List<PropertyInfo>();
            res = type.GetProperties().Where(t => t.GetCustomAttributes(typeof(T), false).Any()).ToList();
            return res;
        }
        /// <summary>
        /// 获取程序集所有有T特性的类型class
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<Type> GetAllTypes<T>() where T : Attribute
        {
            var res = new List<Type>();
            //Assembly存放所有的程序集
            res = Assembly.GetExecutingAssembly()
                .GetTypes()
                .Where(t => t.GetCustomAttributes(typeof(T), false).Any())//我们找到所有程序集中带有T特性的Type类型
                .ToList();
            return res;
        }
        /// <summary>
        /// 获取特性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T GetAttribute<T>(Type type) where T : Attribute, new()
        {
            var res = new T();
            res = type.GetCustomAttribute<T>();
            return res;
        }

        /// <summary>
        /// 获取特性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T GetAttribute<T>(MethodInfo type) where T : Attribute, new()
        {
            var res = new T();
            res = type.GetCustomAttribute<T>();
            return res;
        }

        /// <summary>
        /// 获取特性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T GetAttribute<T>(PropertyInfo type) where T : Attribute, new()
        {
            var res = new T();
            res = type.GetCustomAttribute<T>();
            return res;
        }

        /// <summary>
        /// 返回带有Attribute的类型元祖列表
        /// </summary>
        /// <typeparam name="Att"></typeparam>
        /// <returns></returns>
        public static List<(Type type, Att att)> GetAll_TypeAndAtt<Att>() where Att : Attribute, new()
        {
            var res = new List<(Type type, Att att)> ();
            var typeLists = GetAllTypes<Att>();
            foreach (var item in typeLists)
            {
                var att = GetAttribute<Att>(item);
                res.Add((item, att));   
            }
            return res;
        }

        /// <summary>
        /// 返回带有Attribute的变量元祖列表
        /// </summary>
        /// <typeparam name="Att"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<(PropertyInfo property, Att att)> GetAll_PropertyAndAtt<Att>(Type type) where Att : Attribute, new()
        {
            var res = new List<(PropertyInfo type, Att att)>();
            var typeLists = GetAllPropertys<Att>(type);
            foreach (var item in typeLists)
            {
                var att = GetAttribute<Att>(item);
                res.Add((item, att));
            }
            return res;
        }

        /// <summary>
        /// 返回带有Attribute的方法元祖列表
        /// </summary>
        /// <typeparam name="Att"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<(MethodInfo method, Att att)> GetAll_MethodAndAtt<Att>(Type type) where Att : Attribute, new()
        {
            var res = new List<(MethodInfo type, Att att)>();
            var typeLists = GetAllMethods<Att>(type);
            foreach (var item in typeLists)
            {
                var att = GetAttribute<Att>(item);
                res.Add((item, att));
            }
            return res;
        }
    }
}

总结

Attribute是C# 的高级语法,使用范围很广,可以极大的简化我们需要运行代码。本篇文章只是讲解了如何拿到特性属性,如果我们需要深入解决,那么就需要深入了解反射的使用方法。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1355725.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

雾天条件下 SLS 融合网络的三维目标检测

论文地址&#xff1a;3D Object Detection with SLS-Fusion Network in Foggy Weather Conditions 论文代码&#xff1a;https://github.com/maiminh1996/SLS-Fusion 论文摘要 摄像头或激光雷达&#xff08;光检测和测距&#xff09;等传感器的作用对于自动驾驶汽车的环境意识…

3D 纹理的综合指南

在线工具推荐&#xff1a;3D数字孪生场景编辑器 - GLTF/GLB材质纹理编辑器 - 3D模型在线转换 - Three.js AI自动纹理开发包 - YOLO 虚幻合成数据生成器 - 三维模型预览图生成器 - 3D模型语义搜索引擎 我们经常看到超现实主义的视频游戏和动画电影角色出现在屏幕上。他们皮肤上的…

在win10上cuda12+tensorrt8.6+vs2019环境下编译paddle2.6生成python包与c++推理库

paddle infer官方目前没有发布基于cuda12的c库&#xff0c;为此参考https://www.paddlepaddle.org.cn/inference/user_guides/source_compile.html实现cuda12的编译安装&#xff0c;不料博主才边缘好自己的paddle2.6&#xff0c;paddle官方已经发布了cuda12.0的paddle2.6框架。…

嵌入式MCU:如何安装codeWarrior 和Jlink

先安装codeWarrior 15.0版本,这个官网上没有这个版本要去blazar的这个网站上下载: Blazar-α系统电路图纸(MOOC课程对应)(Updating)-Blazar开源硬件与MOOC codeWarrior 安装不要安装在中文路径里面 安装完了codeWarrior 再安装Jlink 然后再装Jlink 这个也是从上面的…

Android 内容生成pdf文件

1.引入itext7 implementation com.itextpdf:itext7-core:7.1.13上面比较大&#xff0c;可以直接下载需要集成的jar包 implementation files(libs\\layout-7.1.13.jar) implementation files(libs\\kernel-7.1.13.jar) implementation files(libs\\io-7.1.13.jar) implementatio…

《MySQL系列-InnoDB引擎04》MySQL表相关介绍

文章目录 第四章 表1 索引组织表2 InnoDB逻辑存储结构2.1 表空间2.2 段2.3 区2.4 页2.5 行2.6 拓展&#xff1a;MySQL的varchar(n)能存储几个字符&#xff1f;占多少字节&#xff1f; 3 InnoDB行记录格式4 文件格式5 约束5.1 数据完整性5.2 约束的创建和查找5.3 约束和索引的区…

如何为项目创建高效的项目进度表?

项目管理是一项负有巨大责任的工作&#xff0c;涉及到完成项目所需的大量流程和任务。如果没有任务和责任的线路图&#xff0c;很容易就偏离方向&#xff0c;无法了解项目每个阶段需要完成的任务。这就是为什么项目进度表是成功执行项目的核心所在。 什么是项目进度表&#xff…

数据分析-24-母婴产品电商可视化分析(包含代码数据)

文章目录 0. 代码数据获取1. 项目1.1 项目介绍1.2 分析目的1.3 分析思路 2. 数据集介绍2.1 数据信息2.2 字段含义 3. 数据清洗3.1 导入包和查看数据3.2 查看列的信息3.3 查看表平均值这些3.4 查出重复的user_id3.5 清洗buy_mount列 4. 针对目的进行分析4.1 销量数量前10的类别I…

T40N 君正智能处理器T40 BGA 芯片

T40N是一款智能视频应用处理器&#xff0c;适用于移动摄像机、安防等视频设备调查、视频聊天、视频分析等。该SoC引入了一种创新的体系结构满足高性能计算和高质量图像和视频编码的要求通过视频设备解决。T40N提供高速CPU计算能力&#xff0c;出色的图像信号过程中&#xff0c;…

ensp vlan连接(详细)

1.将需要的设备放置好 2.将设备连接起来 3.启动所有设备 4.备注好每台PC机的信息 5.配置好每台PC机 6.配置交换机1 进入配置视图&#xff0c;关闭信息提示 重命名设备 批量创建VLAN 开始配置接口 更改接口类型为ACCESS 将接口划分到对应的VLANN 配置下一个接口&#xff0c;步…

JavaScript高级程序设计读书记录(一):语言基础,语法,变量,数据类型

1. 语法 很大程度上借鉴了 C 语言和其他类 C 语言&#xff0c;如 Java 和 Perl. 1.1 区分大小写 1.2 标识符 第一个字符必须是一个字母、下划线&#xff08;_&#xff09;或美元符号&#xff08;$&#xff09;&#xff1b; 剩下的其他字符可以是字母、下划线、美元符号或数…

qiankun 公共依赖

1、提取公共依赖的目的 减少相同资源的重复加载资源版本不同步打包文件庞大2、如何提取公共依赖 基本思路&#xff1a;1、相同依赖 采用 CDN 的方式加载&#xff0c;并把 所有依赖的 CDN 链接 统一放到一个文件中进行管理 2、把存放 CDN 链接的文件&#xff0c;引入到 vue.conf…

NFT 项目入驻 NFTScan Site 流程说明

NFTScan Site 是由数据基础设施 NFTScan 推出的功能强大的 NFT 项目管理平台。NFTScan Site 主要为 NFT Collection、NFT Marketplace、NFTFi 以及其他 NFT 生态项目提供专业的项目管理后台服务和链上数据分析追踪服务。 NFTScan Site 功能&#xff1a; 1&#xff09;项目信息编…

华硕ASUS RT-AC1200 pandavan老毛子 128M DDR固件

原版硬件只支持64M DDR2&#xff0c;更换了128M内存&#xff0c;结果找不到对应的固件&#xff0c;而且全部都是英文版的 所以自己编译了中文版的pandavan老毛子&#xff0c;下载位置可能资源审核中&#xff1a;

基于Kettle开发的web版数据集成开源工具(data-integration)-部署篇

目录 &#x1f4da;第一章 前言&#x1f4d7;背景&#x1f4d7;目的&#x1f4d7;总体方向 &#x1f4da;第二章 下载编译&#x1f4d7;下载&#x1f4d7;编译 &#x1f4da;第三章 部署&#x1f4d7;准备工作&#x1f4d5; 安装数据库&redis&consul&#x1f4d5; 修改…

树莓派控制继电器(IO的配置和使用)

一、硬件函数初始化 int wiringPiSetup(void)返回&#xff1a;执行状态&#xff0c;-1表示失败 当使用这个函数初始化树莓派引脚时&#xff0c;程序使用的是wiringPi 引脚编号表。引脚的编号为 0~16 需要root权限 二、配置IO口的模式 void pinMode (int pin, int …

算法31:针对算法30货币问题进行拓展 + 时间复杂度 + 空间复杂度优化--------从左往右尝试模型

在算法30中&#xff0c;我们说过从左往右尝试模型&#xff0c;口诀就是针对固定集合&#xff0c;就是讨论要和不要的累加和。 那么对于非固定集合&#xff0c;我们应该怎么做呢&#xff1f; 针对非固定集合&#xff0c;面值固定&#xff0c;张数不固定。口诀就是讨论要与不要…

URLConnection()和openStream()两个方法产生SSRF的原理和修复方法

今年是自主研发的第三个年份&#xff0c;也是重视安全的年份。 转一篇小文章&#xff1a; 0x00 前言 SSRF 形成的原因大都是由于服务端提供了从其他服务器应用获取数据的功能且没有对目标地址做过滤与限制。比如从指定 URL 地址获取网页文本内容&#xff0c;加载指定地址的图…

《算法导论》复习——CHP1、CHP2 算法基础

基本定义&#xff1a; 算法是一组有穷的规则&#xff0c;规定了解决某一特定类型问题的一系列运算。 关心算法的正确性和效率。 算法的五个重要特性&#xff1a;确定性、能行性、输入、输出、有穷性。 基础方法&#xff1a; 伪代码&#xff08;Pseudocode&#xff09;&#xff…

C# .Net学习笔记—— 异步和多线程(Task)

一、概念 Task是DotNet3.0之后所推出的一种新的使用多线程的方式&#xff0c;它是基于ThreadPool线程进行封装的。 二、使用多线程的时机 任务能够并发运行的时候&#xff0c;提升速度&#xff1b;优化体验 三、基本使用方法 private void button5_Click(object sender, Ev…