.NET Framework / C# - 动态语言特性、dynamic - 第三方库

创建时间:
2018-10-19 03:08
最近更新:
2018-11-10 01:40

Note

  • 2018-10-19 首次接触到 Clay、ImpromptuInterface、Dynamitey。
  • 2018-10-19 发现 Clay 自 2011-02-28 在 NuGet 发布了唯一的 V1.0.0 之后 不再有更新。
  • ImpromptuInterface & Dynamitey 无文档,因此未花时间研究。
  • TNote: 2018-11-10 决定使用 Newtonsoft.JsonLINQ to JSON,而非这些 没有用户、没有文档、不再维护的 动态语言 项目。

Clay

Blog posts about Clay

2018-10-19 https://github.com/bleroy/clay 页面末尾有以下 3 个链接:

Author

Clay/ClaySharp 安装记录

2018-10-19 通过 NuGet 安装 https://www.nuget.org/packages/Clay,因为依赖关系最终自动安装了以下 Package:

  • Clay/ClaySharp
  • Castle.Core
  • Castle.DynamicProxy2
  • log4net

其中 Clay 包中的核心文件为 ClaySharp.dll

Resource

ImpromptuInterface

  1. https://www.nuget.org/packages/ImpromptuInterface - A Lightweight Duck Casting Framework for dynamic C#
  2. https://github.com/ekonbenefits/impromptu-interface - Static interface to dynamic implementation (duck casting). Uses the DLR combined with Reflect.Emit. Some of the features of ImpromptuInterface have been moved into another library called Dynamitey, ImpromptuInterface depends on Dynamitey.

Note: 2018-10-19 发现 除了 GitHub 该项目的 wiki 与 blob 中有几页文档,bing.com 未找到其他资源。暂不研究它。

Dynamitey

  1. https://www.nuget.org/packages/Dynamitey - (pronounced dyna-mighty) flexes DLR muscle to do meta-mazing things in .net
  2. https://github.com/ekonbenefits/dynamitey - (pronounced dyna-mighty) flexes DLR muscle to do meta-mazing things in .net

Note: 2018-10-19 发现 除了 GitHub 该项目的 wiki 与 blob 中有几页文档,bing.com 未找到其他资源。暂不研究它。

ClaySamples.cs 文件完整备份

using System;
using ClaySharp;

namespace Net451Console.Samples.Clay {
    public static class ClaySamples {
        public static void RunAll() {
            PropertyAssignment();
            IndexedPropertySyntax();
            ChainableSetters();
            AnonymousObject();
            NamedArguments();
            ArrayCreation();
            ArrayCreationOnExistingObject();
            ComplexTest();
            CastToCLRInterface();
        }

        public static void PropertyAssignment() {
            Console.WriteLine("**** PropertyAssignment ****");

            dynamic New = new ClayFactory();

            var person = New.Person();
            person.FirstName = "Louis";
            person.LastName = "Dejardin";

            Console.WriteLine("{0} {1}", person.FirstName, person.LastName);

            // This is equivalent, using method call syntax
            Console.WriteLine("{0} {1}", person.FirstName(), person.LastName());

            Console.WriteLine();
        }

        public static void IndexedPropertySyntax() {
            Console.WriteLine("**** IndexedPropertySyntax ****");

            dynamic New = new ClayFactory();

            var person = New.Person();
            person["FirstName"] = "Louis";
            person["LastName"] = "Dejardin";

            // Note that you can mix and match
            Console.WriteLine("{0} {1}", person["FirstName"], person.LastName);

            Console.WriteLine();
        }

        public static void ChainableSetters() {
            Console.WriteLine("**** ChainableSetters ****");
            dynamic New = new ClayFactory();

            var person = New.Person()
                .FirstName("Louis")
                .LastName("Dejardin");

            Console.WriteLine("{0} {1}", person.FirstName, person.LastName);

            Console.WriteLine();
        }

        public static void AnonymousObject() {
            Console.WriteLine("**** AnonymousObject ****");

            dynamic New = new ClayFactory();

            var person = New.Person(new {
                FirstName = "Louis",
                LastName = "Dejardin"
            });

            Console.WriteLine("{0} {1}", person.FirstName, person.LastName);

            Console.WriteLine();
        }

        public static void NamedArguments() {
            Console.WriteLine("**** NamedArguments ****");

            dynamic New = new ClayFactory();

            var person = New.Person(
                FirstName: "Louis",
                LastName: "Dejardin"
            );

            Console.WriteLine("{0} {1}", person.FirstName, person.LastName);

            Console.WriteLine();
        }

        public static void ArrayCreation() {
            Console.WriteLine("**** ArrayCreation ****");

            dynamic New = new ClayFactory();

            var people = New.Array(
                New.Person().FirstName("Louis").LastName("Dejardin"),
                New.Person().FirstName("Bertrand").LastName("Le Roy")
            );

            Console.WriteLine("Count = {0}", people.Count);
            Console.WriteLine("people[0].FirstName = {0}", people[0].FirstName);

            foreach (var person in people) {
                Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
            }

            Console.WriteLine();
        }

        public static void ArrayCreationOnExistingObject() {
            Console.WriteLine("**** ArrayCreationOnExistingObject ****");

            dynamic New = new ClayFactory();

            var person = New.Person(
                FirstName: "Bertrand",
                LastName: "Le Roy"
            );

            person.Aliases("bleroy", "BoudinFatal");

            // Array size is dynamic
            person.Aliases.Add("One more alias");

            Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
            foreach (var alias in person.Aliases) {
                Console.WriteLine("    {0}", alias);
            }

            Console.WriteLine();
        }

        public static void ComplexTest() {
            Console.WriteLine("**** ComplexTest ****");

            dynamic New = new ClayFactory();

            var directory = New.Array(
                New.Person(
                    FirstName: "Louis",
                    LastName: "Dejardin",
                    Aliases: new[] { "Lou" }
                ),
                New.Person(
                    FirstName: "Bertrand",
                    LastName: "Le Roy"
                ).Aliases("bleroy", "boudin"),
                New.Person(
                    FirstName: "Renaud",
                    LastName: "Paquay"
                ).Aliases("Your Scruminess", "Chef")
            ).Name("Some Orchard folks");

            foreach (var person in directory) {
                Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
                foreach (var alias in person.Aliases) {
                    Console.WriteLine("    {0}", alias);
                }
            }

            Console.WriteLine();
        }

        public interface IPerson {
            string FirstName { get; set; }
            string LastName { get; set; }
        }

        public static void CastToCLRInterface() {
            Console.WriteLine("**** CastToCLRInterface ****");

            dynamic New = new ClayFactory();

            var person = New.Person();
            person.FirstName = "Louis";
            person.LastName = "Dejardin";

            // Concrete interface implementation gets magically created!
            IPerson lou = person;

            // You get intellisense and compile time check here
            Console.WriteLine("{0} {1}", lou.FirstName, lou.LastName);

            Console.WriteLine();
        }
    }
}