如何检测何时从catch块中调用当前正在执行的代码?
void SomeFunction()
{
// how do I detect whether I am being called from within a catch block?
}
Run Code Online (Sandbox Code Playgroud)
编辑:
对于那些要求,我想实现这样的类,不要错过错误冒泡逻辑:在编写此代码示例时,我收到编译器错误"在catch子句之外不允许使用不带参数的throw语句"所以无论如何,这有点摧毁了我的想法.
public class ErrorManager {
public void OnException(Exception ex) {
LogException(ex);
if (IsInsideCatchBlockAlready()) {
// don't destroy the stack trace,
// but do make sure the error gets bubbled up
// through the hierarchy of components
throw;
} else {
// throw the error to make it bubble up
// through the hierarchy of components
throw ex;
}
} …Run Code Online (Sandbox Code Playgroud) 我使用这个单元格公式成功地将ThinkOrSwim交易平台的DDE数据接收到excel:
=TOS|Last!AAPL
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用.Net C#应用程序中的NDDE库连接到ThinkOrSwim DDE时,我收到以下错误消息,这与我在ThinkOrSwim平台根本没有运行时会得到的相同:
客户端无法连接到"TOS | LAST".确保服务器应用程序正在运行,并且它支持指定的服务名称和主题名称对.
与NDDE连接的完全相同的方法在2009年工作,并且在2012年重新使用它时失败了.也许ThinkOrSwim在他们的应用程序中做了一些改变来阻止它?我想知道下一步该做什么......要么找到一种方法让NDDE工作(更好),要么在我的.Net应用程序中运行excel电子表格(凌乱).
希望有人在这里知道我可以继续使用NDDE并修复该连接问题.
单击下载我创建的小代码库以演示此问题(Visual Studio 2010).页面加载后,您必须单击文件 - >下载.
Bounty仅用于显示使用纯.Net/windows api解决方案的解决方案的答案.bounty不接受.Net内的Excel自动化.
从以下网址下载ThinkOrSwim:https://mediaserver.thinkorswim.com/installer/InstFiles/thinkorswim_jse6_installer.exe 如果您需要登录详细信息,请私下与我联系.
如何设置Newtonsoft.Json以使用旧成员名称反序列化对象,但使用当前成员名称对其进行序列化?
这是一个需要序列化和反序列化的示例对象.我给了一个属性一个属性,其中包含过去可能已被序列化的名称列表.
[DataContract]
class TestObject {
[LegacyDataMemberNames("alpha", "omega")]
[DataMember(Name = "a")]
public int A { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想json序列化始终使用名称"a",但能够从任何遗留名称反序列化到一个属性,包括"alpha"和"omega"以及当前名称"a"
以下代码在 netcoreapp2.0 应用程序中运行时,似乎最终不会抛出UnobservedTaskException
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp3 {
class Program {
public static void Main(string[] args) {
TaskScheduler.UnobservedTaskException += (s, e) => {
/// Try to crash the application - we wanna nail unobserved exceptions dead.
/// Unfortunately, this code never seems to run.
Console.WriteLine("UnobservedTaskException thrown.");
throw e.Exception;
};
var t = Task.Run(() => {
throw new NotImplementedException();
});
while (!t.IsFaulted)
Thread.Sleep(1);
t = null;
Console.WriteLine("Task is faulted.");
GC.Collect();
GC.WaitForPendingFinalizers();
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是项目文件。我怎样才能 …