我有一些用C#编写的代码.我正在尝试编写一些我可以重用的通用代码.我的实际代码更复杂.但是,代码问题如下所示:
public async Task<T> ExecuteQueryAsync<T>(string sql)
{
var results = default(T);
using (var database = new SqlConnection("[ConnectionString]"))
{
database.Open();
results = await database.QueryAsync<T>(sql);
}
return results;
}
Run Code Online (Sandbox Code Playgroud)
当我构建我的项目时,我得到一个编译时错误,说:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<T>' to 'T'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
我不完全理解为什么我会收到这个错误.我打算用这样的方式调用它:
Adapter adapter = new Adapter();
var results = await adapter.ExecuteQueryAsync<IEnumerable<Customer>>("SELECT * FROM Customer");
Run Code Online (Sandbox Code Playgroud)
在上面,不会T是一个IEnumerable<T>?如果是这种情况,我不明白为什么我在编译时遇到这种类型转换错误.
谢谢!
我有一个JavaScript数组对象.像这样的东西:
var people = [
{ id:1, firstName: 'Joe', lastName: 'Smith' },
{ id:2, firstName: 'Bill', lastName: 'Smith' }
];
Run Code Online (Sandbox Code Playgroud)
我正在通过人们进行迭代forEach.这是我的代码:
function doSomething() {
people.forEach(function(person, self) {
self.helpPerson(person);
}, this);
}
function helpPerson(person) {
alert('Welcome ' + person.firstName);
}
Run Code Online (Sandbox Code Playgroud)
我试图helpPerson从forEach循环内调用.但是,当我尝试调用它时,我收到一条错误消息:
TypeError: self.helpPerson is not a function
Run Code Online (Sandbox Code Playgroud)
如果我添加console.log(self);,"0"将打印到控制台窗口.这意味着我没有正确传递我的参数.或者,我误解了闭包(就在我认为我完全理解它的时候:)).
那么,为什么不self退出呢?
我正在学习ASP.NET MVC 5(vNext).为了做到这一点,我正在迁移现有的应用程序.在该应用程序中,我获得了一个实现特定接口的类列表.为了做到这一点,我使用以下代码:
// Find all classes that implement IMyInterface
var type = typeof(IMyInterface);
var classes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(y => type.IsAssignableFrom(y) && y.GetInterfaces().Contains(type))
.ToList();
if (classes == null)
Console.WriteLine("None found");
else
Console.WriteLine(classes.Count + " found.");
try {
foreach (var c in classes)
{
Console.WriteLine(c.GetType().FullName);
var converted = (IMyInterface)(c);
// Never gets here. Exception gets thrown.
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
// Prints: Cannot cast from source type to destination type.
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,抛出的异常是:"无法从源类型转换为目标类型." 当我打印出该类型的全名时,它就是System.MonoType.我究竟做错了什么?
我正在使用Moment.js跟踪我的应用中操作的持续时间。目前,我有以下代码:
var startMoment = null;
var actionClock = null;
function startClock() {
actionClock = setInterval(function(){
if (startMoment === null) {
startMoment = moment();
$('#duration').text('00:00:00');
} else {
var now = moment();
var span = moment.duration(now - startMoment);
$('#duration').text(span.minutes() + ':' + span.seconds() + '.' + span.milliseconds());
}
}, 1000);
}
function stopClock() {
clearInterval(actionClock);
var now = moment();
var span = moment.duration(now - startMoment);
$('#duration').text(span.minutes() + ':' + span.seconds() + '.' + span.milliseconds());
startMoment = null;
actionClock = …Run Code Online (Sandbox Code Playgroud) 在我的C#app中,我有一个看起来像这样的界面:
public interface IObject
{
Task<T> GetById<T>(int id);
Task<T> GetAll<T>();
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个实现此接口的类:
public class MyClass : IObject
{
public async Task<MyClass> GetById(int id)
{
return null;
}
public async Task<List<MyClass> GetAll()
{
var results = new List<MyClass>();
return results;
}
}
Run Code Online (Sandbox Code Playgroud)
在构建时,我收到以下错误:
MyClass没有实现接口成员'IObject.GetById(int)
MyClass没有实现接口成员'IObject.GetAll(int)
我究竟做错了什么?
我有一个用 C# 编写的 Windows 服务。该应用程序的“大脑”位于一个名为 的类中Driver。该类异步启动一堆线程来执行许多操作。这段代码中没有任何“阻塞”。驱动程序负责启动这些线程,侦听特定事件,并根据这些事件停止线程。
此时,我已将这段代码包装为 Windows 服务。、等方法只是此类的包装OnStart。OnStopWindows 服务按我希望的方式运行。但是,现在,我想在 Docker 容器中运行此代码。我的问题是,如何修改此代码以成为一个长时间运行的进程而不是 Windows 服务。
我最初的想法是将我的代码包装在控制台应用程序中。然后,我将代码从OnStart控制台应用程序的主体中移出。在该代码之后,我只需要有一个while(true){}声明来保持控制台应用程序处于活动状态。然而,这感觉就像是黑客攻击。
这种方法是黑客吗?或者,有没有一种“真正”的方法可以做到这一点?如果是这样,在 Docker 中长期运行 C# 应用程序的推荐方法是什么?
我正在学习打字稿。为此,我为自己构建了一个带有Node的基本实用程序。对于此应用程序,我需要使用Node的OS模块。我的问题是,如何导入该模块?
在我的Typescript文件中,我具有以下内容:
import { os } from 'os';
此行生成错误:“找不到模块'os'”。我想念什么?
c# ×4
javascript ×2
ado.net ×1
asp.net-mvc ×1
dapper ×1
docker ×1
momentjs ×1
node.js ×1
typescript ×1