标签: clr

从C#调用ILAsm vararg函数

我正在编写ILAsm函数,它接收可变数量的参数并返回它们的总和:

.class public auto ansi TestSentinel
{
    .method public static vararg unsigned int64 Sum( /* all arguments are optional */ )
    {
        .locals init( value class [mscorlib]System.ArgIterator Args, 
                      unsigned int64 Sum, 
                      int32 NumArgs )
        ... 
        ldloc Sum
        ret
    }
}
Run Code Online (Sandbox Code Playgroud)

我的DLL编译成功但我不能用C#代码调用这个函数.我打电话的时候

var k = TestSentinel.Sum(1);
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

Error  The best overloaded method match for 'TestSentinel.Sum(__arglist, ...)' has some invalid arguments
Run Code Online (Sandbox Code Playgroud)

我收到任何其他数量的论据wrong argument number.调用我的ILAsm函数的正确方法是什么?

.net c# clr il

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

存储注释的位置以及如何提取注释?

假设我们有以下代码:

/// <summary>
/// Test class comment
/// </summary>
public class Test
{
    /// <summary>
    /// Method comment
    /// </summary>
    /// <param name="a">First parameter comment</param>
    /// <param name="b">Second parameter comment</param>
    /// <returns>Return value comment</returns>
    public int Foo(int a, int b) { return 1; }
}
Run Code Online (Sandbox Code Playgroud)

C#编译器在汇编中存储注释的位置?如果IntelliSence可以使用评论,我也想要.

.net c# reflection clr visual-studio

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

构造函数是初始化值还是CLR?

请参阅下面的代码

我的问题是:我没有初始化的默认值eIdeName,但仍向属性初始化为默认值.

CLR这样做吗?

class Employee
{
    int _empId;
    String _eName;
    public Employee()
    {
        // I am not initializing the attributes here
    }
    public void Disp()
    {
        Console.WriteLine("Id:: {0}  Name:: {1}", _empId, _eName);
    }
}

class Program
{
     static void Main(string[] args)
     {
        new Employee().Disp();
     }
}
Run Code Online (Sandbox Code Playgroud)

.net c# clr constructor

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

如果int是32位.ToString()和GetType()等函数存储在哪里?

int是32位.所有这些位都用于存储int值.为什么int也包含ToString()和GetType()等函数?

.net c# clr

1
推荐指数
2
解决办法
90
查看次数

有什么方法可以限制类型变量可能包含的类型吗?

我的代码类似于以下内容.它将整数与类型相关联.我想然后使用这个字典来查找给定整数的类型,然后实例化该类型.

Dictionary<int, Type> RegistrationMethods;

RegistrationMethods = new Dictionary<int, Type>();
RegistrationMethods.Add(1, typeof(RegistrationProvider_01));
RegistrationMethods.Add(2, typeof(RegistrationProvider_02));
Run Code Online (Sandbox Code Playgroud)

问题:我的所有类型都实现了IRegistrationMethod.有没有办法声明我的字典,以便它只能容纳实现此接口的类型?这将使我的代码更安全.

谢谢你的任何提示.

.net c# clr types

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

C#方法中的静态参数

我在C#中怀疑方法中的静态类使用.假设我们在另一个类中有一个带有两个参数int和Enum的方法.

public void DemoMethod(int pricePerTenant , TenantType tenantType){
    //Method implementation    
}
Run Code Online (Sandbox Code Playgroud)

如果我们实现静态类而不是Enum,C#不允许将静态类作为方法参数传递

public static class TenantType
{
   public static readonly int TenantAdmin = 1;
   public static readonly int TenantUser = 2;
   public static readonly int PublicUser = 3;
}

//With static class parameters
public void DemoMethod(int pricePerTenant , TenantType tenantType){
    //Method implementation    
}
Run Code Online (Sandbox Code Playgroud)

为什么C#CLR拒绝将Static类作为参数?

谢谢

.net c# clr static class

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

CLR识别线程

此页面http://msdn.microsoft.com/en-us/library/zf749bat(v=vs.110).aspx讨论了CLR报告的不同线程编号:物理,逻辑和已识别.

有人可以解释一下线程被CLR"识别"的意义吗?

为什么会使我认可的线程最大值为2,而物理和逻辑线程都在30s?

.net clr performance multithreading

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

为什么不引发溢出异常?

以下运行正常,没有错误,diff是1:

int max = int.MaxValue;
int min = int.MinValue;
//Console.WriteLine("Min is {0} and max is {1}", min, max);
int diff = min - max;
Console.WriteLine(diff);
Run Code Online (Sandbox Code Playgroud)

那么所有程序都不会被怀疑吗? a + b不再是ab的总和,其中ab是类型int.有时它是,但有时它是a,b和2*int.MinValue 的总和*.

*加上普通英语含义的总和,忽略任何计算机知识或单词大小

在PowerShell中,它看起来更好,但它仍然不是添加操作的硬件异常.似乎在使用long之前使用了一个int:

[int]$x = [int]::minvalue - [int]::maxvalue
Cannot convert value "-4294967295" to type "System.Int32". Error: "Value was either too large or too small for an Int32."
Run Code Online (Sandbox Code Playgroud)

c# clr powershell

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

我们可以在DotNet类库项目中使用global.asax文件

我们可以Global.asax.NET类库项目中使用文件吗?因为我还没有看到任何Global.asax文件只能用于基于Web的应用程序.

Global.asax应用程序启动时,正在编译文件代码并由.net框架调用.我想在我的类库中使用类似的功能,即当我的类库被加载到内存中时Application_Start,Global.asax将调用类库的文件.

我知道这可以使用静态构造函数,但我想利用Global.asax这个.

任何建议/意见将受到高度赞赏..

谢谢

.net c# asp.net clr

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

带有optional-parameter的构造函数是否重载了?

这是一个代码示例:

public List(int capacity = defaultCapacity) {
    items = new T[capacity];
}
Run Code Online (Sandbox Code Playgroud)

C# 5 Language Specification Section 1.6.7写中:

实例构造函数可以重载.例如,List类声明了两个实例构造函数,一个没有参数,另一个接受int参数.

IL为此代码编译不包含2个构造函数.它只包含此声明:

.method public hidebysig specialname rtspecialname 
        instance void  .ctor([opt] int32 capacity) cil managed
Run Code Online (Sandbox Code Playgroud)

这意味着可选参数是CLRlevel并由其定义[opt].

CLR没有运行时,可以表示与2个重载构造该对象.

例如,如果我创建2个单独的构造函数而没有可选参数,则编译IL将包含2 .ctor-s.

我想澄清一下,如果语言规范说这class declares two instance constructors并不意味着编译IL也会包含2- ctors.

c# clr language-specifications

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