在C#中,我可以使用两个不同的类型参数在一个类上实现两次泛型接口:
interface IFoo<T> { void Foo(T x); }
class Bar : IFoo<int>, IFoo<float>
{
public void Foo(int x) { }
public void Foo(float y) { }
}
Run Code Online (Sandbox Code Playgroud)
我想在F#中做同样的事情:
type IFoo<'a> = abstract member Foo : 'a -> unit
type Bar() =
interface IFoo<int> with
[<OverloadID("int")>]
member this.Foo x = ()
interface IFoo<float> with
[<OverloadID("float")>]
member this.Foo x = ()
Run Code Online (Sandbox Code Playgroud)
但它给出了编译器错误:
这种类型的实现或继承在不同类属实例相同的接口
'IFoo<float>'和'IFoo<int>'.在此版本的F#中不允许这样做.
这是一个C#程序,它尝试Marshal.SizeOf几种不同的类型:
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
class AClass { }
[StructLayout(LayoutKind.Sequential)]
struct AStruct { }
[StructLayout(LayoutKind.Sequential)]
class B { AClass value; }
[StructLayout(LayoutKind.Sequential)]
class C<T> { T value; }
class Program
{
static void M(object o) { Console.WriteLine(Marshal.SizeOf(o)); }
static void Main()
{
M(new AClass());
M(new AStruct());
M(new B());
M(new C<AStruct>());
M(new C<AClass>());
}
}
Run Code Online (Sandbox Code Playgroud)
对M()的前四次调用成功,但在最后一次调用时,SizeOf会抛出ArgumentException:
"Type 'C`1[AClass]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed."
Run Code Online (Sandbox Code Playgroud)
为什么?具体来说,为什么SizeOf会打开C<AClass>,但不会打开B或打开 …
我正在尝试学习WPF,所以这是一个简单的问题,我希望:
我有一个窗口,其中包含一个Image元素,该元素绑定到具有用户可配置Stretch属性的单独数据对象
<Image Name="imageCtrl" Source="{Binding MyImage}" Stretch="{Binding ImageStretch}" />
Run Code Online (Sandbox Code Playgroud)
当用户将鼠标移动到图像上时,我想确定鼠标相对于原始图像的坐标(在控件中显示时进行拉伸/裁剪之前),然后使用这些坐标执行某些操作(更新图像).
我知道我可以通过Image控件向MouseMove事件添加一个事件处理程序,但我不确定如何最好地转换坐标:
void imageCtrl_MouseMove(object sender, MouseEventArgs e)
{
Point locationInControl = e.GetPosition(imageCtrl);
Point locationInImage = ???
updateImage(locationInImage);
}
Run Code Online (Sandbox Code Playgroud)
现在我知道我可以比较的大小Source的ActualSize控制,然后打开imageCtrl.Stretch计算在X和Y的标量和偏移,并做了改造自己.但是WPF已经拥有了所有的信息,这似乎可能是内置于某个WPF库的功能.所以我想知道:有一个短暂而甜蜜的解决方案吗?或者我自己需要写这个?
编辑我正在追加我目前的,不那么短而且甜蜜的解决方案.它并没有那么糟糕,但如果WPF没有自动提供此功能,我会感到有些惊讶:
Point ImgControlCoordsToPixelCoords(Point locInCtrl,
double imgCtrlActualWidth, double imgCtrlActualHeight)
{
if (ImageStretch == Stretch.None)
return locInCtrl;
Size renderSize = new Size(imgCtrlActualWidth, imgCtrlActualHeight);
Size sourceSize = bitmap.Size;
double xZoom = renderSize.Width / sourceSize.Width;
double yZoom = renderSize.Height / sourceSize.Height;
if (ImageStretch == Stretch.Fill) …Run Code Online (Sandbox Code Playgroud) 在C#中,我可以编译
static class Foo<T> { /* static members that use T */ }
Run Code Online (Sandbox Code Playgroud)
结果是通用的,不可实例化.
什么是等效的F#代码? module<'a>不编译,type Foo<'a>可以实例化.
当我使用F#读取public readonlyC#程序集中定义的结构类型的成员时,我遇到了一个奇怪的错误.
// C#: compile to Lib.dll
namespace Lib
{
public class MyClass { public readonly int ReadonlyFoo; }
public struct MyStruct
{
public readonly int ReadonlyFoo;
public int WriteableFoo;
}
}
// F#: compile to Client.exe
open Lib
let myClass = new MyClass()
printfn "MyClass.ReadonlyFoo = %x" myClass.ReadonlyFoo
let myStruct = new MyStruct()
printfn "MyStruct.WriteableFoo = %x" myStruct.WriteableFoo
printfn "MyStruct.ReadonlyFoo = %x" myStruct.ReadonlyFoo
Run Code Online (Sandbox Code Playgroud)
当我使用F#1.9.6.16编译Client.exe时,最后一行给出错误:
"The address of the variable 'copyOfStruct' may not be used at this point" …Run Code Online (Sandbox Code Playgroud)