当前位置:网站首页>C# 谁改了我的代码
C# 谁改了我的代码
2022-08-04 14:48:00 【林德熙】
本文告诉大家一个特殊的做法,可以修改一个字符串常量
我们来写一个简单的程序,把一个常量字符串输出
private const string str = "lindexi";
static void Main(string[] args)
{
Foo();
Console.WriteLine(str);
}其中的 Foo 是其他的函数,大家可以猜到输出是 lindexi ,但是,实际上把Foo调用函数添加之后,输出是 Lindexi 被大写了。那么这时 Foo 做了什么?
Foo 做的就是 C# 字符串首字符大写
public static unsafe void Foo()
{
fixed (char* ptr = str)
{
*ptr = char.ToUpper(*ptr);
}
}虽然出现了问题,但是找到问题很简单,如果这时需要做一个安全有关的。让别人看到源代码也不知道怎么使用,那么就可以使用这个科技,下面就是显示技术的时候
我把 Foo 做一些修改,把 str 变量去掉,这样大家就难以通过搜索变量引用找到了这个函数。但是我在其他的某个地方使用了这个常量字符串,于是就把上面的 str 修改为 “lindexi” 。大家也许会想,这是两个变量,对他做什么修改也不会对之前的 str 有什么影响。实际上,请跑一下下面的代码。
public static unsafe void Foo()
{
fixed (char* ptr = "lindexi")
{
*ptr = char.ToUpper(*ptr);
}
}这时输出 str 结果是 Lindexi ,因为编译器把相同的常量视为同一个地址,这样修改一个地方的常量就可以修改其他地方的。所以可以写的是一个常量,实际上这个常量在另一个地方被修改。
如果我代码很多,在某个地方使用了反射,反射一个方法,这个方法是修改一个常量的值,常量是写自己写的,没有引用。这时可以发现代码执行就可以更改之前的字符串值。实际上不只字符串,其它的常量也可以修改。多使用这些技术,可以让看代码的人成为强大的杀人狂。
这个方法是不推荐在一般情况使用,因为谁都不能说没有其他地方使用一样的字符串。
边栏推荐
- JCMsuite应用:倾斜平面波传播透过光阑的传输
- 爬虫——动作链、xpath、打码平台使用
- [Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
- 输入输出流总结
- Basic Introduction for PLSQL
- Technology sharing | Description of the electronic fence function in the integrated dispatching system
- B.构造一个简单的数列(贪心)
- LeetCode_模拟_中等_498.对角线遍历
- 程序猿七夕礼物-如何30分钟给女朋友快速搭建专属语聊房
- 郑轻新生校赛和中工选拔赛题解
猜你喜欢
随机推荐
【问题解决】QT更新组件出现 “要继续此操作,至少需要一个有效且已启用的储存库”
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
centos7安装mysql急速版
leetcode: 241. Designing precedence for arithmetic expressions
MySQL优化学习笔记
B. Construct a simple sequence (greedy)
技术分享| 融合调度系统中的电子围栏功能说明
Almost all known protein structures in the world are open sourced by DeepMind
OAID是什么
leetcode: 259. Smaller sum of three numbers
leetcode:255 验证前序遍历序列二叉搜索树
ping的原理
SQL语句的写法:Update、Case、 Select 一起的用法
Why does the decimal point appear when I press the space bar in word 2003?
Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market
解题-->在线OJ(十八)
华为云 & 达达,帮有情人“一键送达”
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
LeetCode_模拟_中等_498.对角线遍历
leetcode:241. 为运算表达式设计优先级








