Skip to content

A add-in for Fody that provides delegated property support.

License

Notifications You must be signed in to change notification settings

Cyl18/DelegatedPropertySharp.Fody

Repository files navigation

DelegatedPropertySharp.Fody

This is an add-in for Fody.
Created by Cyl18.
Still work in process.
The idea comes from Kotlin delegated property.

Installation

  • Install the NuGet package:
    Install-Package DelegatedPropertySharp.Fody
    
  • Add <DelegatedPropertySharp/> to your FodyWeavers.xml.

Example

Before

    public class Class1
    {
        [Lazy] public int LazyObject1 { get; }
        [Lazy] public object LazyObject2 { get; set; }
    }

    public class LazyAttribute : DelegatedPropertyAttributeBase
    {
    }

    public class Handler<TThis, TProperty> : IDelegatedPropertyHandler<TThis, TProperty, Lazy>
    {
        public PropertyInfo PropertyInfo { get; set; }
        readonly Lazy<TProperty> _lazy = new Lazy<TProperty>();

        public TProperty Get(TThis @this)
        {
            return _lazy.Value;
        }

        public void Set(TThis @this, TProperty value)
        {
            throw new InvalidOperationException();
        }
    }

What gets compiled

    public class Class1()
    {
        static Class1()
        {
            _handler0 = new Handler<Class1, int>();
            _handler0.PropertyInfo = typeof(Class1).GetProperty("LazyObject1");

            _handler1 = new Handler<Class1, object>();
            _handler1.PropertyInfo = typeof(Class1).GetProperty("LazyObject2");
        }

        private static Handler<Class1, int> _handler0;
        [Lazy]
        public int LazyObject1
        {
            get => _handler0.Get(this);
        }

        private static Handler<Class1, object> _handler1;
        [Lazy]
        public object LazyObject2
        {
            get => _handler1.Get(this);
            set => _handler1.Set(this, value);
        }
    }
    ...

About

A add-in for Fody that provides delegated property support.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages