小编Red*_*ood的帖子

从资源读取文件时指定编码

我有一个 UTF8 文件,我已将其添加到 Resources.resx 中的项目中,名为 Template.txt

如果我像这样正常读取文件:

string template = File.ReadAllText(@"filepath\Template.txt", Encoding.UTF8);
Run Code Online (Sandbox Code Playgroud)

一切正常。

但是,如果我这样读:

string template = Properties.Resources.Template
Run Code Online (Sandbox Code Playgroud)

它充满了日文字符,因此编码错误。

byte[] bytes = Encoding.Default.GetBytes(Properties.Resources.Template);
string template = Encoding.UTF8.GetString(bytes);
Run Code Online (Sandbox Code Playgroud)

这也仍然给出了日语字符。

有谁知道原因?如果我只是在 Visual Studio 中双击 Template.txt 文件,我也可以正常读取它。

.net c#

7
推荐指数
1
解决办法
4670
查看次数

C#使用Memory <T>或ArraySegment <T>访问非托管数组?

通过引入的Memory,Span并且ArraySegment在C#7.2,我想知道如果我可以代表一个非托管数组作为枚举对象,即生活在堆上.

后一个要求排除了Span,它基本上实现了我想要的东西:例如

unsafe { bytes = new Span<byte>((byte*)ptr + (index * Width), Width); 
Run Code Online (Sandbox Code Playgroud)

是否可以用ArraySegment或做同样的事情Memory?他们的构造函数只接受byte[],也许有某种方法可以欺骗C#byte*而不是传递byte[]

c# unmanaged-memory

7
推荐指数
1
解决办法
577
查看次数

为什么带有 EVENT_OBJECT_LOCATIONCHANGE 的 SetWinEventHook 在鼠标移动时触发?

我在进程上创建了一个钩子,以便在其窗口移动时进行注册。我使用事件常量 EVENT_OBJECT_LOCATIONCHANGE,根据MSDN

物体的位置、形状或大小发生了变化。系统为以下用户界面元素发送此事件:插入符号和窗口对象。服务器应用程序为其可访问对象发送此事件。

它可以工作,但它也会在简单的鼠标悬停在应用程序上时触发。谁能解释为什么?

这是重现它的示例:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class NativeMethods
    {
        [DllImport("user32.dll")]
        public static extern System.IntPtr SetWinEventHook(uint eventMin, uint eventMax, System.IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

        public delegate void WinEventDelegate(System.IntPtr hWinEventHook, uint eventType, System.IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
    }

    public partial class Form1 : Form
    {
        private const uint WINEVENT_OUTOFCONTEXT = 0x0000;
        private const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B;

        public Form1()
        { …
Run Code Online (Sandbox Code Playgroud)

c#

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

如何使用 DNNE 从 Inno Setup 调用 .NET DLL?

我之前曾成功使用Unmanaged ExportsDllExport与 Inno Setup 一起使用 .NET DLL 文件。

然而现在我正在尝试让它与DNNE一起工作。

我有以下针对 x86 的 C# 代码

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableDynamicLoading>true</EnableDynamicLoading>
    <Platforms>x86</Platforms>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="DNNE" Version="1.0.31" />
  </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)
using System.Runtime.InteropServices;

namespace DNNETest
{
    internal static class NativeMethods
    {
        [DllImport("User32.dll", EntryPoint = "MessageBox",
            CharSet = CharSet.Auto)]
        internal static extern int MsgBox(
            IntPtr hWnd, string lpText, string lpCaption, uint uType);
    }

    public class Class1
    {
        [UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvStdcall) })]
        public static …
Run Code Online (Sandbox Code Playgroud)

.net c# inno-setup pascalscript

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

Flex 框填充父级,但不要在溢出时拉伸父级

这是一个与Can You make a flexbox child expand to fit parent but not contents类似的问题,但是,该解决方案对我不起作用。

我有一个布局,这样整个页面应该总是适合屏幕(页眉和页脚之间),如果内容太大,那么它的容器应该滚动。(基本上最后一个例子来自:https : //css-tricks.com/snippets/css/a-guide-to-flexbox/

我已经尝试了我上面链接的问题的解决方案:

#chat {
    background-color: #ceecf5;
    flex-grow: 1;
    overflow: auto;
}
Run Code Online (Sandbox Code Playgroud)

但是父级仍然被拉出页面。

如果我为#chat它设置了手动高度,它可以正常工作,但它只适合我制作的屏幕尺寸,所以我想要一个自动高度。

#chat {
    background-color: #ceecf5;
    flex-grow: 1;
    overflow: auto;
}
Run Code Online (Sandbox Code Playgroud)
#flex-container {
  display: flex;
  flex-flow: column nowrap;
  justify-content: flex-start;
  min-width: 300px;
  flex: 1;
}
#header {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: red;
}
#column-flex-container {
  display: flex;
  flex-flow: row wrap-reverse;
  justify-content: …
Run Code Online (Sandbox Code Playgroud)

html css flexbox

5
推荐指数
1
解决办法
6860
查看次数

何时使用ArraySegment <T> over Memory <T>?

我正在研究将"视图"返回到一个非常大的数组中的最佳方法,并发现ArraySegment它非常适合我的需求.然而,我发现Memory<T>它似乎行为相同,但需要跨度来查看内存.

对于创建和写入大量(2GB +)数组的视图的用例,使用哪一个是否重要?

大数组的原因是它们保存图像的字节.

c#

5
推荐指数
1
解决办法
348
查看次数

有没有办法在异步代码中强制上下文切换而不需要 Task.Delay

我的代码看起来像这样:

