在C#中,可以通过创建扩展方法
public static class MyExtensions {
public static ReturnType MyExt(this ExtType ext) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
由于我的所有库都是用C++/CLI编写的,我想在C++/CLI中创建.net扩展方法(为了拥有一个DLL而不是两个).我试过以下代码
static public ref class MyExtensions {
public:
static ReturnType^ MyExt(this ExtType ^ext) {
...
}
};
Run Code Online (Sandbox Code Playgroud)
但编译器无法在第一个参数中识别关键字"this".
error C2059: syntax error: 'this'
Run Code Online (Sandbox Code Playgroud)
有没有办法在C++/CLI中创建扩展方法?
我可以将任何InputStream写入FileChannel吗?
我正在使用java.nio.channels.FileChannel打开文件并将其锁定,然后将InputStream写入输出文件.InputStream可以由另一个文件,URL,套接字或任何东西打开.我写了以下代码:
FileOutputStream outputStream = new FileOutputStream(outputFile);
FileChannel outputChannel = outputStream.getChannel();
FileLock lock = outputChannel.lock();
try {
outputChannel.transferFrom(???);
} finally {
lock.release();
outputChannel.close();
outputStream.close();
}
Run Code Online (Sandbox Code Playgroud)
但是,outputChannel.transferFrom(...)的第一个参数请求ReadableByteChannel对象.由于我使用InputStream作为输入,因此它没有inputStream.getChannel()方法来创建所需的通道.
有没有办法从InputStream获取ReadableByteChannel?
我用一个名为project和device的资源创建了一个安静的Web服务.它旨在使用"/ project/{projectId}/device/{deviceId}"之类的URI来访问项目中的特定设备.但是,如果设备不属于项目,则没有意义.如果projectId和deviceId不匹配,系统应返回什么状态代码?
HTTP 400错误请求:不太可能,因为请求的语法是正确的.
HTTP 403 Forbidden:很可能,但是当项目或设备被禁用时,我想返回403.
未找到HTTP 404:projectId和deviceId都存在.他们只是不匹配.