小编Mic*_*Liu的帖子

如何将List <object>转换为IEnumerable <T>

如何返回expected作为IEnumerable<T>给定下面的代码?

public List<object> expected = new List<object>();

public IEnumerable<T> SqlQuery<T>(string sql)
{
    // Return expected as IEnumerable<T>
}
Run Code Online (Sandbox Code Playgroud)

(这是为单元测试创​​建的类,用于模拟实体框架SqlQuery<T>方法.我们可以提前设置我们的预期结果,并让它返回您期望的结果.)

.net c# generics ienumerable

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

TypeScript:如何获取子类的方法来返回父类

我试图弄清楚如何让它正常工作:

class A {
    john(): B {
        return this; // <-- ERROR HERE
    }
}

class B extends A {
    joe(): B {
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我可以做方法链接:

let instance = new B();
instance.john().joe(); 
Run Code Online (Sandbox Code Playgroud)

当然,TypeScript抱怨this与B的类型不匹配.

class method-chaining typescript

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

在C#中访问泛型类型的GenericTypeParameters

我想用声明的参数名生成泛型类的名称.例如,如果我有通用类和实例化的类,如下所示我想打印"MyClass<P1,M>".

class MyClass<P1, M1> {}
// ... some code removed here
var myInstance = new MyClass<int,string>();
Run Code Online (Sandbox Code Playgroud)

现在我得到我的类型信息myInstance,然后是通用类型定义,如下所示:

// MyClass<int,string> type info here
var type = myInstance.GetType();
 // MyClass<P1, M1> type info here
var genericType = type.GetGenericTypeDefinition();
Run Code Online (Sandbox Code Playgroud)

调试器显示genericType具有所有参数名称和类型信息的属性GenericTypeParameters.但是,我无法从我的C#代码访问该集合,并且转换genericType为System.RuntimeType类不起作用,因为RuntimeType是内部的.

那么有没有办法以某种方式访问​​GenericTypeParameters属性或我在这里SOL?环境VS2015,.NET 4.6.1

c# generics types

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

冗余演员会影响演出吗?

我知道装箱/拆箱会影响性能.根据MSDN,拆箱可能需要四倍的时间.我们的代码中有许多行具有"冗余"强制转换.实际上并不需要它们,如果没有它们,代码就可以很好地编译.可能它不会损害性能,因为编译器发现不需要进行拆箱,但也许不行!也许当我们强制转换时,编译器将被迫进行不必要的拆箱.我想知道那种类型的"冗余铸造"是否也会影响性能,就像拆箱或者nop一样?

c# performance boxing

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

如何测试Date对象是否等于特定日期

什么是最好的(最清晰,最短,最快等)确定Date对象是否等于2012年7月4日的方式?

例如,这可行,但相当冗长:

if (date.getFullYear() === 2012 && date.getMonth() + 1 === 7 && date.getDate() === 4)
Run Code Online (Sandbox Code Playgroud)

有更优雅的解决方案吗?

(在我的实际场景中,服务器端代码将插入特定日期进行测试.此外,date它来自jQuery UI datepicker,因此时间组件为00:00:00.)

javascript date jquery-ui-datepicker

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

如何找到阵列的模式(最常见的元素)?

这是我的数组:

int myArray = new int[5];

myArray[0] = 1;
myArray[1] = 1;
myArray[2] = 1;
myArray[3] = 3;
myArray[4] = 5;
Run Code Online (Sandbox Code Playgroud)

如果我想让程序找到这个数组的模式,我该怎么写?

.net c# arrays mode

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

Windows正在一台机器上删除我日期的斜杠

我的程序中有两种日期格式.在一台特定的机器上:

  • date.ToString("M/d/yyyy h:m:s tt")正在生产像11222013 12:0:0 AM.(这:0:0是可以接受的;它是一个通常不会被人读取的文件.)
  • date.ToString("MM/dd/yyyy hh:mm tt")正在生产像11222013 12:00 AM.

我的代码是在C#中.那个机器的人有一些编程经验,并且能够用Delphi获得同样的行为.为什么格式化日期中缺少正斜线?或者我应该建议他核武并铺好违规的盒子?

.net c# windows date-formatting

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

是否可以扩展 TypeScript 中的内置 Event 类?

代码:

class MyEvent extends Event {
    constructor(name) {
        super(name);
    }
}

var event = new MyEvent("mousemove");
Run Code Online (Sandbox Code Playgroud)

运行时错误:

未捕获的类型错误:无法构造“事件”:请使用“new”运算符,此 DOM 对象构造函数不能作为函数调用。

有没有解决的办法?

typescript

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

强制XmlDocument使用显式结束标记保存空元素

我有以下代码:

string PathName = "C:\\Users\\TestUser\\Documents\\Project";
string FileName = "settings.xml";

XmlDocument Settings = new XmlDocument(); 
Settings.Load(Path.Combine(PathName, FileName));

XmlNode KnowledgeNode = Settings.SelectSingleNode("/Config/Map/Knowledge");

XmlNode UsersNode = Settings.CreateNode(XmlNodeType.Element, "Users", null);
XmlAttribute FileDirectory = Settings.CreateAttribute("FileDirectory");
FileDirectory.Value = UserSelectedFileDirectory;
UsersNode.Attributes.Append(FileDirectory);
KnowledgeNode.AppendChild(UsersNode);

Settings.Save(Path.Combine(PathName, FileName));
Run Code Online (Sandbox Code Playgroud)

这导致我的XML文件包含<Users FileDirectory="C:\data" /> 而不是<Users FileDirectory="C:\data" ></Users>我想要的.

如何创建结束元素?我给它一个谷歌,我找不到多少.任何帮助表示赞赏,谢谢.

.net c# xml xmldocument

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

当我将 `ConcurrentDictionary` 转换为 `IDictionary` 时出现奇怪的并发行为

我有以下测试:

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;               

public class Program
{
    public static void Main()
    {
        IDictionary<string, string> dictionary = new ConcurrentDictionary<string, string>(); // FAILS sometimes
        // ConcurrentDictionary<string, string> dictionary = new ConcurrentDictionary<string, string>(); // WORKS always

        Parallel.For(0, 10, i => dictionary.TryAdd("x1", "y1"));
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我在 .NET Fiddle 中运行它,有时会出现以下异常:

未处理的异常。System.AggregateException:发生一个或多个错误。(该键已存在于字典中)
System.ArgumentException:该键已存在于字典中。

在 System.Collections.Concurrent.ConcurrentDictionary`2.System.Collections.Generic.IDictionary<TKey,TValue>.Add(TKey key, TValue value)
在 System.Collections.Generic.CollectionExtensions.TryAdd[TKey,TValue](IDictionary` 2 字典,TKey 键,TValue 值)
在 Program.<>c__DisplayClass0_0.b__0(Int32 i)
在 System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.b__1(RangeWorker& currentWorker, Int64 timeout, Boolean&replicationDelegateYieldedBeforeCompletion)
---之前位置的堆栈跟踪结束 ---
在 System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.b__1(RangeWorker& currentWorker, Int64 timeout, Boolean&replicationDelegateYieldedBeforeCompletion)
在 …

.net c# concurrency thread-safety concurrentdictionary

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