使用附加构造函数和方法将 C# Class 转换为 F# Class 的语法
Syntax on converting a C# Class to an F# Class with additional constructor and methods
我是 F# 的新手 类。我认为这很明显,但恐怕我脑残了。此 C# 代码如何转换为 F# class?
C#代码:
public class MyStroke : System.Windows.Ink.Stroke
{
public static Guid strokeGUID = new Guid("e3b886cc-7d89-42e9-9336-dbb38f108ab2");
public static int DiagramId = 100;
public MyStroke(StylusPointCollection stylusPointCollection, PenEnum penName, DrawingAttributes drawingAttributes)
: base(stylusPointCollection, drawingAttributes)
{
var id = DiagramId;
if (penName == PenEnum.InkPen)
id = (int)penName;
AddPropertyData(strokeGUID, id);
}
public static void StartNewDiagram()
{
System.Threading.Interlocked.Increment(ref DiagramId);
}
public static StrokeCollection Label(Stroke stroke, PenEnum penName, DrawingAttributes drawingAttributes)
{
StrokeCollection sc = new System.Windows.Ink.StrokeCollection();
if (!(stroke is MyStroke ms))
ms = new MyStroke(stroke.StylusPoints, penName, drawingAttributes);
sc.Add(ms);
return sc;
}
}
感谢您的帮助。
这实际上有点棘手,但像这样应该可以做到:
open System
open System.Windows.Ink
type MyStroke(stylusPointCollection, penName : PenEnum, drawingAttributes) as this =
inherit Stroke(stylusPointCollection, drawingAttributes)
let strokeGUID = Guid("e3b886cc-7d89-42e9-9336-dbb38f108ab2")
static let mutable diagramId = 100
do
let id =
if penName = PenEnum.InkPen then
(int) penName
else diagramId
this.AddPropertyData(strokeGUID, id);
static member StartNewDiagram() =
System.Threading.Interlocked.Increment(&diagramId)
static member Label(stroke : Stroke, penName, drawingAttributes) =
let ms =
match stroke with
| :? MyStroke as ms -> ms
| _ -> MyStroke(stroke.StylusPoints, penName, drawingAttributes)
seq { ms :> Stroke }
|> StrokeCollection
警告:我还没有尝试 运行 这段代码,所以我不知道它是否真的有效。
我是 F# 的新手 类。我认为这很明显,但恐怕我脑残了。此 C# 代码如何转换为 F# class?
C#代码:
public class MyStroke : System.Windows.Ink.Stroke
{
public static Guid strokeGUID = new Guid("e3b886cc-7d89-42e9-9336-dbb38f108ab2");
public static int DiagramId = 100;
public MyStroke(StylusPointCollection stylusPointCollection, PenEnum penName, DrawingAttributes drawingAttributes)
: base(stylusPointCollection, drawingAttributes)
{
var id = DiagramId;
if (penName == PenEnum.InkPen)
id = (int)penName;
AddPropertyData(strokeGUID, id);
}
public static void StartNewDiagram()
{
System.Threading.Interlocked.Increment(ref DiagramId);
}
public static StrokeCollection Label(Stroke stroke, PenEnum penName, DrawingAttributes drawingAttributes)
{
StrokeCollection sc = new System.Windows.Ink.StrokeCollection();
if (!(stroke is MyStroke ms))
ms = new MyStroke(stroke.StylusPoints, penName, drawingAttributes);
sc.Add(ms);
return sc;
}
}
感谢您的帮助。
这实际上有点棘手,但像这样应该可以做到:
open System
open System.Windows.Ink
type MyStroke(stylusPointCollection, penName : PenEnum, drawingAttributes) as this =
inherit Stroke(stylusPointCollection, drawingAttributes)
let strokeGUID = Guid("e3b886cc-7d89-42e9-9336-dbb38f108ab2")
static let mutable diagramId = 100
do
let id =
if penName = PenEnum.InkPen then
(int) penName
else diagramId
this.AddPropertyData(strokeGUID, id);
static member StartNewDiagram() =
System.Threading.Interlocked.Increment(&diagramId)
static member Label(stroke : Stroke, penName, drawingAttributes) =
let ms =
match stroke with
| :? MyStroke as ms -> ms
| _ -> MyStroke(stroke.StylusPoints, penName, drawingAttributes)
seq { ms :> Stroke }
|> StrokeCollection
警告:我还没有尝试 运行 这段代码,所以我不知道它是否真的有效。