使用 System::DateTime 参数调用 C++/CLI 方法需要“ValueType”;作为参数?

发布于 2024-09-03 10:55:52 字数 527 浏览 6 评论 0原文

我正在尝试从 C# 调用用 C++/CLI 编写的方法。 C++/CLI 代码用于在给定记录 ID 和 .NET 兼容数据类型的 System::DateTime 对象的情况下更新 Oracle 数据库中的 TIMESTAMP 列对于 Oracle 的 TIMESTAMP 类型。

我调用的方法具有以下原型:

bool ChangeJobUpdateDate (int jobIdIn, System::DateTime^ updateDateIn)

我在我制作的测试项目中添加了对此 DLL 项目的引用;我正在用 C# 编写测试。但是,当我尝试从 C# 单元测试项目中调用此方法时,该函数似乎具有以下方法声明(通过智能感知):

bool ChangeJobUpdateDate (int jobIdIn, ValueType updateDateIn)

我承认对 C++/CLI 不太熟悉,所以我缺少什么吗?

I'm attempting to call a method written in C++/CLI from C#. The C++/CLI code is used to update a TIMESTAMP column in an Oracle database, given a record ID and the System::DateTime object which is the .NET compatible data type for Oracle's TIMESTAMP type.

The method I am calling has the following prototype:

bool ChangeJobUpdateDate (int jobIdIn, System::DateTime^ updateDateIn)

I've added a reference to this DLL project in a test project that I made; I'm writing the tests in C#. However, when I try to call this method from the C# unit test project, the function appears to have the following method declaration (via intellisense):

bool ChangeJobUpdateDate (int jobIdIn, ValueType updateDateIn)

I'm admittedly not that familiar with C++/CLI, so is there something I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2024-09-10 10:55:53

我怀疑这是因为您使用的是 DateTime^ 而不仅仅是 DateTime。它是一种值类型,那么为什么要尝试使用引用呢?

C# 没有任何方法来表示与值类型关联的装箱引用类型,因此它能做的最好的事情就是 ValueType - 我怀疑这就是正在发生的事情,尽管我由于我缺乏 C++ 经验,所以不能肯定。尝试一下 DateTime 并看看它看起来如何......

I suspect it's because you're using DateTime^ instead of just DateTime. It's a value type, so why are you trying to use a reference?

C# doesn't have any way of representing the boxed reference type associated with a value type, so the best it can do is ValueType - I suspect that's what's happening, although I can't say for sure due to my lack of experience with C++. Try just DateTime and see how that looks...

护你周全 2024-09-10 10:55:53

人们对 DateTime^ 感到困惑的原因很简单,考虑这段代码:

// C# method
void somefunc(Int32 i, DateTime d);

// C++/CLR code
DateTime^ d = gcnew DateTime(2007, 11, 7); // You must use handle (^) format here
somefunc(42, d);  // Which means this won't work

所以基本上你应该使用 * 来访问 DateTime^ 的值> 并将其传递给您的 C# 方法:

somefunc(42, *d);

当然,如果您打算传递值而不是引用(这可能是大多数人首先想要的)。

The reason people are getting confused over DateTime^ is quite simple, consider this code:

// C# method
void somefunc(Int32 i, DateTime d);

// C++/CLR code
DateTime^ d = gcnew DateTime(2007, 11, 7); // You must use handle (^) format here
somefunc(42, d);  // Which means this won't work

So basically you should use * to access the value of DateTime^ and pass it to your C# method:

somefunc(42, *d);

That is of course if you intended to pass the value instead of a reference (which is what most people probably wanted in the first place).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文