我有一个编译的dll,我需要改变一点.它是半年前由我写的,但我丢失了源代码.其中有一个单独的硬编码字符串(它是文件名)
我需要将它TestPage.html改为TestPage1.html(它不会再长)
怎么做?该字符串是匿名的,相应的代码片段是:
... + folder + "TestPage.html"
Run Code Online (Sandbox Code Playgroud)
它没有分配给它的变量.
提前致谢.
编辑:我不想在使用类似Reflector的工具提取c#代码后重新编译它!
是否可以在IL Generator中使用泛型?
DynamicMethod method = new DynamicMethod(
"GetStuff", typeof(int), new Type[] { typeof(object) });
ILGenerator il = method.GetILGenerator();
... etc
Run Code Online (Sandbox Code Playgroud) 我有以下C#代码:
public static double f(double x1, double x2 = 1)
{
return x1 * x2;
}
Run Code Online (Sandbox Code Playgroud)
这里是IL代码(ILSpy):
.method public hidebysig static
float64 f (
float64 x1,
[opt] float64 x2
) cil managed
{
.param [2] = float64(1)
// Method begins at RVA 0x20c6
// Code size 4 (0x4)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: mul
IL_0003: ret
} // end of method A::f
Run Code Online (Sandbox Code Playgroud)
如何使用System.Reflection.Emit或更好的Mono.Cecil获取它?
我是用C#制作的psp模拟器的作者.
我正在使用ILGenerator生成大量"DynamicMethod".我正在将汇编代码转换为AST,然后生成IL代码并构建DynamicMethod.我在另一个线程中执行此操作,因此我可以在程序执行其他程序时生成新方法,以便它可以顺利运行.
我的问题是本机代码生成是懒惰的,因此在调用函数时生成机器代码,而不是在生成IL时生成.因此它在程序执行线程中生成,本机代码生成速度非常慢,因为它是asm-> ast-> il步.
我尝试过Marshal.Prelink方法,它在执行函数之前生成机器代码.它适用于Mono,但它不适用于MS .NET.
Marshal.Prelink(MethodInfo);
Run Code Online (Sandbox Code Playgroud)
有没有办法在MS .NET上预先链接DynamicMethod?
我想为函数添加一个布尔参数,如果设置,则立即退出函数,因此实际上没有执行任何代码.我可以用这种方式"预链接",但我认为这是一个我想避免的令人讨厌的解决方案.
任何的想法?
我知道编译器会"省略int foo = 0;为字段生成代码因为内存分配器会将字段初始化为默认值." 参考
class Foo
{
public int a = 1;
public int b = 0;
}
Foo..ctor:
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: stfld UserQuery+Foo.a // There is no b.
IL_0007: ldarg.0
IL_0008: call System.Object..ctor
IL_000D: ret
Run Code Online (Sandbox Code Playgroud)
我也知道"编译器会自动添加.locals init每个使用局部变量的方法,表明JIT必须在开始执行方法之前注入初始化所有局部变量的代码." 参考
为什么编译器没有int foo = 0;为局部变量之类的东西省略生成IL,因为.locals init已经覆盖了它?(为了与字段保持一致?)
(我理解C#规范要求明确分配局部变量,我很好.)
(我引用的链接说明了为什么.locals init需要,以及为什么C#规范要求初始化本地化.但是它没有说明为什么必须存在额外的IL指令来初始化默认值.因为验证过程已经确定了.locals init)
void Main()
{
int a = 0;
int b = 1;
int c = 0; …Run Code Online (Sandbox Code Playgroud) struct Point
{
public int x;
public int y;
}
void Main()
{
Point p;
p.x = 1;
p.y = 1;
Object o = p;
((Point) o).x = 4; // error
((Point) o).x = 5; // error
((Point) o).x = 6; // error
p = (Point) o // expect 6
}
Run Code Online (Sandbox Code Playgroud)
为什么不编译
ldloc.1 // o
unbox Point
ldc.i4.4
stfld Point.x
Run Code Online (Sandbox Code Playgroud)
C++ CLI允许的地方.
考虑以下IL代码:
.method public static void Main()
{
ldstr "Starts Here"
call void [mscorlib] System.Console::WriteLine(string)
.try {
ldstr "Try Me!"
call void [mscorlib] System.Console::WriteLine(string)
leave.s Done
}
catch [mscorlib] System.Exception {
ldstr "Catch Me!"
call void [mscorlib] System.Console::WriteLine(string)
leave.s Done
}
Done:
ldstr "Ends Here"
call void [mscorlib] System.Console::WriteLine(string)
ret
}
Run Code Online (Sandbox Code Playgroud)
CLR如何try在JIT编码中定义块?本机代码看起来如下:
...
00900076 8b0538214703 mov eax,dword ptr ds:[3472138h] ("Starts Here")
...
00900090 8b053c214703 mov eax,dword ptr ds:[347213Ch] ("Try Me!")
...
009000a2 eb1b jmp 009000bf ;// Done
009000a4 8945d4 …Run Code Online (Sandbox Code Playgroud) 我想分析.Net程序集与C#,VB.NET或其他任何语言无关.
我知道Roslyn和NRefactory,但他们似乎只在C#源代码级别上工作?CodePlex上
还有" 通用编译器基础设施:代码模型和AST API "项目,该项目声称"支持一种表示与语言无关的结构化形式的代码块的分层对象模型",它完全符合我的要求.
但是,我无法找到任何有用的文档或实际执行此操作的代码.
有什么建议如何存档?
Mono.Cecil可以做点什么吗?
减少downvotes :(起初可跳过)
我知道这个问题听起来毫无意义和/或奇怪.我正在创建JIT,它使用C#代码用csc.exe编译它,提取IL并将其平行化为CUDA,我想覆盖C#中的一些东西.
如何覆盖基础的东西,如int\ string?我试过了:
class String { /* In namespace System of course */
BasicString wrap_to;
public String( ) {
wrap_to = new BasicString( 32 ); // capacity
}
public String( int Count ) {
wrap_to = new BasicString( Count );
}
public char this[ int index ] {
get { return wrap_to[ index ]; }
set {
if ( wrap_to.Handlers != 1 )
wrap_to = new BasicString( wrap_to );
wrap_to[ index ] = value;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何If在C#中使用ILGenerator生成语句.
这是我的代码:(ilg是ILGenerator)
ilg.Emit(OpCodes.Ldc_I4_1);
Label start = ilg.DefineLabel();
ilg.Emit(OpCodes.Brfalse, start);
ilg.Emit(OpCodes.Ldstr, "Hello");
ilg.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
ilg.MarkLabel(start);
Run Code Online (Sandbox Code Playgroud)
一些说明:
未处理的异常:System.InvalidProgramException:公共语言运行时检测到无效的程序.在Testing.Test.Main(String [])
il ×10
c# ×8
.net ×4
clr ×3
decompiling ×2
mono.cecil ×2
reflection ×2
assembly ×1
boxing ×1
ilgenerator ×1
nrefactory ×1
performance ×1
roslyn ×1
unboxing ×1