![PersistentObject PersistentObject]()
为什么哦为什么不让dotnet让我在调用之前执行一些自己的代码 基础构造函数?我可以用方法做到这一点。 我发现它如此限制!>I have an object persistence framework I have written, this frameworkexpects every object to descend ultimately from PersistentObject and tohave a constructor (ObjectSpace objectSpace) so that objects may berecreated.PersistentObject''s constructor will do something like thisthis.ObjectSpace = objectSpace;objectSpace.RegisterObjectCreation(this);The object space will record a reference to this new object + do somethinglike thisif (!IsLoadingFromDatabase) newInstance.AfterConstruction();The virtual AfterConstruction method on a class is therefore only evercalled when an object is initially created and not when it is recreateddue to being fetched from the database. This gives a good opportunity tocreate composite objects etc. Here is my problempublic class CustomerAction : PersistentObject{ public CustomerAction(Customer customer) : base(customer.ObjectSpace) { this.Customer = customer; } protected override void AfterConstruction() { //Do some stuff with this.Customer }}My problem is that this.Customer has not been set by the timeAfterConstruction is called.1) new CustomerAction(someCustomer);2) CustomerAction constructor is called3) Base constructor (customer.ObjectSpace is called)4) CustomerAction.AfterConstruction is called5) CustomerAction constructor code is executedWhy oh why wont dotnet let me execute some of my own code before callingthe base constructor? I can do this in methods.I find it so restrictive! 这篇关于构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 14:20