GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - coplt/Coplt.Dropping: Auto gen dispose pattern

BranchesTags
Open more actions menu

Latest commit

 

History

21 Commits
21 Commits

Folders and files

NameName
Last commit message
Last commit date
.idea/.idea.Coplt.Dropping/.idea
.idea/.idea.Coplt.Dropping/.idea
 
 
Coplt.Dropping.Analyzers
Coplt.Dropping.Analyzers
 
 
Coplt.Dropping
Coplt.Dropping
 
 
Tests
Tests
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
Coplt.Dropping.slnx
Coplt.Dropping.slnx
 
 
LICENSE.md
LICENSE.md
 
 
README.md
README.md
 
 
global.json
global.json
 
 

Repository files navigation

Coplt.Dropping

Nuget MIT

Auto gen dispose pattern

  • Auto handle Dispose(bool dispoing) pattern
  • Auto handle destructor/finalizer
  • Allow multiple drops
  • Specify the drop order [Drop(Order = X)]
  • The first argument of Drop target method can be bool disposing
  • Mark Drop directly on fields and properties (requires target type have Dispose method)
  • Dose not supported AsyncDispose, too complicated, it is recommended to implement it manually
  • Drop can mark on static methods, will pass this on first argument, if have bool disposing will be the second argument

Example

  • Basic usage

    [Dropping]
    public partial class Foo1
    {
        [Drop]
        public void Drop()
        {
            Console.WriteLine(1);
        }
    }

    Generate output:

    Foo1.dropping.g.cs
    // <auto-generated/>
    
    #nullable enable
    
    using Coplt.Dropping;
    
    public partial class Foo1 : global::System.IDisposable
    {
    
        protected virtual void Dispose(bool disposing)
        {
            Drop();
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        ~Foo1()
        {
            Dispose(false);
        }
    
    }

  • Drop Froms

    [Dropping(From = DropFrom.Dispose)]
    public partial class Foo2
    {
        [Drop]
        public void Drop()
        {
            Console.WriteLine(1);
        }
    }
    
    [Dropping(From = DropFrom.Finalizer)]
    public partial class Foo3
    {
        [Drop]
        public void Drop()
        {
            Console.WriteLine(1);
        }
    }

    Generate output:

    Foo2.dropping.g.cs
    // <auto-generated/>
    
    #nullable enable
    
    using Coplt.Dropping;
    
    public partial class Foo2 : global::System.IDisposable
    {
    
        protected virtual void Dispose(bool disposing)
        {
            if (disposing) Drop();
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        ~Foo2()
        {
            Dispose(false);
        }
    
    }

    Foo3.dropping.g.cs
    // <auto-generated/>
    
    #nullable enable
    
    using Coplt.Dropping;
    
    public partial class Foo3 : global::System.IDisposable
    {
    
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing) Drop();
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        ~Foo3()
        {
            Dispose(false);
        }
    
    }

  • Inherit

    [Dropping]
    public partial class Foo8 : Foo1
    {
        [Drop]
        public void Drop1()
        {
            Console.WriteLine(1);
        }
    }

    Generate output:

    Foo8.dropping.g.cs
    // <auto-generated/>
    
    #nullable enable
    
    using Coplt.Dropping;
    
    public partial class Foo8 : global::System.IDisposable
    {
    
        protected override void Dispose(bool disposing)
        {
            Drop1();
            base.Dispose(disposing);
        }
    
    }

  • Sealed And Struct

    [Dropping]
    public partial struct Foo7
    {
        [Drop]
        private void Drop()
        {
            Console.WriteLine(1);
        }
    }
    
    [Dropping]
    public sealed partial class Foo9
    {
        [Drop]
        private void Drop()
        {
            Console.WriteLine(1);
        }
    }
    
    [Dropping(From = DropFrom.Dispose)]
    public sealed partial class Foo10
    {
        [Drop]
        private void Drop()
        {
            Console.WriteLine(1);
        }
    }
    
    [Dropping(From = DropFrom.Finalizer)]
    public sealed partial class Foo11
    {
        [Drop]
        private void Drop()
        {
            Console.WriteLine(1);
        }
    }

    Generate output:

    Foo7.dropping.g.cs
    // <auto-generated/>
    
    #nullable enable
    
    using Coplt.Dropping;
    
    public partial struct Foo7 : global::System.IDisposable
    {
    
        public void Dispose()
        {
            Drop();
        }
    
    }
    Foo9.dropping.g.cs
    // <auto-generated/>
    
    #nullable enable
    
    using Coplt.Dropping;
    
    public partial class Foo9 : global::System.IDisposable
    {
    
        public void Dispose()
        {
            Drop();
            GC.SuppressFinalize(this);
        }
    
        ~Foo9()
        {
            Dispose();
        }
    
    }
    Foo10.dropping.g.cs
    // <auto-generated/>
    
    #nullable enable
    
    using Coplt.Dropping;
    
    public partial class Foo10 : global::System.IDisposable
    {
    
        public void Dispose()
        {
            Drop();
        }
    
    }
    Foo11.dropping.g.cs
    // <auto-generated/>
    
    #nullable enable
    
    using Coplt.Dropping;
    
    public partial class Foo11 : global::System.IDisposable
    {
    
        private void Dispose(bool disposing)
        {
            if (!disposing) Drop();
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        ~Foo11()
        {
            Dispose(false);
        }
    
    }

More see Tests

About

Auto gen dispose pattern

Topics

csharp-sourcegeneratordestructiondisposedispose-patternfinalizeridisposablesource-generatorssourcegenerator

Resources

Readme
MIT license
Activity
Custom properties

Stars

2 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.