当前位置:网站首页>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 ,因为编译器把相同的常量视为同一个地址,这样修改一个地方的常量就可以修改其他地方的。所以可以写的是一个常量,实际上这个常量在另一个地方被修改。
如果我代码很多,在某个地方使用了反射,反射一个方法,这个方法是修改一个常量的值,常量是写自己写的,没有引用。这时可以发现代码执行就可以更改之前的字符串值。实际上不只字符串,其它的常量也可以修改。多使用这些技术,可以让看代码的人成为强大的杀人狂。
这个方法是不推荐在一般情况使用,因为谁都不能说没有其他地方使用一样的字符串。
边栏推荐
- xpath获取带命名空间节点注意事项
- 1401 - Web technology 】 【 introduction to graphical Canvas
- G.登山小分队(暴力&dfs)
- 解题-->在线OJ(十八)
- 我爱七夕哈哈哈
- 集合划分差最小问题(01背包)
- CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source
- Win10无法访问移动硬盘怎么解决
- Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
- JCMsuite Application: Oblique Plane Wave Propagation Transmission Through Aperture
猜你喜欢

Technology sharing | Description of the electronic fence function in the integrated dispatching system

Zheng Qing freshmen school competition and middle-aged engineering selection competition

特殊品种的二次开户验资金额

爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例

leetcode: 253. How many meeting rooms are required at least

ASA归因:如何评估关键词的投放价值
ping的原理

leetcode:215无序数组中找第k大的元素

技术分享| 小程序实现音视频通话

The Internet of things application development trend
随机推荐
16. Learn MySQL Regular Expressions
爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
[LeetCode] 38. Appearance sequence
物联网应用发展趋势
AOSP内置APP特许权限白名单
Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
期货开户之前要谈好最低手续费和交返
[Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
[in-depth study of 4 g / 5 g / 6 g project - 50] : URLLC - 16 - the 3 GPP URLLC agreement, specification, technical principle of depth interpretation - 10 - high reliability technology - 1 - low codin
leetcode:250. 统计同值子树
九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来
饿了么智能头盔专利获授权,进一步提升骑手安全保障
AOSP built-in APP franchise rights white list
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
How to automatically renew the token after it expires?
Why does the decimal point appear when I press the space bar in word 2003?
兆骑科创创新创业大赛活动举办,线上直播路演,投融资对接
特殊品种的二次开户验资金额
leetcode:259. 较小的三数之和
Zheng Qing freshmen school competition and middle-aged engineering selection competition