当前位置:网站首页>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();
}
}
}
边栏推荐
猜你喜欢

第十七章 Excel操作

【New Edition】DeepFakes: Creation, Detection and Influence

SAP 云平台上一种 Low Code Development(低代码开发)解决方案

net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。

软件测试X模型

LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路

带你认识40G单纤双向光模块-QSFP+ BiDi光模块

软件测试H模型

二维数组零碎知识梳理

Using the TCP protocol, will there be no packet loss?
随机推荐
R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
Use compilation to realize special effects of love
关于缓存数据的探讨
从零开始入门单片机(一):必会背景知识总结
HikariCP database connection pool, too fast!
李航《统计学习方法》笔记之监督学习Supervised learning
The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit
周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...
日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
R language time series data arithmetic operation: use the log function to log the time series data, and use the diff function to calculate the successive difference of the logarithmic time series data
用了TCP协议,就一定不会丢包嘛?
js防抖函数和函数节流的应用场景
STL中list实现
Spearman's correlation coefficient
win10打印服务无法启动(运行时错误automation)
新“内卷”席卷科技圈,Google CEO 要求 174000 员工提高工作效率!
SAP 云平台上一种 Low Code Development(低代码开发)解决方案
牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
QT专题:自定义部件
用汇编实现爱心特效【七夕来袭】