小编use*_*667的帖子

c#以编程方式强制调试模式

没关系为什么人们想要这样做,我只是想知道它是否可以完成.

这是我的代码不起作用:

if (!Debugger.IsAttached)
{
    try
    {
        Debugger.Launch();
        while (!Debugger.IsAttached)
        {
            Thread.Sleep(1000);
        }
    }
    catch (System.Security.SecurityException e)
    {
        Console.WriteLine("exception " + e.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上我很想知道如何使用该Debugger.Launch()方法.

c# debugging

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

当值类型可以相互转换时,为什么我不能将一个值类型的字典转换为另一个值类型的字典?

可能重复:
在C#中,为什么List <string>对象不能存储在List <object>变量中

以下为什么不工作?

List<string> castMe = new List<string>();
IEnumerable<string> getFromCast  = (IEnumerable<string>)castMe; // allowed.

Dictionary<int, List<string>> castMeDict = new Dictionary<int, List<string>>();
Dictionary<int, IEnumerable<string>> getFromDict = (Dictionary<int, IEnumerable<string>>)castMeDict;  // Not allowed
Run Code Online (Sandbox Code Playgroud)

这是Dictionary铸造机制中的一个缺陷,还是我认为应该允许这样做?

谢谢.

c# generics dictionary casting covariance

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

是否可以在运行时向EXISTING类添加方法?为什么或者为什么不?

我想这可能会使用Reflection.Emit,

但是关于SO的类似问题只回答了如何动态创建类/方法,而不是如何更新现有类.

与此类似,是否可以在运行时删除方法/类?如果是这样,我想可以删除该类,并使用旧方法和新方法将其添加回来.

提前致谢.

PS我没有这个用途,只是好奇心问题.

c# reflection.emit dynamic

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

枚举案例处理 - 更好地使用开关或字典?

在根据具体情况处理枚举值时,使用switch语句或字典会更好吗?

我认为字典会更快.就空间而言,它占用了一些内存,但case语句也会占用程序本身所需的内存中的一些内存.所以底线我认为只使用字典总是更好.

以下是两个并列的实现进行比较:

鉴于这些枚举:

enum FruitType
{
    Other,
    Apple,
    Banana,
    Mango,
    Orange
}
enum SpanishFruitType
{
    Otra,
    Manzana, // Apple
    Naranja, // Orange
    Platano, // Banana
    Pitaya // Dragon fruit, only grown in Mexico and South American countries, lets say
    // let's say they don't have mangos, because I don't remember the word for it.
}
Run Code Online (Sandbox Code Playgroud)

以下是使用switch语句执行此操作的方法:

private static SpanishFruitType GetSpanishEquivalent(FruitType typeOfFruit)
{
    switch(typeOfFruit)
    {
        case FruitType.Apple:
            return SpanishFruitType.Manzana;
        case FruitType.Banana:
            return SpanishFruitType.Platano;
        case FruitType.Orange:
            return SpanishFruitType.Naranja;
        case FruitType.Mango:
        case …
Run Code Online (Sandbox Code Playgroud)

c# enums dictionary switch-statement

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

暂停/中止线程为什么不好?

我的线程如何工作的模型是有些ThreadManager给每个线程转一圈.当它是一个线程时,它会执行几行代码.

暂停一个线程,是不是只有ThreadManager(暂时)停止允许该线程转弯?

要中止一个线程,不能ThreadManager只是永远不会给那个线程另一个转弯?

有什么问题?

.net c# parallel-processing multithreading thread-safety

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

使用javascript在javascript中确定堆栈深度

有没有办法确定使用javascript本身在javascript中执行的所有函数的堆栈深度?

我想它可能涉及修改Function原型,但我真的不知道.

另外,能够在堆栈深度足够高的任何时候断开都是很好的.

原因是我在IE中有一个堆栈溢出错误,显然是不可调试的.我很懒,而且我宁愿不必去寻找我正在寻找原因的代码.

谢谢你帮助我的懒惰.

javascript stack-overflow stack

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

c#:调试时为public,否则为private

有没有一种很好的方法来实现它,以便在我使用NUnit进行测试时函数是公共的,但是否则是私有的?

不必生成大量无关代码也不错.

- - - - - - - - - - - - 编辑 - - - - - - - - - - - -

似乎解决方案属于3种类型:

  1. 不要做我想做的事.
  2. 使用编译器指令.
  3. 尝试一个聪明的解决方案(如使用InternalsVisibleTo).

有没有办法以编程方式执行此操作?即只需创建一个新的临时应用程序,使所有protected/private/internal功能public,插入NUnit,在那里运行测试,然后回到使用私有函数的发布版本?

c# debugging nunit access-modifiers

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

使用JavaScript获取所有对象(DOM或其​​他)

简短版本:

  • 如何在页面上获取所有对象(包括其后代对象)的列表(而不仅仅是第一个深度对象)?
    • 预期的子问题:当我走过物体时,如何跟踪被访问的对象?

提前致谢.



长版(带背景!!):

使用in关键字,我们可以获得对象的所有属性.(并且使用该hasOwnProperty方法允许我们仅过滤掉属于该对象的属性,而不过滤掉继承的属性.)

for (var prop in obj) {
   if (typeof(obj[prop]) == 'object'  && obj.hasOwnProperty(prop)) {
            showObjectsOfInternal(obj[prop], visitedObjects); // recursion.
   }
}
Run Code Online (Sandbox Code Playgroud)

这是一个很好的起点,但我想得到所有的对象.可以想象迭代所有属性并累积对象,然后递归迭代这些属性.但是,如果有一个对象引用循环,就像一个自己引用的对象,比如in window.window,那么最好不要陷入困境.因此,需要一种方法来跟踪递归期间的所有"访问对象".

要跟踪被访问对象,需要根据内部对象键真正需要对象的哈希集.我尝试通过制作一个visitedObjects对象并将其设置为要添加的对象,并且值无关紧要.

if(visitedObjects[obj] == 1){return;}
visitedObjects[obj] = 1;
Run Code Online (Sandbox Code Playgroud)

但这对我不起作用.(似乎将对象转换为键的字符串,而不是使用其内部引用键)

所以我决定使用一个数组并添加一个indexOf方法.

Array.prototype.indexOf = function(obj){
   for(var i = 0; i < this.length; i++)
   {
      if(this[i] == obj) // reference comparison for non-primitive objects.
      {
         return i;
      }  
   }
   return -1; 
}
Run Code Online (Sandbox Code Playgroud)

但这也没有用(最终我得到了我不能做的,for(var prop in obj) …

javascript javascript-objects

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

对另一个断点的条件使用一个断点的命中计数

我在C#代码上使用Visual Studio 2008.

如果另一个断点被击中(并打破了),我只想在断点处打破.有没有办法做到这一点?

我认为作为子问题,访问调试器具有的信息会很好.

这个的基本原理是我只对给定某个callstack(以及在callstack中执行其中一个函数的某个点)打破某个断点感兴趣.也许我应该使用callstack代替?另一个原因是编程访问调试器知道的东西会很有趣.

谢谢.

debugging conditional-breakpoint visual-studio-2008

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

这个派生的接口getter有什么含糊之处?

我想添加这些接口:

public interface IIndexGettable<TKey, TValue>
{
    TValue this[TKey key]{ get; }
}

public interface IIndexSettable<TKey, TValue>
{
    TValue this[TKey key]{ set; }
}

public interface IIndexable<TKey, TValue> : IIndexGettable<TKey, TValue>, IIndexSettable<TKey, TValue>
{   
  // new TValue this[TKey key]{ get; set; } // as suggested by SO question 1791359 this will "fix" the issue.
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用IIndexablegetter时,我得到以下编译器错误:

The call is ambiguous between the following methods or properties: 'IIndexGettalbe<TKey, TValue>.this[Tkey]' and 'IIndexSettable<TKey, TValue>.this[TKey]'

这是我运行的代码:

public static class IIndexableExtensions
 {
    public static …
Run Code Online (Sandbox Code Playgroud)

c# inheritance interface

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