标签: circular-reference

循环引用是否必要?

我继承了一个Visual Studio解决方案,它包含了Projects之间的许多循环引用.

是否存在远程可接受的情况?

试图证实我怀疑这个应用程序设计可怕.提前致谢.

circular-dependency circular-reference visual-studio

6
推荐指数
1
解决办法
609
查看次数

序列化代码示例中的无限循环

这里查看以下代码.
它是关于在wcf中序列化时在数据提取(对象模型,对象图,域模型)中保留循环引用.

class ReferencePreservingDataContractSerializerOperationBehavior
      :DataContractSerializerOperationBehavior
    {
        public ReferencePreservingDataContractSerializerOperationBehavior(
          OperationDescription operationDescription)
          : base(operationDescription) { }

        public override XmlObjectSerializer CreateSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }

        private static XmlObjectSerializer CreateDataContractSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }

        public override XmlObjectSerializer CreateSerializer(
          Type type, XmlDictionaryString name, XmlDictionaryString ns,
          IList<Type> knownTypes)
        {
            return new DataContractSerializer(type, name, ns, knownTypes,
                0x7FFF /*maxItemsInObjectGraph*/,
                false/*ignoreExtensionDataObject*/,
                true/*preserveObjectReferences*/,
                null/*dataContractSurrogate*/);
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c# recursion wcf circular-reference

6
推荐指数
1
解决办法
746
查看次数

Javascript内存泄漏:为什么将对象分配给null工作?

关于用于防止内存泄漏的赋值到空的修复的性质,有人可以为我抓痒吗?

我们都熟悉以下技术来停止DOM对象和JS对象之间的循环引用,以防止内存泄漏:

    function foo() {
      var ele = document.getElementById("someParagraphId");

      ele.onclick = function() {
        //some action here
      };

      ele = null;
    }
Run Code Online (Sandbox Code Playgroud)

问题是为什么上述工作?将"ele"设置为null肯定会停止循环引用,但它是否也会阻止将来引用"ele"?

    function foo() {
      var ele = document.getElementById("someParagraphId");

          ele.onclick = function() {
              console.log("accessing ele ... after set to null " + ele.onclick);
          };

      ele = null;

    }
Run Code Online (Sandbox Code Playgroud)

然而事件听众却开火了.它会抱怨"ele"对象为null(这是我们所期望的).

鉴于上述行为,我们是否正确推断Javascript引擎实现将保留对事件侦听器的某种内部引用,并且在触发事件时调用此引用?

eventHandlerRef = //assignment to "ele.onclick" handler/listener;
Run Code Online (Sandbox Code Playgroud)

如果有如上所述的引用,那么赋值为null的修复是否依赖于实现?或者,它是ECMAScript规范的一部分.

从我的理解,这个修复一直是跨浏览器安全的.在应用null赋值之前,我没有遇到过许多关于检测/嗅探浏览器类型的具体提及的示例.

===============编辑==================

我认为由于我提出的方式,问题可能会在不知不觉中直接从我试图传达的内容中进行讨论.正在引用的几个概念:

对象句柄/对象引用ala:

 var obj1, obj2;
     obj1 = {foo: "bar"}; //obj1 points to the an area of the heap allocated 
                          //for the …
Run Code Online (Sandbox Code Playgroud)

javascript javascript-events circular-reference

6
推荐指数
2
解决办法
2621
查看次数

如何初始化const循环引用成员

例如,我有两个班

class Foo;
class Bar;

class Foo {
  const Bar &m_bar;
  ...
};

class Bar {
  const Foo &m_foo;
  ...
};
Run Code Online (Sandbox Code Playgroud)

让我们foo为对象Foobar为对象Bar.有什么办法(正常或"黑客")创建/初始化foobar其成员m_bar,并m_foo会互相引用(我的意思foo.m_barbarbar.m_foo是"富")?

允许添加任何成员,FooBar为其添加父项,以使其成为模板等.

c++ initialization circular-dependency circular-reference member-initialization

6
推荐指数
1
解决办法
572
查看次数

在 PostgreSQL 表中查找循环引用?

我有一张带有属性的表(ID int、SourceID int、TargetID int、TargetType int)

ID 源ID 目标ID
--------------------
1 123 456  
2 456 789  
3 1 123  
4 456 1   
5 2 1   

我想找出所有循环引用。我想为此编写 PL/pgsql 函数。

这里 ID 4 的循环引用 = 456 1 123 456

我想找到这样的例子。谁能建议我如何进行此操作。

sql postgresql plpgsql circular-reference postgresql-9.1

6
推荐指数
1
解决办法
2390
查看次数

Xamarin垃圾收集器和循环引用

"性能"部分阅读Xamarin文档时,我注意到以下章节:

下图说明了强引用可能出现的问题:

循环参考

对象A具有对对象B的强引用,对象B具有对对象A 的强引用.由于存在圆形强引用,这些对象被称为不朽对象.这种父子关系并不罕见,因此,即使对象不再被应用程序使用,垃圾收集器也不能回收任何对象.

这是我第一次在C#/ .NET/Mono语境中听说过"不朽的对象".

然后页面继续建议使用WeakReference其中一个对象,这将删除强循环引用并修复此"问题".

与此同时,Xamarin关于垃圾收集的文档声称:

Xamarin.Android使用Mono的Simple Generational垃圾收集器.这是一个标记和清除垃圾收集器[...]

标记和扫描GC不受循环引用的影响吗?

mono garbage-collection weak-references circular-reference xamarin

6
推荐指数
1
解决办法
682
查看次数

为什么c#中的名称空间允许循环依赖?

在c#中,您可以在文件a.cs中具有语句(其具有MyApp.A的命名空间):

using MyApp.B;
Run Code Online (Sandbox Code Playgroud)

而文件b.cs(具有MyApp.B的命名空间)已经有了该语句

using MyApp.A;
Run Code Online (Sandbox Code Playgroud)

如果在不同的dll中存在类似的依赖关系(其中a.dll具有对b.dll的引用,反之亦然),由于循环依赖性错误,它将不被允许,所以为什么它允许使用命名空间(并且编译器不允许)甚至产生警告)?无论如何,这不是代码味道吗?

