标签: il

在IL中有这样的评论吗?

我意识到没有人会坐下来直接用IL开发软件(是吗?).但是假设您想与其他人共享一段IL代码(例如,由C#编译器输出)以供讨论,而且您想要用一些注释来注释它.在IL中是否有实际的注释语法,以便您可以在不将文本作为IL无效的情况下执行此操作?没什么大不了的,只是好奇.

.net syntax il comments cil

6
推荐指数
1
解决办法
530
查看次数

为VB.NET和C#生成IL差异

今天我正在使用Entity Framework,我已经读过为C#创建的IL与以下代码的VB.NET不同:

VB.NET:

Dim ctx As New TravelEntities

Sub Main()
    CallContext()
    CallContext()
    CallContext()
End Sub

Private Sub CallContext()

    Dim someCustomer = From x In ctx.Customer
            Where x.CustomerId.Equals(5)
            Select x
    Console.WriteLine(someCustomer.Count())
End Sub
Run Code Online (Sandbox Code Playgroud)

C#:

    private static TravelEntities ctx = new TravelEntities();

    static void Main(string[] args)
    {
        CallContext(); 
        CallContext(); 
        CallContext();
    }

    private static void CallContext()
    {
        var someCustomer = from x in ctx.Customer
                           where x.CustomerId.Equals(5)
                           select x;
        Console.WriteLine(someCustomer.Count());
    }
Run Code Online (Sandbox Code Playgroud)

他们产生以下IL:

VB:

.method private static void  CallContext() cil managed
{
  // Code size …
Run Code Online (Sandbox Code Playgroud)

c# vb.net compiler-construction il

6
推荐指数
1
解决办法
1114
查看次数

为什么C#编译器在IL中发出额外的OpCode?

如果我的方法Multiply定义为:

public static class Experiment
{
    public static int Multiply(int a, int b)
    {
        return a * b;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么为什么编译器会发出这个IL:

.method public hidebysig static int32 Multiply(int32 a, int32 b) cil managed
{
    .maxstack 2              //why is it not 16?
    .locals init (
        [0] int32 CS$1$0000) //what is this?
    L_0000: nop              //why this?
    L_0001: ldarg.0 
    L_0002: ldarg.1 
    L_0003: mul 
    L_0004: stloc.0          //why this?
    L_0005: br.s L_0007      //why this?
    L_0007: ldloc.0          //why this?
    L_0008: ret 
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,它还包含一些对我没有意义的额外操作码,实际上我期望以下IL:

.method public …
Run Code Online (Sandbox Code Playgroud)

c# il cil opcode

6
推荐指数
1
解决办法
439
查看次数

为什么LocalBuilder.SetLocalSymInfo没有发出变量名?

我试图运行出现的文档页面上的示例代码System.Reflection.Emit.LocalBuilder类,但它看来,调用LocalBuilder.SetLocalSymInfo(string, int, int)没有做任何事情,因为IL反汇编显示了这个作为IL的SampleAssembly.dll:

.method public static string  Function1(int32 A_0) cil managed
{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init (string V_0,
           int32 V_1)
  IL_0000:  ldarg.0
  IL_0001:  stloc.1
  IL_0002:  ldstr      "string value"
  IL_0007:  stloc.0
  IL_0008:  ldloc.0
  IL_0009:  ret
} // end of method Example::Function1
Run Code Online (Sandbox Code Playgroud)

为什么不在Dissasembler中列出变量名(myStringmyInt)?

环境信息:

  • Windows 7 64位
  • Visual Studio 2010 Professional SP1
  • .Net 4.0.30319 SP1
  • 目标框架:.Net 4客户端配置文件
  • 调试配置(对于使用System.Reflection.Emit的程序)

编辑:正如我在评论中提到的,有一个SampleAssembly.pdb文件与SampleAssembly.dll文件一起生成.

il reflection.emit .net-4.0 variable-names debug-symbols

6
推荐指数
1
解决办法
309
查看次数

如何控制.Net在命名空间冲突中选择哪个程序集?

我需要(忍受我)由于某种原因使用WinRT版本的System.Text.Encoding命名空间.我可以手动添加对程序集的引用,但它仍将使用mscorlib的实现.我显然无法完全删除mscorlib.

如何强制我的项目使用WinRT的System.Text.Encoding.dll而不是mscorlib?

基本上,我需要它来生成这块IL:

call class [System.Text.Encoding]System.Text.Encoding [System.Text.Encoding]System.Text.Encoding::get_UTF8()
Run Code Online (Sandbox Code Playgroud)

而不是这个:

call class [mscorlib]System.Text.Encoding [mscorlib]System.Text.Encoding::get_UTF8()
Run Code Online (Sandbox Code Playgroud)

.net c# il conflict namespaces

6
推荐指数
1
解决办法
1352
查看次数

C#动态类型导致使用IL中的反射解析Console.WriteLine

我一直在使用LINQPad中的一些C#语句来理解发出的中间语言代码.

我首先尝试了以下代码:

var Container = new {Name = "James"};
Console.WriteLine(Container.Name);
Run Code Online (Sandbox Code Playgroud)

并且看到以下六行IL发射:

IL_0001:  ldstr       "James"
IL_0006:  newobj      <>f__AnonymousType0<System.String>..ctor
IL_000B:  stloc.0     
IL_000C:  ldloc.0     
IL_000D:  callvirt    <>f__AnonymousType0<System.String>.get_Name
IL_0012:  call        System.Console.WriteLine
Run Code Online (Sandbox Code Playgroud)

这是我所期望的,并且非常好地演示了匿名类型是如何只读/不可变的,因为没有set_Name属性.

接下来我尝试了这些陈述:

dynamic Container = new System.Dynamic.ExpandoObject();
Container.Name = "James";
Console.WriteLine(Container.Name);
Run Code Online (Sandbox Code Playgroud)

这会导致大量的IL被释放.我不会在这里粘贴它,但你可以在这个pastebin中找到它.

我理解在管理动态类型和ExpandoObject方面存在相当多的开销,但我不明白为什么看起来System.Console.WriteLine在这种情况下调用是通过内部反射来执行的.

IL_0072:  ldstr       "WriteLine"
....
IL_00BF:  ldtoken     System.Console
Run Code Online (Sandbox Code Playgroud)

在第一段代码中,在检索并存储属性之后,它是一个调用的单行IL语句System.Console.WriteLine.

那么为什么一个dynamic类型的呼叫需要额外的所有这些?

c# compiler-construction reflection il dynamic

6
推荐指数
1
解决办法
1543
查看次数

为什么这个.Net IL无法验证?

我有一些我写的自定义IL,它不会通过PEVerify.我得到的错误是

$ peverify foo.exe

Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.17929
Copyright (c) Microsoft Corporation.  All rights reserved.

[IL]: Error: [Z:\virtualbox_shared\foo.exe : HelloWorld.Program::Main][offset 0x00000021] Stack height at all points must be determinable in a single forward scan of IL.
1 Error(s) Verifying foo.exe

然而,该程序将运行正常,没有任何例外.以下是相关方法的IL:

.method private static hidebysig
  default void Main (string[] args)  cil managed
{
// Method begins at RVA 0x2050
.entrypoint
// Code size 54 (0x36)
.maxstack 2

//custom IL
ldc.i4 1
ldc.i4 1
ceq
switch(first, second) …
Run Code Online (Sandbox Code Playgroud)

.net verification il peverify

6
推荐指数
1
解决办法
464
查看次数

为什么需要发出IL代码?

我在一个相当大的代码库中工作,今天我找到了IL code一个正常的内部发光项目class.

包含要发出的IL代码的项目是Service Locator MSDN描述的实现.

什么是这样做的优点为什么会变成这样做是并列使用的C#语言?

c# il intermediate-language

6
推荐指数
2
解决办法
2984
查看次数

C#中的显式构造函数调用

因此,今天我使用ILSpy + dotPeek反映了一个使用ILSpy + dotPeek的仲裁.NET程序集,以便在我偶然发现这个奇怪的部分(虚拟示例)时更深入地了解IL代码的工作原理:

public class SomeBaseClass {
    public SomeBaseClass(SomeType[] iExpectACollection) {
        ...
    }
}

public class SomeDerivedClass {
    public SomeDerivedClass(SomeType onlyOneInstance) {
        SomeType[] collection;
        if(onlyOneInstance != null)
            collection = new SomeType[] { onlyOneInstance };
        base.\u002Ector(collection);
    }
}
Run Code Online (Sandbox Code Playgroud)

据我所看到的,派生类不调用摆在首位的基础构造,而是做一些事onlyOneInstance,并随后调用是基构造.

我的问题是:在完成一些工作后,是否可以在C#中显式调用基础构造函数?或者这只能在IL中使用?我知道它很容易在Java中使用super(),但是我从未在.NET中看到它.


编辑

我刚和老板谈过,他可以发布一些真实的图书馆代码(这是我们公司的内部代码之一):

**IL PART**
.method public hidebysig specialname rtspecialname 
instance void .ctor (
    string contextId,
    class MyComp.NetStack.BufferManager bufferManager,
    class MyComp.NetStack.TcpChannelQuotas quotas,
    class [System]System.Security.Cryptography.X509Certificates.X509Certificate2 clientCertificate,
    class [System]System.Security.Cryptography.X509Certificates.X509Certificate2[] clientCertificateChain,
    class [System]System.Security.Cryptography.X509Certificates.X509Certificate2 serverCertificate,
    class MyComp.NetStack.EndpointDescription endpoint, …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection inheritance il

6
推荐指数
1
解决办法
3004
查看次数

如何从.NET程序集中读取数组初始化值

如何读取值,让我们说:'99'来自包含此代码的程序集

using Sytem; 

public class Class1
{
    public Class1() {
        // array initializer, want to read '99', '100'... from assembly
        var a = new double[,] { { 1, 2, 3 }, { 99, 100, 101 } };
        // ...
    }

}
Run Code Online (Sandbox Code Playgroud)

到目前为止我做了什么

ILDASM中的方法:

.method /*06000001*/ public hidebysig specialname rtspecialname 
        instance void  .ctor() cil managed
// SIG: 20 00 01
{
  // Method begins at RVA 0x2080
  // Code size       29 (0x1d)
  .maxstack  3
  .locals /*11000001*/ init ([0] float64[0...,0...] …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection il .net-assembly

6
推荐指数
1
解决办法
531
查看次数