From ef73a51ab5ebb600eb2e9d1aebf61fdcf89b8469 Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 30 Sep 2019 12:49:21 +0200 Subject: [PATCH] Fixed Init stackOverFlowException for recursive objects --- Database-Attribute_System/Attributes/DbForeignObject.cs | 5 +++-- Database-Attribute_System/Attributes/DbObject.cs | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Database-Attribute_System/Attributes/DbForeignObject.cs b/Database-Attribute_System/Attributes/DbForeignObject.cs index 6c79df0..3ca8f67 100644 --- a/Database-Attribute_System/Attributes/DbForeignObject.cs +++ b/Database-Attribute_System/Attributes/DbForeignObject.cs @@ -26,14 +26,15 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes this._foreignKeyName = foreignKeyName; } - public void Init(FieldInfo fi, DbObject classAttribute) + public void Init(FieldInfo fi, DbObject classAttribute, DbObject foreignClassAttribute = null) { this.parentField = fi; this.classAttribute = classAttribute; this.foreignObjectType = fi.FieldType; // Init foreign-object class - DbObject foreignClassAttribute = ClassAction.Init(this.foreignObjectType); + if(foreignClassAttribute == null) + foreignClassAttribute = ClassAction.Init(this.foreignObjectType); // Check if something is weird if (foreignClassAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{foreignClassAttribute.parentClassType.Name}' does not have a primaryKey."); diff --git a/Database-Attribute_System/Attributes/DbObject.cs b/Database-Attribute_System/Attributes/DbObject.cs index a759b99..b556210 100644 --- a/Database-Attribute_System/Attributes/DbObject.cs +++ b/Database-Attribute_System/Attributes/DbObject.cs @@ -69,7 +69,10 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes } else if (fi.GetCustomAttribute(typeof(DbForeignObject), true) is DbForeignObject fobj) // ForeignObjects { - fobj.Init(fi, this); + if(fi.FieldType == this.parentClassType) // Check if we are referencing ourselfes + fobj.Init(fi, this, this); + else + fobj.Init(fi, this); this.foreignObjectAttributes.Add(fobj); }