From d86e231bda8cfe0c4385ed9259546888ca69fe32 Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 30 Sep 2019 11:40:34 +0200 Subject: [PATCH] Fixed ResolveForeignKeys: Changed generic creation to reflective generation of new object Fixed loading himself --- Database-Attribute_System/Attributes/DbObject.cs | 3 +++ Database-Attribute_System/ClassAction.cs | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Database-Attribute_System/Attributes/DbObject.cs b/Database-Attribute_System/Attributes/DbObject.cs index 4f33908..a759b99 100644 --- a/Database-Attribute_System/Attributes/DbObject.cs +++ b/Database-Attribute_System/Attributes/DbObject.cs @@ -13,6 +13,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes public string _tableName; public Type parentClassType; + public ConstructorInfo parentCInfo; // All childrenAttributes public List baseAttributes = new List() { }; @@ -35,6 +36,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes public void Init(Type classType) { this.parentClassType = classType; + parentCInfo = classType.GetConstructor(Type.EmptyTypes); + this._tableName = this._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name // Iterate thru all fields diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index c3f4a61..fb11352 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -104,14 +104,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System } public static T GetByPrimaryKey(Type classType, Dictionary primaryKeyData, Func>> queryExecutor) where T: new() { - // Create new empty object - T obj = new T(); - // Read dbObject-attribute DbObject dbObject = ClassAction.Init(classType); if (dbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"No primaryKey found in '{classType.Name}'"); + // Create new empty object + T obj = (T)dbObject.parentCInfo.Invoke(null); + // iterate thru them to check and fill object foreach (DbPrimaryKey primaryKeyAtt in dbObject.primaryKeyAttributes) { @@ -301,7 +301,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System if(foreignObject_value == null) { // Resolve it - foreignObject_value = GetByPrimaryKey(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); + foreignObject_value = GetByPrimaryKey(foreignObjectAtt.foreignObjectType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); foreignObjectAtt.parentField.SetValue(classObject, foreignObject_value); // Set the value // Now scan the just resolved class to be able to set myself