标签: using

是否可以在C#中强制使用using语句

我真的希望能够使用某种类型的属性来装饰我的类,这将强制使用using语句,以便始终安全地处理类并避免内存泄漏.有人知道这种技术吗?

c# using

6
推荐指数
1
解决办法
369
查看次数

是否可以在静态类函数[C++]上使用'using'声明?

这合法吗?



class SomeClass {
public:
  static void f();
};

using SomeClass::f;

Run Code Online (Sandbox Code Playgroud)

编辑:我忘记了限定功能.抱歉.

c++ using

6
推荐指数
1
解决办法
843
查看次数

c# - 可以在"using"语句中为Web请求嵌入"try/catch"吗?我的代码是否正确?

是否可以在"using"语句中为Web请求嵌入"try/catch"?我的代码是否正确?那是我的要求是:

  1. 想要使用"using"语句来确保在任何情况下都为HttpWebResponse释放资源

    • 但是如果有异常的HttpWebResponse和"response =(HttpWebResponse)request.GetResponse();"仍然想做一些自定义的东西.特别是.

我的源代码:

        var result = new HttpHeaderInfo();
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        using (response)
        {
            try
            {
                request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "HEAD";
                request.KeepAlive = false;
                request.Timeout = Properties.Settings.Default.WebTimeoutDefault;

                response = (HttpWebResponse)request.GetResponse();
                result.LastModified = response.LastModified;
                result.ContentType = response.ContentType;
                result.StatusCode = response.StatusCode;
                result.ContentLength = response.ContentLength;
            }
            catch (Exception ex)
            {
                if (ex is InvalidOperationException ||
                    ex is ProtocolViolationException ||
                    ex is WebException)
                {
                    result.HttpError = ex;
                    result.LastModified = System.DateTime.MinValue;
                    result.ContentType = null;
                }
                else { …
Run Code Online (Sandbox Code Playgroud)

c# using try-catch httpwebrequest

6
推荐指数
2
解决办法
1万
查看次数

Python相当于C#的using语句

可能重复:
IronPython中C#"using"块的等价物是什么?

我正在使用一些一次性.NET对象编写一些IronPython,并想知道是否有一种很好的"pythonic"方式.目前我有一堆finally语句(我想在每个语句中都应该检查None)或者如果构造函数失败,变量是否甚至不存在?)

def Save(self):
    filename = "record.txt"
    data = "{0}:{1}".format(self.Level,self.Name)
    isf = IsolatedStorageFile.GetUserStoreForApplication()
    try:                
        isfs = IsolatedStorageFileStream(filename, FileMode.Create, isf)
        try:
            sw = StreamWriter(isfs)
            try:
                sw.Write(data)
            finally:
                sw.Dispose()
        finally:
            isfs.Dispose()
    finally:
        isf.Dispose()
Run Code Online (Sandbox Code Playgroud)

python ironpython using

6
推荐指数
1
解决办法
4722
查看次数

在任务中使用语句

在ASP.NET Web API控制器中,我想返回一个图像.为了流式传输图像,我需要一个MemoryStream.通常我会把它包装在一个using声明中,以确保它之后得到妥善处理.但是,因为这在异步中执行Task不起作用:

public class ImagesController : ApiController
{
    private HttpContent GetPngBitmap(Stream stream)
    {
        var pngBitmapEncoder = new PngBitmapEncoder();
        pngBitmapEncoder.Save(stream);
        stream.Seek(0, SeekOrigin.Begin);
        return new StreamContent(stream);
    }

    // GET api/images
    public Task<HttpResponseMessage> Get(string id, string path)
    {            
        //do stuff           
        return Task.Factory.StartNew(() =>
        {
            var stream = new MemoryStream(); //as it's asynchronous we can't use a using statement here!
            {
                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = GetPngBitmap(stream)
                };
                response.Content.Headers.ContentType =
                    new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
                return response; …
Run Code Online (Sandbox Code Playgroud)

c# using task-parallel-library asp.net-web-api

6
推荐指数
1
解决办法
1731
查看次数

'use strict'不停止在功能范围内吊装

我的问题在这里,我正在学习JavaScript,但根本不是编程新手.我理解提升,但是使用严格模式不应该产生错误,并且当6被分配给未声明的变量或文件时被捕获.getElement ...被分配x这不会产生错误所以我的诊断就是提升仍在继续...我不喜欢并想要摆脱使用严格.使用Chrome版本42.0.2311.152 m作为我的浏览器

function strictMode(){
    'use strict';
    try {
        x = 6;
        document.getElementById('hoisting').innerHTML = x;
        var x;
     }
     catch(err) {
                    document.getElementById('error_report').innerHTML = 
                        "There was an error that occured (Were in Strict Mode)" +
                            " " + err.message;
               }
}
Run Code Online (Sandbox Code Playgroud)

javascript using strict hoisting

6
推荐指数
1
解决办法
3768
查看次数

Haxe中的`using`关键字是什么?

我经常看到人们using在他们的Haxe代码中使用关键字.它似乎追随了这些import陈述.

例如,我发现这是一个代码片段:

import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using haxe.macro.Tools;
using Lambda;
Run Code Online (Sandbox Code Playgroud)

它做什么以及如何工作?

using haxe

6
推荐指数
1
解决办法
639
查看次数

结构类型的深层副本是否也在"使用......"块中处理?

假设我有一个实现IDisposible的结构类型,如果我使用下面的代码:

using (MyStruct ms = new MyStruct())
{
     InnerAction(ms);   //Notice "InnerAction" is "InnerAction(MyStruct ms)"
}
Run Code Online (Sandbox Code Playgroud)

当然我看到在使用块之后,ms被处理掉了.然而,"InnerAction"中的结构呢?它是否仍然存在,因为深层复制或它也处置?

如果它仍然存活(未处理),我必须使用"ref"作为"InnerAction"吗?

请给我你的证明:)

大家好.

c# struct idisposable using

6
推荐指数
1
解决办法
140
查看次数

如何使用"使用"功能?

如何使用"使用"功能?例如

class A;
void f(int);

struct B
{
   using BA = A;
   using Bf = f; ???
};
Run Code Online (Sandbox Code Playgroud)

c++ using c++11

6
推荐指数
1
解决办法
475
查看次数

C#"使用"块

我有类似下面的代码...有人在这里提到WebClient,Stream和StreamReader对象都可以从使用块中受益.两个简单的问题:

1:这个小片段看起来如何使用块?我做自己的研究没有问题,所以资源链接很好但是看到一个例子会更快更容易,我会从中理解它.

2:我想养成良好的编码标准的习惯,如果我对使用积木更好的原因有所了解会有所帮助...是否只是让你不必担心关闭或在那里更多原因?谢谢!

WebClient client = new WebClient();
Stream stream = client.OpenRead(originGetterURL);
StreamReader reader = new StreamReader(stream);

JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
string encryptionKey = (string)jObject["key"];
string originURL = (string)jObject["origin_url"];

stream.Close()
reader.Close()
Run Code Online (Sandbox Code Playgroud)

c# design-patterns using stream streamreader

5
推荐指数
2
解决办法
1663
查看次数