zoukankan      html  css  js  c++  java
  • C# dynamic class inherit from dynamicobject

    Dynamic object provides a base class for specifying dynamic behavior at runtime.

    public class DynamicObjectInvoker:DynamicObject
        {
            public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
            {
                Console.WriteLine(binder.Name + " method was called");
                result = null;
                return true;
            }
        }
     static void DynamicInvokeMemberDemo()
            {
                dynamic doi = new DynamicObjectInvoker();
                doi.MakeMoney();
                doi.MakeBigMoney();
            }

    static void ExpandObjectDemo()
            {
                dynamic x = new ExpandoObject();
                x.FavoriteColor = ConsoleColor.Green;
                x.FavoriteNumber = 7;
                Console.WriteLine(x.FavoriteColor);
                Console.WriteLine(x.FavoriteNumber);
    
                var dict = (IDictionary<string, object>)x;
                Console.WriteLine(dict["FavoriteColor"]);
                Console.WriteLine(dict["FavoriteNumber"]);
                Console.WriteLine(dict.Count);
            }

    C# can also interoperate with Dynamic language such as IronPython.The DLR can parse lexical and sematic statement.

     static void Main(string[] args)
            {
                int result = (int)Calculate("2*3");
                Console.WriteLine(result);
                Console.ReadLine();
            }
    
            static object Calculate(string expression)
            {
                ScriptEngine engine = Python.CreateEngine();
                return engine.Execute(expression);
            }
  • 相关阅读:
    【2021-08-09】问题还需一点一点去改正
    【2021-08-08】连岳摘抄
    【2021-08-07】请教帖
    21春助教总结
    实践总结+技术博客评分
    来吧 ,来吧 自己搭建一个erp 系统
    博客索引
    「CCNU21暑期第六次周赛」
    「CCNU21暑期第五次周赛」
    「图论」连通性问题
  • 原文地址:https://www.cnblogs.com/Fred1987/p/13636397.html
Copyright © 2011-2022 走看看