c# namespaces circular-dependency circular-reference

6
推荐指数
1
解决办法
1035
查看次数

事件循环

我发现自己经常处于以下情况:

我有一个绑定到一些数据的用户控件。无论何时更新控件,都会更新基础数据。每当更新基础数据时,控件都会更新。所以很容易陷入永无止境的更新循环(控制更新数据、数据更新控制、控制更新数据等)。

通常我通过使用 bool(例如updatedByUser)来解决这个问题,因此我知道控件是否已以编程方式或由用户更新,然后我可以决定是否触发事件以更新基础数据。这看起来不太整洁。

是否有一些处理此类场景的最佳实践?

编辑:我添加了以下代码示例,但我想我已经回答了我自己的问题......?

public partial class View : UserControl
{
    private Model model = new Model();

    public View()
    {
        InitializeComponent();
    }

    public event EventHandler<Model> DataUpdated;

    public Model Model
    {
        get
        {
            return model;
        }
        set
        {
            if (value != null)
            {
                model = value;
                UpdateTextBoxes();
            }
        }
    }

    private void UpdateTextBoxes()
    {
        if (InvokeRequired)
        {
            Invoke(new Action(() => UpdateTextBoxes()));
        }
        else
        {
            textBox1.Text = model.Text1;
            textBox2.Text = model.Text2;
        }
    }

    private void textBox1_TextChanged(object sender, EventArgs …
Run Code Online (Sandbox Code Playgroud)

c# user-interface circular-reference winforms

6
推荐指数
1
解决办法
675
查看次数

在列表中查找循环引用的最有效方法

给出以下重定向列表

[
    {
        "old": "a",
        "target": "b"
    },
    {
        "old": "b",
        "target": "c"
    },
    {
        "old": "c",
        "target": "d"
    },
    {
        "old": "d",
        "target": "a"
    },
    {
        "old": "o",
        "target": "n"
    },
    {
        "old": "n",
        "target": "b"
    },
    {
        "old": "j",
        "target": "x"
    },
    {
        "old": "whatever",
        "target": "something"
    }
]
Run Code Online (Sandbox Code Playgroud)

在这里我们可以看到第一个项目“a”应该重定向到“b”。如果我们遵循该列表,我们可以看到以下模式:

a -> b
b -> c
c -> d
d -> a
Run Code Online (Sandbox Code Playgroud)

因此,我们最终会得到一个循环引用,因为“a”最终会指向“d”,而“d”会指向“a”。

查找循环引用的最有效方法是什么?

我用 C# 提出了以下算法

var items = JsonConvert.DeserializeObject<IEnumerable<Item>>(json)
    .GroupBy(x => x.Old)
    .Select(x => x.First())
    .ToDictionary(x …
Run Code Online (Sandbox Code Playgroud)

c# algorithm circular-reference cycle-detection

6
推荐指数
1
解决办法
3876
查看次数

循环参考 - 架构问题

这可能是一个非常初学的问题,但我已经搜索了很多主题并且无法找到相同的情况,尽管我确信这种情况一直都在发生.

我的项目/计划将跟踪建筑项目图纸的变化,并在更改图纸时向人们发送通知.

将有许多建筑项目(工地),每个建筑项目中都会有很多图纸.每个图纸都会有一些修改(当它们被更改时,会创建一个新版本).

这是我的项目类

public class Project
{
    private readonly List<Drawing> _drawings = new List<Drawing>(30);
    private readonly List<Person> _autoRecepients = new List<Person>(30);

    public int ID { get; private set; }
    public string ProjectNumber { get; private set; }
    public string Name { get; private set; }
    public bool Archived { get; private set; }
    public List<Person> AutoRecepients { get { return _autoRecepients; } }


    public Project(int id, string projectNumber, string name)
    {
        if (id < 1) { id = -1; …
Run Code Online (Sandbox Code Playgroud)

c# oop circular-reference

6
推荐指数
1
解决办法
131
查看次数