인터페이스에서 C # / WPF에서 사용할 F # 목록을 만드는 방법
Aug 20 2020
나는 초보자입니다. 이들 각각은 C #에서 서로 다른 클래스를 나타냅니다.
- 연락처
- 인터넷
- 전화 번호
- 구애
다양한 구체적인 유형을 보유하기 위해 F #에서 "목록"을 어떻게 생성합니까?
위의 모든 유형에는 다음과 같은 공통 필드가 있습니다.
- 이름-문자열
이름 외에 모든 구체적인 유형은 다른 필드와 내용을 갖습니다.
이 "목록"유형은 WFP / XAML에서 사용됩니다.
(F # 목록에 대한 인터페이스를 사용해야한다고 생각하지만 이것이 어떻게 구현되는지 모르겠습니다. F #은 나에게 정말 새로운 것입니다. :)
TIA
답변
3 ScottNimrod Aug 20 2020 at 19:03
C # 클라이언트를 지원하려면 List 대신 Seq를 사용하는 것이 좋습니다.
C # 사용을 위해 목록 대신 시퀀스 (즉, seq)를 사용하는 것이 좋습니다. 따라서 F #의 시퀀스는 C #의 IEnumerable과 동일합니다. 따라서 Windows 앱에서 이러한 항목을 사용할 수 있습니다.
요구 사항을 구현하는 방법은 다음과 같습니다.
type ContactDetail = { Name : string; Other:string }
type Internet = { Name : string; Other:string }
type PhoneNumber = { Name : string; Other:string }
type Address = { Name : string; Other:string }
type MyType =
| ContactDetails of ContactDetail seq
| Internet of Internet seq
| PhoneNumbers of PhoneNumber seq
| Addresses of Address seq
let contactDetail : ContactDetail = { Name="some name"; Other="???" }
let contactDetails = ContactDetails [contactDetail]
let internet : Internet = { Name="some name"; Other="???" }
let internets = Internet [internet]
let phoneNumber : PhoneNumber = { Name="some name"; Other="???" }
let PhoneNumbers = PhoneNumbers [phoneNumber]
let myTypes : MyType seq = seq [ contactDetails
internets
PhoneNumbers
]
2 AnibalYeh Aug 20 2020 at 09:33
실례합니다.이게 당신이 원하는 건가요?
에프#
module FSharpTest.ListTest
open System
type YourType = Object
type ContactDetails = YourType
type Internet = YourType
type PhoneNumbers = YourType
type Addresses = YourType
type WrapperOfCSharpClass =
| CD of ContactDetails
| I of Internet
| PN of PhoneNumbers
| A of Addresses
let list = [
Unchecked.defaultof<WrapperOfCSharpClass>
CD (new ContactDetails())
I (new Internet())
]
씨#
using System;
using FSharpTest;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var fsharplist_item = ListTest.list[0];
if (fsharplist_item.IsPN)
{
Console.WriteLine("I am a phone number");
} else if (fsharplist_item.IsA)
{
Console.WriteLine("I am an address");
}
}
}
}