-
Add a reference to the
Uno.CodeGen.ClassLifecycle
Nuget package in your project. -
Create a new class and add methods one of the following attributes
[ConstructorMethod]
[DisposeMethod]
[FinalizerMethod]
public partial class MyClass { [ConstructorMethod] private void MyConstructor() { } [DisposeMethod] private void MyDispose() { } [FinalizerMethod] private void MyFinalizer() { } }
-
It will generate the following public methods for you:
partial class MyClass : IDisposable { // If none defined on your class, a default constructor which invokes the Initialize() public MyClass() { Initialize(); } // The initialize method which must be invoked in each constructor private void Initialize() { MyConstructor(); } // Disposable pattern implementation protected virtual void Dispose(bool isDisposing) { if (isDisposing) { this.MyDispose(); } } // IDisposable implementation public void Dispose() { Dispose(true); } // Class finalizer ~MyClass() { Dispose(false); this.MyFinalizer(); } }
If you have any method which is marked with the [ConstructorMethod]
attribute, an Initialize
method will be generated.
- All constructors of the class must invoke the
Initialize
method (either directly in its body, or by invoking an other constructor. - Parameters:
You can have some parameters on your constructor methods. They will be aggregated
as parameters of the
Initialize()
method.- To be consistent between builds, they are sorted using the following rules:
- Non optional parameters
- Alphabetically
- If a parameter is optional, its default value will also be copied.
- If two methods are defining a parameter with the same name:
- If they have the same type, they will be merged, otherwise an error will be generated.
- If they are booth optional, and the default value is the same, they will stay optional
on the
Initialize
. If default value are different, an error will be generated. If any is non optional, it will be required.
- To be consistent between builds, they are sorted using the following rules:
- If you don't have any parameter-less constructor defined in your class, and the
Initialize
doesn't have any non optional parameter, then a parameter-less constructor will be generated. It will invoke theInitialize
for you, so you only have to invoked it in your own constructors:public MyClass(string aParameter) *: this()* { ../.. }
- The return type of the method marked with
[ConstructorMethod]
must bevoid
If you have any method marked with the [DisposeMethod]
attribute, the generation
will make your class implement IDisposable
, and ensure to invoke your methods when the
IDisposable.Dispose
method is invoked.
- Your methods must not have any parameter.
- The return type of the method marked with
[DisposeMethod]
must bevoid
- Your class must not implement
IDisposable
itself. If you want to add stuff to the dispose process, add another method marked with[DisposeMethod]
. - If your class inherits from a class which already implements IDisposable:
- If the base class implements the
dispose pattern,
the generated code will override the
Dispose(bool isDisposing)
method. - If the base class is
Uno.Core.IExtensibleDisposable
, it will register itself as an extention. - If the
Dispose()
method is virtual, it will override it. - As the override in not possible, an error will be generated.
- If the base class implements the
dispose pattern,
the generated code will override the
If you have any method which is marked with the [FinalizerMethod]
attribute, the generation
will generate the class finalizer (~MyClass
), and ensure to invoke your methods when invoked.
- Your methods must not have any parameter.
- The return type of the method marked with
[FinalizerMethod]
must bevoid
- Your class must not define the finalizer itself. If you want to add stuff
to the finalize process, add another method marked with
[FinalizerMethod]
.