小编Ron*_*ein的帖子

我可以使用反射更改C#中的私有只读字段吗?

我想知道,因为可以使用反射完成很多事情,我可以在构造函数完成执行后更改私有只读字段吗?
(注意:只是好奇心)

public class Foo
{
 private readonly int bar;

 public Foo(int num)
 {
  bar = num;
 }

 public int GetBar()
 {
  return bar;
 }
}

Foo foo = new Foo(123);
Console.WriteLine(foo.GetBar()); // display 123
// reflection code here...
Console.WriteLine(foo.GetBar()); // display 456
Run Code Online (Sandbox Code Playgroud)

c# reflection field readonly

111
推荐指数
5
解决办法
5万
查看次数

如何获取当前的PowerShell执行文件?

注意:PowerShell 1.0
我想获取当前正在执行的PowerShell文件名.也就是说,如果我像这样开始我的会话:

powershell.exe .\myfile.ps1
Run Code Online (Sandbox Code Playgroud)

我想得到字符串".\ myfile.ps1"(或类似的东西).编辑:"myfile.ps1"是首选.
有任何想法吗?

powershell

86
推荐指数
8
解决办法
12万
查看次数

流作为WCF中的返回值 - 谁处置它?

假设我有以下WCF实现:

public Stream Download(string path)
{
    FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
    return stream;
}
Run Code Online (Sandbox Code Playgroud)

谁负责处理退回的价值?毕竟,可能会发生网络故障,因此消费者可能无法处置它.

.net wcf idisposable stream

53
推荐指数
2
解决办法
8387
查看次数

Varchar上的索引是否会产生性能差异?

varchar列上的索引是否使查询运行得更慢?我可以把它变成int.而且我不需要进行LIKE%比较.

mysql sql indexing performance varchar

38
推荐指数
3
解决办法
5万
查看次数

如何在Console应用程序中使用System.Web.Caching.Cache?

上下文:.Net 3.5,C#
我想在我的控制台应用程序中使用缓存机制.
我想使用System.Web.Caching.Cache(而这是最后的决定,我不能使用其他缓存框架,不要问为什么),而不是重新发明轮子.
但是,它看起来System.Web.Caching.Cache应该只在有效的HTTP上下文中运行.我非常简单的代码片段如下所示:

using System;
using System.Web.Caching;
using System.Web;

Cache c = new Cache();

try
{
    c.Insert("a", 123);
}
catch (Exception ex)
{
    Console.WriteLine("cannot insert to cache, exception:");
    Console.WriteLine(ex);
}
Run Code Online (Sandbox Code Playgroud)

结果是:

cannot insert to cache, exception:
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Web.Caching.Cache.Insert(String key, Object value)
   at MyClass.RunSnippet()

显然,我在这里做错了.有任何想法吗?


更新:+1到大多数答案,通过静态方法获取缓存是正确的用法,即HttpRuntime.CacheHttpContext.Current.Cache.谢谢你们!

.net caching console-application httpcontext

29
推荐指数
3
解决办法
3万
查看次数

bin文件夹中的任何文件中的任何更改是否会导致ASP.NET Web应用程序中的应用程

我知道在ASP.NET Web应用程序中,更改位于该bin文件夹中的DLL文件会导致应用程序回收.

但我想,正如主题所暗示的那样,是否有任何文件更改会导致此类行为?这个场景中是否包含简单的文本文件?

那么子文件夹到bin文件夹呢?他们的内容?

我知道我可以自己尝试一下,但更重要的是,我正在为此寻找合适的文档.

asp.net web-applications bin application-restart

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

c ++/cli将(托管)委托传递给非托管代码

如何将函数指针从托管C++(C++/CLI)传递给非托管方法?我读了一些文章,比如MSDN中的这篇文章,但它描述了两个不同的程序集,而我只想要一个.

这是我的代码:

1)标题(MyInterop.ManagedCppLib.h):

#pragma once

using namespace System;

namespace MyInterop { namespace ManagedCppLib {

    public ref class MyManagedClass
    {
    public:
        void DoSomething();
    };
}}
Run Code Online (Sandbox Code Playgroud)

2)CPP代码(MyInterop.ManagedCppLib.cpp)

#include "stdafx.h"
#include "MyInterop.ManagedCppLib.h"

#pragma unmanaged
void UnmanagedMethod(int a, int b, void (*sum)(const int))
{
    int result = a + b;
    sum(result);
}

