背景:Noda Time包含许多可序列化的结构.虽然我不喜欢二进制序列化,但我们收到了许多支持它的请求,回到1.x时间线.我们通过实现ISerializable接口来支持它.
我们收到了最近一份关于Noda Time 2.x 在.NET Fiddle中失败的问题报告.使用Noda Time 1.x的相同代码工作正常.抛出的异常是这样的:
重写成员时违反了继承安全规则:'NodaTime.Duration.System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext)'.覆盖方法的安全性可访问性必须与被覆盖的方法的安全性可访问性相匹配.
我把它缩小到了目标框架:1.x目标.NET 3.5(客户端配置文件); 2.x的目标是.NET 4.5.它们在支持PCL与.NET Core和项目文件结构方面存在很大差异,但看起来这是无关紧要的.
我已经设法在本地项目中重现了这一点,但我还没有找到解决方案.
在VS2017中重现的步骤:
Program.cs.这是此Microsoft示例中代码的缩写版本.我保持所有路径相同,所以如果你想回到更完整的代码,你不应该改变其他任何东西.码:
using System;
using System.Security;
using System.Security.Permissions;
class Sandboxer : MarshalByRefObject
{
static void Main()
{
var adSetup = new AppDomainSetup();
adSetup.ApplicationBase = System.IO.Path.GetFullPath(@"..\..\..\UntrustedCode\bin\Debug");
var permSet = new PermissionSet(PermissionState.None);
permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
var fullTrustAssembly = typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<System.Security.Policy.StrongName>();
var newDomain = AppDomain.CreateDomain("Sandbox", null, adSetup, permSet, fullTrustAssembly);
var handle = …Run Code Online (Sandbox Code Playgroud) 我正在重构我的库,以Span<T>尽可能避免堆分配,但是由于我也针对较旧的框架,因此我也在实现一些通用的后备解决方案。但是现在我发现了一个奇怪的问题,我不确定是在.NET Core 3中发现了错误还是在做违法的事情。
问题:
// This returns 1 as expected but cannot be used in older frameworks:
private static uint ReinterpretNew()
{
Span<byte> bytes = stackalloc byte[4];
bytes[0] = 1; // FillBytes(bytes);
// returning bytes as uint:
return Unsafe.As<byte, uint>(ref bytes.GetPinnableReference());
}
// This returns garbage in .NET Core 3.0 with release build:
private static unsafe uint ReinterpretOld()
{
byte* bytes = stackalloc byte[4];
bytes[0] = 1; // FillBytes(bytes);
// returning bytes as uint:
return *(uint*)bytes;
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,它 …
我不确定这是否是一个.NET错误,但我发现它真的很有趣.
正如所料,我不能这样做:
sbyte[] sbytes = { 1, 2, 3 };
byte[] bytes = sbytes; // fails: cannot convert source type 'sbyte[]' to taget type 'byte[]'
Run Code Online (Sandbox Code Playgroud)
但是,如果类型sbytes为object,则有效:
object obj = new sbyte[]{ 1, 2, 3 };
byte[] bytes = obj as byte[];
Assert.IsNull(bytes, "WTF??")
Run Code Online (Sandbox Code Playgroud)
备注1:同样的问题int[]- uint[]以及其他原始类型也是如此.
备注2:虽然代码将数组处理为a byte[],但调试器会丢失焦点并?在数组中显示-s.
备注3:这仅适用于数组,不适用于基础类型本身:
object sbyteObj = (sbyte)1;
byte byteObj = (byte)sbyteObj; // System.InvalidCastException: Specified cast is not valid.
Run Code Online (Sandbox Code Playgroud)
好的,我当然可以检查这样的类型:
if (obj.GetType() == …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码来制作圆角矩形.但它只绘制了rectanlge的左上角和右上角,更不能完成下部的矩形.如何使其完整和充实.我应该做些什么改变?
public static Bitmap DrawRoundedRectangle(Bitmap Image, Color BoxColor, int XPosition, int YPosition,
int Height, int Width, int CornerRadius)
{
Bitmap NewBitmap = new Bitmap(Image, Image.Width, Image.Height);
using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
{
using (Pen BoxPen = new Pen(BoxColor))
{
using (GraphicsPath Path = new GraphicsPath())
{
Path.AddLine(XPosition + CornerRadius, YPosition, XPosition + Width - (CornerRadius * 2), YPosition);
Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition, CornerRadius * 2, CornerRadius * 2, 270, 90);
Path.AddLine(XPosition + Width, YPosition + CornerRadius, …Run Code Online (Sandbox Code Playgroud) C#7.0引入了值元组,并为它们提供了一些语言级支持.他们还添加了单元素和零元素元组的支持 ; 但是,当它们有用时,我找不到任何情况.
通过ValueTuple.Create重载,我可以创建任何类型的元组,但C#7.0语法只允许至少两个元素:
Microsoft (R) Roslyn C# Compiler version 2.8.3.62923
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.
> ValueTuple.Create()
[()]
> ValueTuple.Create(1)
[(1)]
> ValueTuple.Create(1, "x")
[(1, x)]
Run Code Online (Sandbox Code Playgroud)
通过元组语法:
> var t2 = (1, 2);
> var t1 = (1); // t1 is now int (of course)
> ValueTuple<int> t1 = (1);
(1,23): error CS0029: Cannot implicitly convert type 'int' to 'ValueTuple<int>'
> ValueTuple<int> t1 = new ValueTuple<int>(1);
> t1
[(1)]
Run Code Online (Sandbox Code Playgroud)
我想我找到了 …
我尝试对 16 位整数 ARGB 通道的 64 位颜色的预乘进行矢量化。
我很快意识到,由于缺乏加速整数除法支持,我需要将我的值转换为float并显式使用一些 SSE2/SSE4.1 内在函数以获得最佳性能。尽管如此,我还是想保留非特定的通用版本作为后备解决方案(我知道它目前比某些普通操作慢,但它将提供未来可能的改进的兼容性)。
但是,在我的机器上结果不正确。
一个非常小的重现:
// Test color with 50% alpha
(ushort A, ushort R, ushort G, ushort B) c = (0x8000, 0xFFFF, 0xFFFF, 0xFFFF);
// Minimal version of the fallback logic if HW intrinsics cannot be used:
Vector128<uint> v = Vector128.Create(c.R, c.G, c.B, 0u);
v = v * c.A / Vector128.Create(0xFFFFu);
var cPre = (c.A, (ushort)v[0], (ushort)v[1], (ushort)v[2]);
// Original color:
Console.WriteLine(c); // prints (32768, 65535, 65535, 65535)
// Expected …Run Code Online (Sandbox Code Playgroud) 我刚刚在VisualStudio 2015中将项目类型更改为Portable.但似乎ReSharper无法再对标准.NET库进行反编译.
我知道.NET标准的确切实现可能在不同的平台上有所不同.但是,在没有明确访问参考源站点或使用DotPeek或类似工具的情况下,至少查看标准库的Windows版本仍然很有用.
如果项目类型是Portable,我可以以某种方式配置ReSharper来反编译其中一个实际实现(例如Windows版本)吗?
我有一个自定义的可等待类型,问题是延续在不同的线程上恢复,这会导致 WinForms/WPF/MVC/等 UI 出现问题:
private MyAwaitable awaitable;
private async void buttonStart_Click(object sender, EventArgs e)
{
awaitable = new MyAwaitable(false);
progressBar1.Visible = true;
// A regular Task can marshal the execution back to the UI thread
// Here ConfigureAwait is not available and I don't know how to control the flow
var result = await awaitable;
// As a result, here comes the usual "Cross-thread operation not valid" exception
// A [Begin]Invoke could help but regular Tasks also can handle this …Run Code Online (Sandbox Code Playgroud) 我想System.Drawing.Bitmap“手动”创建一个包含动画的实例。
Bitmap要创建的实例应满足以下条件:
image.FrameDimensionsLists有一个时间维度)image.GetFrameCount(dimension) > 1)image.GetPropertyItem(0x5100).Value)我很确定可以通过一些 WinApi 创建这样的图像。这也是 GIF 解码器的实际作用。
我知道我可以播放动画,如果我通过做手工有任何来源的框架,但我希望做一个兼容的方式:如果我能产生这样的位图,我可以简单地使用它的一个Button,Label,PictureBox或者任何其他现有控件,内置控件ImageAnimator也可以自动处理它。
大多数类似主题都建议将帧转换为动画 GIF;然而,这不是一个好的解决方案,因为它不能处理真彩色和半透明(例如 APNG 动画)。
更新:经过一些探索,我了解到我可以使用WIC实现解码器;但是,我不想在 Windows 中注册新的解码器,它使用 COM,如果可能,我想避免使用 COM。更不用说最后我会有一个IWICBitmapSource,我仍然需要将其转换为Bitmap.
更新 2:我设置了赏金。如果您可以实现以下方法,您就是赢家:
public void Bitmap CreateAnimation(Bitmap[] frames, int[] delays)
{
// Any WinApi is allowed. WIC is also allowed, but not preferred.
// Creating an animated GIF is not …Run Code Online (Sandbox Code Playgroud) 码:
public class Main{
public static void main(String[] a){
long t=24*1000*3600;
System.out.println(t*25);
System.out.println(24*1000*3600*25);
}
}
Run Code Online (Sandbox Code Playgroud)
这打印:
2160000000
-2134967296
Run Code Online (Sandbox Code Playgroud)
为什么?
感谢所有的答复.
是数字后使用L的唯一方法吗?
我试过了,(long)24*1000*3600*25但这也是消极的.