CSharp链式编程

用了dotween之类的插件这么久链式编程怎么实现:


namespace TestProgram
{
     class   Programmer
    {
        public string Name { get;  set; }
        public Programmer(string name)
        {
            Name = name;
        }
    }

    static class ProgrammerManager
    {
        public static Programmer SetName(this Programmer p, string name)
        {
            p.Name = name;
            return p;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Programmer p= new Programmer("Haha").SetName("hhaha");
            Console.WriteLine(p.Name);
        }
    }
}

引入 泛型

namespace TestProgram
{
    class  Programmer
    {
        public string Name { get;  set; }
        public Programmer(string name)
        {
            Name = name;
        }
    }

    static class ProgrammerManager
    {
        public static Programmer SetName(this Programmer p, string name)
        {
            p.Name = name;
            return p;
        }
        public static T SetName(this T p, string name) where T:Programmer
        {
            p.Name = name;
            return p;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Programmer p= new Programmer("Haha").SetName("hhaha");
            Console.WriteLine(p.Name);
        }
    }
}

你可能感兴趣的:(C#)