所以我知道这有效:
class A
{
}
class B : A
{
}
[Test]
public void CanCast()
{
Assert.That(typeof(A).IsAssignableFrom(typeof(B)));
Assert.That(!typeof(B).IsAssignableFrom(typeof(A)));
}
Run Code Online (Sandbox Code Playgroud)
但是,让我们说这两种类型是Int32和Int64.
在运行时,我可以将Int32值转换为Int64变量,但不是相反.如何在运行时检查这种类型的转换兼容性?(IsAssignableFrom不适用于此,它总是为Int32和Int64提供false)
编辑:我不能简单地尝试施放,因为我没有这些类型的任何值,我问的是假设有两种类型A和B,没有两个值a和b.
假设我想要一个记录类型,例如:
type CounterValues = { Values: (int) list; IsCorrupt: bool }
Run Code Online (Sandbox Code Playgroud)
问题是,我想创建一个构造函数,将整数传递的列表转换为没有负值的新列表(它们将被0替换),并且只有在构造时发现负值时才有IsCorrupt = true .
这可能与F#有关吗?
现在,这就是我所做的,使用属性(但是,它,它不是非常F#-ish,它每次都在getter上调用ConvertAllNegativeValuesToZeroes(),所以效率不高):
type CounterValues
(values: (int) list) =
static member private AnyNegativeValues
(values: (int) list)
: bool =
match values with
| v::t -> (v < 0) || CounterValues.AnyNegativeValues(t)
| [] -> false
static member private ConvertAllNegativeValuesToZeroes
(values: (int) list)
: (int) list =
match values with
| [] -> []
| v::t ->
if (v < 0) then
0::CounterValues.ConvertAllNegativeValuesToZeroes(t)
else
v::CounterValues.ConvertAllNegativeValuesToZeroes(t)
member this.IsCorrupt = …Run Code Online (Sandbox Code Playgroud) 我读到void从C#async调用返回并不好.但我有以下情况:
public async void MainFunction()
{
await DoSomething()
await DoSomethingMore()
}
public void DoSomething()
{
//some code that I want to execute (fire and forget)
}
public void DoSomethingMore()
{
//some code that I want to execute (fire and forget)
}
Run Code Online (Sandbox Code Playgroud)
因为我只想要执行该函数而根本没有返回.我应该这样保留它,还是应该Task从DoSomething()返回?如果我将其更改为返回Task,因为我的代码根本不需要返回任何内容,我应该返回什么?
我运行这段代码:
var cancellation = new CancellationTokenSource();
var cancelledTask1 = .....;//starting new long-running task that accepts cancellation.Token
var cancelledTask2 = .....;//starting new long-running task that accepts cancellation.Token
//then I request cancellation
cancellation.Cancel();
//some task gets cancelled before code below executes
try
{
//wait for completion (some task is already in cancelled state)
await Task.WhenAll(cancelledTask1, cancelledTask2);
}
catch (OperationCanceledException e)
{
Logger.Debug("await WhenAll", e);
}
Run Code Online (Sandbox Code Playgroud)
我明白了
await WhenAll System.Threading.Tasks.TaskCanceledException: A task was canceled.
Run Code Online (Sandbox Code Playgroud)
我认为它很少,因为某些任务已经处于取消状态.为什么Task.WhenAll方法会破坏正常流程并在取消子任务时抛出异常?什么是受益于这种行为?
然后,我尝试了这个方法Task.WhenAny:
var cancellation = …Run Code Online (Sandbox Code Playgroud) 所以我有这段代码,我想在其中添加一个try-with块:
static member private SomeFunc
(someParam: list<DateTime*int>) =
let someLocalVar = helpers.someOtherFunc someParam
let theImportantLocalVar =
List.fold
helpers.someFoldFunc
([],someLocalVar.First())
someLocalVar.Tail
let first = fst theImportantLocalVar
let tail = someParam.Tail
helpers.someLastFunc(first,tail,theImportantLocalVar)
Run Code Online (Sandbox Code Playgroud)
我想添加的try-with块应该只是将调用包装起来List.fold,但是如果我只包装该行,那么我以后就无法访问该变量theImportantLocalVar.
现在,作为一种解决方法,我已经将try-with块包装整个函数体(除了关于赋值的第一行除外someLocalVar),但我想避免这种情况:
static member private SomeFunc
(someParam: list<DateTime*int>) =
let someLocalVar = helpers.someOtherFunc someParam
try
let theImportantLocalVar =
List.fold
helpers.someFoldFunc
([],someLocalVar.First())
someLocalVar.Tail
let first = fst theImportantLocalVar
let tail = someParam.Tail
helpers.someLastFunc(first,tail,theImportantLocalVar)
with
| BadData(_) -> raise (new
ArgumentException("output values can only increase: " …Run Code Online (Sandbox Code Playgroud) 所以我用flotcharts库生成了这个图表:

HTML是:
<div class="demo-container">
<div id="legend-container"></div>
<div id="placeholder" class="demo-placeholder"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
现在,我想将图例放在图表之外.所以我开始在我的情节选项中使用它:
var options = {
...,
legend:{
container:$("#legend-container"),
}
...
}
Run Code Online (Sandbox Code Playgroud)
而CSS:
#legend-container {
background-color: #fff;
padding: 2px;
margin-bottom: 8px;
border-radius: 3px 3px 3px 3px;
border: 1px solid #E6E6E6;
display: inline-block;
margin: 0 auto;
}
Run Code Online (Sandbox Code Playgroud)
结果是:

那很棒!但是现在我想放置图例,而不是放在图表的顶部(或底部),而是放在图表旁边的空白和空白处.
怎么做?
为了完整起见,我的其余CSS是:
.demo-container {
box-sizing: border-box;
width: 750px;
height: 300px;
padding: 20px 15px 15px 15px;
margin: 15px auto 30px auto;
border: 1px solid #ddd;
background: #fff;
background: linear-gradient(#f6f6f6 0, #fff 50px);
background: -o-linear-gradient(#f6f6f6 …Run Code Online (Sandbox Code Playgroud) 我正在努力整理一个简单的功能.
考虑以下定义:
type Entity = {Id:int;Data:string}
type IRepository =
abstract member SaveAsync: array<Entity> -> Task<bool>
abstract member RollBackAsync: array<Entity> -> Task<bool>
type INotification =
abstract member SaveAsync: array<Entity> -> Task<bool>
Run Code Online (Sandbox Code Playgroud)
这Task<T>是因为它们是用其他.NET语言开发的库.
(我为了这个例子创建了这段代码)
基本上,我想在存储库服务中保存数据,然后将数据保存在通知服务中.但是如果第二个操作失败,并且包含异常,我想回滚存储库中的操作.然后有两种情况我想要调用回滚操作,第一种if notification.SaveAsync返回false,第二种if抛出异常.当然,我想编写一次调用回滚,但我找不到方法.
这是我尝试过的:
type Controller(repository:IRepository, notification:INotification) =
let saveEntities entities:Async<bool> = async{
let! repoResult = Async.AwaitTask <| repository.SaveAsync(entities)
if(not repoResult) then
return false
else
let notifResult =
try
let! nr = Async.AwaitTask <| notification.SaveAsync(entities)
nr
with
| _-> false
if(not notifResult) then
let forget = …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过端口587与SSL,smtp.gmail.com一起在我的应用程序中通过Mono发送消息,并获取:
System.Net.Mail.SmtpException:无法发送消息。---> System.IO.IOException:身份验证或解密失败。---> System.IO.IOException:身份验证或解密失败。---> Mono.Security.Protocol.Tls.TlsException:从服务器接收到无效的证书。错误代码:0xffffffff800b010a
最有趣的是那一周一切正常,并且消息已发送。现在我弄错了。我也尝试过, mozroots --import但没有帮助。我也更新了证书GoogleInternetAuthorityG2.crt,但这没有帮助。
带有Mono 4.6.2的Ubuntu 16.04(稳定版4.6.2.7/08fd525)
有谁知道Mono是否支持.NET Standard 2.0.0?我目前有一个在Windows 10上正常运行的构建.当我尝试在Ubuntu Linux环境中使用Mono时,我收到以下错误:
无法找到程序集'netstandard'.检查以确保磁盘上存在程序集.
通过SDK添加Dotnet核心2没有任何问题,但我似乎没有任何对.NET Standard 2的引用.我最初在Windows 10上遇到了类似的问题,在我更新了我的visual studio版本后解决了这个问题.
C#怎么检查null.(value is null)或(null == value).我们可以使用is运算符代替==运算符吗?
C#7.0支持带is运算符的const模式.所以我们可以is null用于所有空检查?
除了null之外,对象是否也可以为空?