首页 > 解决方案 > 循环参考 Delphi vs C#

问题描述

我正在尝试在 Delphi 中建立一些类关系。

在 C#、PHP 等其他语言中,我对这些关系没有任何问题,但在 Delphi 中,我有一个循环引用错误。

为什么这个错误发生在 Delphi 而不是 C#?其他语言如何以及为什么可以处理循环引用而 Delphi 不能?C# 是如何处理这个问题的?

Delphi 有没有不使用接口或单机的解决方案?

德尔福

爸爸.pas

unit Dad;

interface

uses
  System.Generics.Collections, Child;

type
  TDad = class
  private
    { private declarations }
    FName: string;
    FChildren: TObjectList<TChild>;
  protected
    { protected declarations }
  public
    { public declarations }
    property Name: string read FName write FName;
    property Children: TObjectList<TChild> read FChildren write FChildren;
  end;

implementation

end.

孩子.pas

unit Child;

interface

uses
  Dad;

type
  TChild = class
  private
    { private declarations }
    FName: string;
    FDad: TDad;
  protected
    { protected declarations }
  public
    { public declarations }
    property Name: string read FName write FName;
    property Dad: TDad read FDad write FDad;
  end;

implementation

end.

夏普

爸爸.cs

using System;
using System.Collections.Generic;
using System.Text;
using CircularReferenceTest.SecondNamespace;

namespace CircularReferenceTest.FirstNamespace
{
    public class Dad
    {
        public String Name { get; set; }
        public IList<Child> Children { get; set; }
    }
}

儿童.ps

using System;
using System.Collections.Generic;
using System.Text;
using CircularReferenceTest.FirstNamespace;

namespace CircularReferenceTest.SecondNamespace
{
    public class Child
    {
        public String Name { get; set; }
        public Dad Dad { get; set; }

    }
}

标签: c#delphicircular-reference

解决方案


推荐阅读