当前位置:网站首页>c#反射和特性
c#反射和特性
2022-08-02 09:51:00 【zzyzxb】
1、关于类型的反射示例
static void Main(string[] args)
{
string s = "hello";
Type t1 = s.GetType();
Console.WriteLine(t1.FullName);
Type t2 = Type.GetType("System.String", false, true);
Console.WriteLine(t2.FullName);
Type t3 = typeof(string);
Console.WriteLine(t3.FullName);
Console.ReadLine();
}
2、关于方法的反射示例
static void Main(string[] args)
{
string s = "hello";
Type t1 = s.GetType();
Console.WriteLine(t1.FullName);
Type t2 = Type.GetType("System.String", false, true);
Console.WriteLine(t2.FullName);
Type t3 = typeof(string);
Console.WriteLine(t3.FullName);
Console.WriteLine("*********************************************");
//GetMethods(t1);
Console.WriteLine("Copy method: {0}", t1.GetMethod("Copy"));
GetMethods(t1, BindingFlags.Public & BindingFlags.Instance);
//GetFields(), GetProperties();
Console.ReadLine();
}
public static void GetMethods(Type t, BindingFlags flags)
{
MethodInfo[] methodInfos = t.GetMethods(flags);
foreach (MethodInfo method in methodInfos)
{
Console.WriteLine(method.Name);
}
}
3、条件预处理指令
//#define DEBUG
//#undef DEBUG
#define TRACE
using System;
using System.Diagnostics;
using System.Reflection;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
#if (DEBUG)
{
Console.WriteLine("Debugging is enabled");
}
#elif(TRACE)
{
Console.WriteLine("Tracing is enabled");
}
#else
{
Console.WriteLine("Release is enabled");
}
#endif
Console.WriteLine("hello");
Console.ReadLine();
}
}
}
边栏推荐
- Facebook自动化数据分析方案,广告投放省心省力
- Implementation of mysql connection pool
- 单词接龙 II
- 让电商运营10倍提效的自动化工具,你get了吗?
- 如何安装dosbox(pycharm详细安装教程)
- 1对1视频源码——快速实现短视频功能提升竞争力
- AutoJs学习-实现科赫雪花
- 【OpenCV】-霍夫变换
- Application scenarios of js anti-shake function and function throttling
- Long battery life or safer?Seal and dark blue SL03 comparison shopping guide
猜你喜欢
Shell脚本实现多选DNS同时批量解析域名IP地址(新更新)
SAP 云平台上一种 Low Code Development(低代码开发)解决方案
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
In the whole development of chi V853 board tried to compile QT test
单词接龙 II
Application scenarios of js anti-shake function and function throttling
重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透
QT专题:自定义部件
QT专题:事件机制event基础篇
AlterNET Studio用户界面设计功能扩展
随机推荐
中国发布丨滴滴因违反网络安全法等被罚80.26亿元!调查细节公布
第十五章 多线程
The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"
Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
【新版干货书】深度伪造 (DeepFakes):创造,检测和影响
读博一年后对机器学习工程的思考
node封装一个图片拼接插件
阿里巴巴 CTO 程立:开源是基础软件的源头!
Rust 从入门到精通03-helloworld
The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化堆叠的柱状图(stacked bar plot)、lab.pos参数指定柱状图的数值标签的位置,lab.col参数指定数值标
软件测试的基本理论知识(软件测试面试基础知识)
斯皮尔曼相关系数
新“内卷”席卷科技圈,Google CEO 要求 174000 员工提高工作效率!
Use compilation to realize special effects of love
迭代器失效问题
图形化矩阵,矩阵到底长什么样?
第十六章 协程
node制作一个视频帧长图生成器
8月份的.NET Conf 活动 专注于 .NET MAUI