我试图避免将它放在 Task.Run 中,因为它是异步的并且应该在主线程上运行良好

但是,它不会执行上下文切换(并且将永远运行),除非我将 Task.Delay 插入循环中

有没有更好的方法来实现这一点(没有 Task.Run)?

var tasks = new List<Task>();
var cts = new CancellationTokenSource();
tasks.Add(DoSomething(cts.Token));
cts.Cancel();
Task.WaitAll(tasks.ToArray());
Console.WriteLine("Done");

async Task DoSomething(CancellationToken ct)
{
    while (!ct.IsCancellationRequested)
    {
        await Task.CompletedTask;
        await Task.Delay(1); // Without this is doesn't do context switch
    }
}
Run Code Online (Sandbox Code Playgroud)

当我工作量很大并遇到此问题时,我正在尝试取消单元测试。

c# asynchronous async-await

5
推荐指数
1
解决办法
648
查看次数

Moq It.IsSubtype 没有隐式引用转换

我试图模拟一个通用方法,它有一个约束T : IFoo

但是最小起订量似乎无法理解转换并给出以下错误:

类型“Moq.It.IsSubtype<SomeNamepace.IFoo>”不能用作泛型类型或方法“IMyClass.DoSomething(Action)”中的类型参数“T”。没有从“Moq.It.IsSubtype<SomeNamepace.IFoo>”到“SomeNamepace.IFoo”的隐式引用转换。

    public interface IFoo
    {
    }

    class Foo : IFoo
    {

    }

    public interface IMyClass
    {
        public IDisposable DoSomething<T>(Action<T> asd) where T : IFoo;
    }

    public class MyTest
    {
        [Test]
        public void SomeTest()
        {
            var mock = new Mock<IMyClass>();

            mock.Setup(e => e.DoSomething(It.IsAny<Action<IFoo>>())).Returns(Mock.Of<IDisposable>());

// What I want but gives compiler error
//             mock.Setup(e => e.DoSomething(It.IsAny<Action<It.IsSubtype<IFoo>>>())).Returns(Mock.Of<IDisposable>());


// Action<IFoo> would work, but in real code its not used like that
            Action<Foo> myAction = (e) => { };
            var …
Run Code Online (Sandbox Code Playgroud)

c# moq

4
推荐指数
2
解决办法
1282
查看次数

_T宏是否会影响std :: string构造函数?

我继承了一些需要保持多字节/ unicode兼容的旧代码.它目前使用_T()宏.我想知道是否std::string引入了s _T()仍然需要吗?

例如

std::string foo(_T("hello"));
Run Code Online (Sandbox Code Playgroud)

或者它现在是多余的?

c++ visual-c++

3
推荐指数
1
解决办法
250
查看次数

与 Azure 消费函数结合使用的 Redis 连接最佳实践

Redis 最佳实践建议使用长期存在的 ConnectionMultiplexer。不过,我想在天蓝色的消费函数中使用 Redis,该函数可能只存在几秒钟(但运行很多次)。

我想知道我是否有这样的代码:

        private static Lazy<ConnectionMultiplexer> lazyRedisConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        string cacheConnection = ConfigurationManager.AppSettings["RedisKey"].ToString();
        return ConnectionMultiplexer.Connect(cacheConnection);
    });

    public static ConnectionMultiplexer RedisConnection
    {
        get
        {
            return lazyRedisConnection.Value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在 Azure 消耗函数上,运行例如 10000 次。由于 Azure 消耗函数的工作方式,这实际上会创建 10000 个连接,而不是重用单个连接?

为每个函数手动创建/处置连接会更安全吗?

redis azure-functions

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

使用带有socket.io的箭头函数

我想知道如何使用箭头函数来替换bind.我的理解是我可以使用箭头函数来词法调用this函数,但函数甚至不再被调用.

奇怪的是我没有得到任何错误,如果我使用箭头功能它只是默默地失败.

constructor(socket: SocketIO.Socket
{
    // Works
    socket.on(this.onLogin.name, this.onLogin.bind(this));

    // Doesn't work?
    socket.on(this.onLogin.name, (data: LoginDetails) => this.onLogin);
}


public onLogin(loginDetails: LoginDetails) {
    console.log(this.onLogin.name + " " + this.socketID);
}
Run Code Online (Sandbox Code Playgroud)

javascript typescript

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

当排除扩展接口时,打字稿为什么给出类型“从不”

我创建了一个剥离接口对象的函数。但是Typescript(版本3.2.2)现在声称该类型是never应该具有属性的类型child

interface Child extends Parent {
    child: string
}

interface Parent {
    parent: string
}

const a: Child = { child: "", parent: "" }

const b = removeParent(a);

function removeParent<T extends Parent>(obj: T) {
    delete obj.parent;
    return obj as Exclude<T, Parent>;
}

// b is now type never...
Run Code Online (Sandbox Code Playgroud)

这确实有效:

function removeParent<T extends Parent>(obj: T) {
    delete obj.parent;
    type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
    return obj as Without<T, "parent">;
}
Run Code Online (Sandbox Code Playgroud)

但是我想要一个通用的解决方案,不需要我写出要排除的类型。

typescript

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

如何在 Cortex M4 上设置辅助控制寄存器位

我在 Cortex M4 上运行的应用程序因硬故障而崩溃。CSFR 寄存器指示IMPRECISERR.

阅读http://chmorgan.blogspot.nl/2013/06/debugging-imprecise-bus-access-fault-on.html建议我设置DISDEFWBUF辅助控制寄存器 (ACTLR) 中的位。这将使我能够获得PRECISERR更容易调试的信息。

cortex-m

0
推荐指数
1
解决办法
944
查看次数