如何返回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>方法.我们可以提前设置我们的预期结果,并让它返回您期望的结果.)
我试图弄清楚如何让它正常工作:
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的类型不匹配.
我想用声明的参数名生成泛型类的名称.例如,如果我有通用类和实例化的类,如下所示我想打印"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
我知道装箱/拆箱会影响性能.根据MSDN,拆箱可能需要四倍的时间.我们的代码中有许多行具有"冗余"强制转换.实际上并不需要它们,如果没有它们,代码就可以很好地编译.可能它不会损害性能,因为编译器发现不需要进行拆箱,但也许不行!也许当我们强制转换时,编译器将被迫进行不必要的拆箱.我想知道那种类型的"冗余铸造"是否也会影响性能,就像拆箱或者nop一样?
什么是最好的(最清晰,最短,最快等)确定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.)
这是我的数组:
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)
如果我想让程序找到这个数组的模式,我该怎么写?
我的程序中有两种日期格式.在一台特定的机器上:
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获得同样的行为.为什么格式化日期中缺少正斜线?或者我应该建议他核武并铺好违规的盒子?
代码:
class MyEvent extends Event {
constructor(name) {
super(name);
}
}
var event = new MyEvent("mousemove");
Run Code Online (Sandbox Code Playgroud)
运行时错误:
未捕获的类型错误:无法构造“事件”:请使用“new”运算符,此 DOM 对象构造函数不能作为函数调用。
有没有解决的办法?
我有以下代码:
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>我想要的.
如何创建结束元素?我给它一个谷歌,我找不到多少.任何帮助表示赞赏,谢谢.
我有以下测试:
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)
在 …
c# ×7
.net ×5
generics ×2
typescript ×2
arrays ×1
boxing ×1
class ×1
concurrency ×1
date ×1
ienumerable ×1
javascript ×1
mode ×1
performance ×1
types ×1
windows ×1
xml ×1
xmldocument ×1