如何使用应用程序本身更改C#应用程序代码 [英] How to change C# Application Code using application itself

查看:137
本文介绍了如何使用应用程序本身更改C#应用程序代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序并对其进行编码,但代码中有一个特定的文本应该是自定义的我意味着应用程序的用户可以将文本更改为他们想要的任何方式吗?

示例代码



 如果( requestedText.Contains( 文本[我想让任何人更改此文本]))

解决方案

从形式上讲,标题中提出的问题的答案是: System.Reflection.Emit

https://msdn.microsoft.com/en-us/library/system.reflection.emit%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en -us / library / vstudio / 8ffc3x75%28v = vs.110%29.aspx [ ^ ]。



但很明显,这真的很高 - 技术的东西不是你需要的。你需要的是从本质上学习什么是编程。此时,你真的没有任何线索。



你不需要修改应用程序代码来修改一些文本。文字是数据。数据存储在变量和类型成员中,也可以保存(存储在文件和其他流中)。你所展示的是立即常数,这不是最典型的数据使用。



对于应用程序代码,是受保护的通过OS进行任何更改。 System.Reflection.Emit 对于编译的语言和平台来说是非常奇特的,以及在特殊意义上更改代码的高级工具:它只能在运行时添加代码,不要修改任何东西当编程出现时,建立了两个基本概念:程序代码存储并且代码与数据分离:

https://en.wikipedia.org/wiki/Self-modifying_code [ ^ ],

https: //en.wikipedia.org/wiki/Code_segment [ ^ ],

https://en.wikipedia.org/wiki/Data_segment [ ^ ]。



理解根概念,例如变量,是编程的核心。我建议你学习它,忘了你的问题。对不起,没有代码样本给你,没有别的。你需要从一开始就开始。



-SA


所以不要努力代码 - 或者通过文本框以通常的方式输入它,或者将其保存在配置文件中,并提供一种改变它的机制。

配置文件是其中一个最简单:

1)在解决方案资源管理器中打开项目属性,然后双击Settings.settings

2)在结果网格中,将名称更改为 MySetting,并将值设置为默认值。将类型和范围分别保留为字符串和用户。

3)保存并关闭设置窗口。

4)阅读设置:

 string s = Properties.Settings.Default.MySetting; 

5)写下你的设置:

 Properties.Settings.Default.MySetting =我的新设定值; 
Properties.Settings.Default.Save();



然后您可以在代码中使用该字符串:

  if (requestedText.Contains(  a text [ + Properties .Settings.Default.MySetting +  ]))


I am developing an application and coded it, but there is a particular text in the code which should be custom I mean the users of the application can change the text to whatever they want how to do that?
Example Code

if (requestedText.Contains("A text [I Want this text to be changed by anyone]"))

解决方案

Formally speaking, the answer to the question formulated in the title would be: System.Reflection.Emit:
https://msdn.microsoft.com/en-us/library/system.reflection.emit%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/vstudio/8ffc3x75%28v=vs.110%29.aspx[^].

But it's obvious enough that this really high-tech stuff is not what you need. What you need is to learn what is programming, in essence. At this moment, you really have no clue.

You don't need to modify "application code" do modify some text. Text is data. And data is stored in variables and type members, and also can persist (stored in files and other streams). What you show is immediate constant which is not the most typical use of data.

As to the "application code", is is protected by OS from any changes. System.Reflection.Emit is a very exotic, for compiled languages and platforms, and advanced facility which changes the code in a special sense: it can only add the code during runtime, not modify anything. When the programming was emerging, two fundamental conceptions were established: program code is stored and code is separated from data:
https://en.wikipedia.org/wiki/Self-modifying_code[^],
https://en.wikipedia.org/wiki/Code_segment[^],
https://en.wikipedia.org/wiki/Data_segment[^].

Understanding the root concepts, such as variables, is the core of programming. I suggest you learn it, and just forget about your question. Sorry, not code sample for you, nothing else. You need to start from the very beginning.

—SA


So don't hard code it - either get your use to input it in the usual way via a text box, or save it in a configuration file, and provide a mechanism to alter it.
The config file is one of the simplest:
1) Open your projects Properties in the solution explorer, and double click on "Settings.settings"
2) In the resulting grid, change the Name to "MySetting", and set the Value to "Defaulted value". Leave Type and Scope as "string" and "User" respectively.
3) Save and close the settings window.
4) To read your setting:

string s = Properties.Settings.Default.MySetting;

5) To write your setting:

Properties.Settings.Default.MySetting = "My new setting value";
Properties.Settings.Default.Save();


You can then use that string in your code:

if (requestedText.Contains("A text [" + Properties.Settings.Default.MySetting + "]"))


这篇关于如何使用应用程序本身更改C#应用程序代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