我是 Java 背景的人,所以请多多包涵 :)
我正在尝试enum class从另一个 C++ 文件“导入”(使用 Java 术语)an ,以便能够直接使用它,而不必在它前面加上类名。
例如,假设我在头文件中有这个:
class Foo
{
public:
enum class Bar {ITEM_1, ITEM_2};
void doThings(Bar bar);
};
Run Code Online (Sandbox Code Playgroud)
现在,目前,如果我想Bar从另一个 C++ 文件中使用,我会这样做:
#include "Foo.h"
void Foo2::methodInAnotherFile()
{
Foo foo();
Foo::Bar bar = Foo::Bar::ITEM_2;
foo.doThings(bar);
}
Run Code Online (Sandbox Code Playgroud)
现在我想要做的是能够执行类似的“进口”(使用Java术语)Bar,以便能够删除需要前缀Bar用Foo::,即做Bar bar = Bar::ITEM_2;。
现在,由于我对 C++ 的了解有限,我能想到的一种方法是将所有代码包围在Foo.hwith 中namespace FooNamespace{},将Bar枚举从类中取出(但仍在命名空间中),然后添加using namespace FooNamespace到Foo2班级。但是,对于我的应用程序来说,这确实没有多大逻辑意义,因为Bar枚举确实在逻辑上属于Foo该类。
由于我精通 Java,下面是我想做的 …
C# .Net-Core 3.1
在我的 C# api 中,我在 FileStreamResult 中返回一个 pdf 文件,效果很好。
一般来说,我将流包装在 using 中,但是此代码失败并显示
Cannot access a closed Stream.
using (MemoryStream stream = new MemoryStream(byteArray))
{
fileStreamResult = new FileStreamResult(stream, "application/pdf");
}
return (ActionResult)fileStreamResult;
Run Code Online (Sandbox Code Playgroud)
所以我需要这样做:
var stream = new MemoryStream(byteArray);
fileStreamResult = new FileStreamResult(stream, "application/pdf");
return (ActionResult)fileStreamResult;
Run Code Online (Sandbox Code Playgroud)
我假设流需要保持打开状态,我应该担心内存泄漏还是 IIS 会关闭流吗?有更好的选择吗?
如果我有这样的文件,一切都会按预期进行:
#include <filesystem>
#include <iostream>
int main() {
std::filesystem::path o = "C:\\Windows\\write.exe";
auto s = o.parent_path();
std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果可能,我想使用这样的行:
filesystem::path o = "C:\\Windows\\write.exe";
Run Code Online (Sandbox Code Playgroud)
我试过这个,但我收到一个错误:
// using-declaration may not name namespace 'std::filesystem'
using std::filesystem;
Run Code Online (Sandbox Code Playgroud)
也有错误:
using namespace std::filesystem;
// error: 'filesystem' has not been declared
filesystem::path o = "C:\\Windows\\write.exe";
Run Code Online (Sandbox Code Playgroud)
有可能做我正在尝试的事情吗?
什么规则使以下代码编译无错误:
using integer = int;
struct Foo
{
int integer;
};
int main() {
Foo f;
int integer;
f.integer;
}
Run Code Online (Sandbox Code Playgroud)
using当然不是 的简单替代品#define integer int,但是是什么让这段代码看起来格式良好,而int int;会使其格式错误?
我想用它HttpClient来获取我的网页内容。
public async Task<IActionResult> OnGet()
{
NetworkCredential credentials = new NetworkCredential(Settings.Username, Settings.Password);
using (HttpClientHandler handler = new HttpClientHandler { Credentials = credentials })
using (HttpClient httpClient = new HttpClient(handler))
{
Content = await httpClient.GetStringAsync(requestUriString);
}
return Page();
}
Run Code Online (Sandbox Code Playgroud)
问题是我的using语句导致HttpClient和对象在完成HttpClientHandler之前被销毁。GetStringAsync()
我可以删除这些using语句,但我不能保证连接会及时处理。
请注意,HttpClient没有实现IAsyncDisposable,因此使用await using不是一个选项。
如何确保我的 HTTP 对象尽快得到处理,而不是在代码完成之前处理它们?
我不确定如何正确使用 using 语句。
我像这样尝试:
[HttpDelete("{ClubId}", Name = "DeleteOpenings_Club")]
public async Task<IActionResult> DeleteClub_IsOpening([FromRoute] string ClubId)
{
using (_db_ClubIsOpen)
{
var result = _db_ClubIsOpen.ClubIsOpen_TBL.Where(x => x.FK_Club == ClubId);
foreach (var item in result)
{
_db_ClubIsOpen.ClubIsOpen_TBL.Remove(item);
}
_db_ClubIsOpen.SaveChanges();
return Ok();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个非常奇怪的编译时错误,我在谷歌中检查的任何地方我都看到我的语法应该绝对没问题。
我的项目是 .net-5 并用 C# 语言编写。
public async Task Init()
{
using IEngineProviderAsync provider = await CreateProviderAsync();
}
Run Code Online (Sandbox Code Playgroud)
CS8418“IEngineProviderAsync”:using 语句中使用的类型必须可隐式转换为“system.idisposable”。您的意思是“等待使用”而不是“使用”吗?
我搜索了 CS8418 但没有数据。微软链接不起作用https://docs.microsoft.com/en-us/dotnet/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error?f1url=% 3FappId%3Droslyn%26k%3Dk(CS8418)
我想知道为任务实现处理功能是否有任何好处。这操作会有什么不同吗?这会迫使内存清理得更快吗?
Task updateTask = UpdateRemoteAsync()
await updateTask;
Run Code Online (Sandbox Code Playgroud)
VS
using (Task updateTask = UpdateRemoteAsync())
{
await updateTask;
}
Run Code Online (Sandbox Code Playgroud) 假设我们有一个名为“mytable”的表:
| 名称 [varchar] | 饮食习惯 [int] |
|---|---|
| 安娜 | 1 |
| 罗兰 | 3 |
| 塞普 | 1 |
| 凯特琳 | 2 |
| 卢卡斯 | 4 |
| 海德薇 | 3 |
现在我意识到我想将“eating_habits”列更改为具体的。我创建自己的枚举类型:
CREATE TYPE diet AS ENUM ('vegetarian', 'vegan', 'omni');
Run Code Online (Sandbox Code Playgroud)
我想要的是将“eating_habits”列的类型更改为“饮食”。为此,我还想在这样的类型之间进行映射
1 --> 'vegan'
2 --> 'vegetarian'
rest --> 'omni'
Run Code Online (Sandbox Code Playgroud)
我该怎么办呢?我知道可以像这样使用 USING 子句:
ALTER TABLE mytable
ALTER COLUMN eating_habits TYPE diet
USING (
<Expression>
)
Run Code Online (Sandbox Code Playgroud)
但我不知道“表达”应该是什么,而且网上的绝大多数例子都是琐碎的转换。
考虑以下来自Microsoft 文档的代码:
using FileStream createStream = File.Create(fileName);
// ...write to stream etc..
await createStream.DisposeAsync(); // <- isn't this done automatically because of the using clause in the first line`?
Run Code Online (Sandbox Code Playgroud)
调用该方法不是DisposeAsync()多余的吗?
using ×10
c# ×6
c++ ×3
.net ×2
namespaces ×2
.net-5 ×1
.net-core ×1
alter-table ×1
asp.net-core ×1
async-await ×1
c++17 ×1
dispose ×1
filestream ×1
httpclient ×1
memorystream ×1
postgresql ×1
razor-pages ×1
type-alias ×1