gpt4 book ai didi

c# - 在 MATLAB try/catch 运算符中使用 C# 方法/属性

转载 作者:行者123 更新时间:2023-11-30 17:46:40 25 4
gpt4 key购买 nike

我已经为自动显微镜系统开发了一个驱动程序,用 C# 编写并使用 Visual Studio 2013 编译。在 try/catch 中分配一些属性时我遇到了一个特殊问题 循环。

下面是一个 MATLAB 代码示例(为显示而简化)。

pwsAssembly = NET.addAssembly ( 'file location' );
scope = Pws . Scope ( );
scope . Connect();

% This command performs the movement.
scope . Objective = 1;

% This command DOES NOT perform the movement, but DOES NOT enter the catch statement.
try
scope . Objective = 4;
catch
error ( 'Unable to adjust objective' );
end

% Again, this command performs the movement:
scope . Objective = 4;

ObjectiveScope 类中的 Get/Set 属性。

关于为什么 C# 属性的 Set 在 MATLAB try/catch 语句中无法正常执行的任何想法?

更多详情

我在 MATLAB 中进一步表征了行为。

  1. 如果我将代码作为脚本执行,没有中断或暂停,一些操作将被跳过。
  2. 如果我“逐步”执行脚本,每一行都会按预期执行,即使在 try/catch 运算符中也是如此。
  3. 如果属性赋值在 if 语句中,它总是成功的。

下面是修改后的 MATLAB 代码以反射(reflect)这一观察结果。

pwsAssembly = NET . addAssembly ( 'fileLocation' );
scope = Pws . Scope ( );
scope . Connect ( );

scope . Objective = 1; % Unsuccessful. Successful if I "step" through.

try
scope . Objective = 4; % Unsuccessful . Successful if I "step" through.
catch
error ( 'Headaches' );
end

if ( scope . Objective ~= 6 )
scope . Objective = 6; % Successful, always.
end

有什么想法吗?

最佳答案

我尝试了一个小的装配体,但我无法重现这个问题:

作用域.cs

using System;
namespace PWS
{
public class Scope
{
public int Objective { get; set; }

public Scope()
{
Objective = 0;
}

public void Connect()
{
Console.WriteLine("connected");
}
}
}

使用以下方法将其编译成程序集:

C:\> csc.exe /target:library Scope.cs

MATLAB

这是在 MATLAB 中使用它的代码:

>> NET.addAssembly(fullfile(pwd,'Scope.dll'));

>> scope = PWS.Scope();
>> scope.Connect();

>> scope.Objective = 1;
>> try, scope.Objective = 4, catch ME, error('failed'); end
>> if (scope.Objective ~= 6), scope.Objective = 6; end

无论我如何运行代码,所有行都运行良好:在命令窗口中以交互方式执行,作为脚本或函数运行,正常运行或在调试器中单步执行代码时运行。

(注意:对 Console.WriteLine 的任何调用通常不会显示在 MATLAB 中,尽管有一些方法可以显示 capture the output from .NET )

关于c# - 在 MATLAB try/catch 运算符中使用 C# 方法/属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25892169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com