我有一个 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 文件,我也可以正常读取它。
通过引入的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[]?
我在进程上创建了一个钩子,以便在其窗口移动时进行注册。我使用事件常量 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) 我之前曾成功使用Unmanaged Exports和DllExport与 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) 这是一个与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)我正在研究将"视图"返回到一个非常大的数组中的最佳方法,并发现ArraySegment它非常适合我的需求.然而,我发现Memory<T>它似乎行为相同,但需要跨度来查看内存.
对于创建和写入大量(2GB +)数组的视图的用例,使用哪一个是否重要?
大数组的原因是它们保存图像的字节.
我的代码看起来像这样:
我试图避免将它放在 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)
当我工作量很大并遇到此问题时,我正在尝试取消单元测试。
我试图模拟一个通用方法,它有一个约束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) 我继承了一些需要保持多字节/ unicode兼容的旧代码.它目前使用_T()宏.我想知道是否std::string引入了s _T()仍然需要吗?
例如
std::string foo(_T("hello"));
Run Code Online (Sandbox Code Playgroud)
或者它现在是多余的?
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 个连接,而不是重用单个连接?
为每个函数手动创建/处置连接会更安全吗?
我想知道如何使用箭头函数来替换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) 我创建了一个剥离接口对象的函数。但是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)
但是我想要一个通用的解决方案,不需要我写出要排除的类型。
我在 Cortex M4 上运行的应用程序因硬故障而崩溃。CSFR 寄存器指示IMPRECISERR.
阅读http://chmorgan.blogspot.nl/2013/06/debugging-imprecise-bus-access-fault-on.html建议我设置DISDEFWBUF辅助控制寄存器 (ACTLR) 中的位。这将使我能够获得PRECISERR更容易调试的信息。
c# ×7
.net ×2
typescript ×2
async-await ×1
asynchronous ×1
c++ ×1
cortex-m ×1
css ×1
flexbox ×1
html ×1
inno-setup ×1
javascript ×1
moq ×1
pascalscript ×1
redis ×1
visual-c++ ×1