#pragma managed
void MyInterop::ManagedCppLib::MyManagedClass::DoSomething()
{
    System::Console::WriteLine("hello from managed C++");
    UnmanagedMethod(3, 7, /* ANY IDEA??? */);
}
Run Code Online (Sandbox Code Playgroud)

我尝试创建我的托管委托,然后我尝试使用Marshal::GetFunctionPointerForDelegate方法,但我无法编译.

delegates interop c++-cli function-pointers

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

如何编写在C#中实现给定接口的通用容器类?

上下文:.NET 3.5,VS2008.我不确定这个问题的标题,所以也可以自由评论标题:-)

这是场景:我有几个类,比如Foo和Bar,它们都实现了以下接口:

public interface IStartable
{
    void Start();
    void Stop();
}
Run Code Online (Sandbox Code Playgroud)

现在我想要一个容器类,它在构造函数中获取一个IEnumerable <IStartable>作为参数.反过来,这个类也应该实现IStartable接口:

public class StartableGroup : IStartable // this is the container class
{
    private readonly IEnumerable<IStartable> startables;

    public StartableGroup(IEnumerable<IStartable> startables)
    {
        this.startables = startables;
    }

    public void Start()
    {
        foreach (var startable in startables)
        {
            startable.Start();
        }
    }

    public void Stop()
    {
        foreach (var startable in startables)
        {
            startable.Stop();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:如果不手动编写代码,并且没有代码生成,我怎么能这样做呢?换句话说,我想要像以下一样.

var arr = new IStartable[] { new Foo(), new Bar("wow") };
var mygroup = …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection containers interface

25
推荐指数
1
解决办法
5963
查看次数

Encoding.UTF8.GetString没有考虑Preamble/BOM

在.NET中,我正在尝试使用Encoding.UTF8.GetString方法,它接受一个字节数组并将其转换为string.

看起来这种方法忽略了BOM(字节顺序标记),它可能是UTF8字符串的合法二进制表示的一部分,并将其作为字符.

我知道我可以TextReader根据需要使用a 来消化BOM,但我认为GetString方法应该是某种使我们的代码更短的宏.

我错过了什么吗?这是故意的吗?

这是一个复制代码:

static void Main(string[] args)
{
    string s1 = "abc";
    byte[] abcWithBom;
    using (var ms = new MemoryStream())
    using (var sw = new StreamWriter(ms, new UTF8Encoding(true)))
    {
        sw.Write(s1);
        sw.Flush();
        abcWithBom = ms.ToArray();
        Console.WriteLine(FormatArray(abcWithBom)); // ef, bb, bf, 61, 62, 63
    }

    byte[] abcWithoutBom;
    using (var ms = new MemoryStream())
    using (var sw = new StreamWriter(ms, new UTF8Encoding(false)))
    {
        sw.Write(s1);
        sw.Flush();
        abcWithoutBom = ms.ToArray();
        Console.WriteLine(FormatArray(abcWithoutBom)); // 61, 62, …
Run Code Online (Sandbox Code Playgroud)

.net unicode byte-order-mark character-encoding

25
推荐指数
3
解决办法
9281
查看次数

C#HttpWebRequest SEC_I_RENEGOTIATE间歇性错误

我正在使用C#(.Net framework 3.5)应用程序中的SSL POST调用来处理登录/注销功能.通过HttpWebRequest :: BeginGetResponse()从服务器获取响应的工作时间为80%,但另外20%是间歇性抛出:

The request was aborted: Could not create SSL/TLS secure channel.
Run Code Online (Sandbox Code Playgroud)

我使用另一个问题中的建议文章启用了SSL跟踪.这在请求跟踪中产生了两种不同的模式.

似乎在执行期间,错误:

System.Net Error: 0 : [3680] Decrypt returned SEC_I_RENEGOTIATE.
Run Code Online (Sandbox Code Playgroud)

正在接收,导致重新启动安全上下文.当发生这种情况并且成功时,这是输出(注意我省略了实际地址):

System.Net Error: 0 : [3680] Decrypt returned SEC_I_RENEGOTIATE.
System.Net Information: 0 : [3680] InitializeSecurityContext(credential =   System.Net.SafeFreeCredential_SECURITY, context = 4bec0d0:4c0a8a8, targetName = [omitted].com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [3680] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=78, returned code=ContinueNeeded).
System.Net Information: 0 : [7148] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 4bec0d0:4c0a8a8, …
Run Code Online (Sandbox Code Playgroud)

c# error-handling ssl httpwebrequest

23
推荐指数
1
解决办法
9339
查看次数