zoukankan      html  css  js  c++  java
  • c# dynamic的属性是个变量

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        public class Student : System.Dynamic.DynamicObject
        {
          
            public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
            {
                if (map != null)
                {
                    string name = binder.Name;
                    object value;
                    if (map.TryGetValue(name, out value))
                    {
                        result = value;
                        return true;
                    }
                }
                return base.TryGetMember(binder, out result);
            }
    
            System.Collections.Generic.Dictionary<string, object> map;
    
            public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
            {
                if (binder.Name == "set" && binder.CallInfo.ArgumentCount == 2)
                {
                    string name = args[0] as string;
                    if (name == null)
                    {
                        //throw new ArgumentException("name");  
                        result = null;
                        return false;
                    }
                    if (map == null)
                    {
                        map = new System.Collections.Generic.Dictionary<string, object>();
                    }
                    object value = args[1];
                    map.Add(name, value);
                    result = value;
                    return true;
    
                }
                return base.TryInvokeMember(binder, args, out result);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                dynamic t = new Student();
                string @a = "ggff";
                t.set(@a, "galrj");
                Console.WriteLine(t.ggff);
    
    
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    Python:循环语句
    Python:list用法
    ettercap局域网DNS欺骗实现过程
    Linux关于用户信息的一些命令
    业务逻辑漏洞
    Linux安装Sqlmap等工具
    CVE-2015-1635(MS15-034 )进行DOS攻击
    HTTP.SYS远程代码执行漏洞测试(ms15-034)
    爬虫学习笔记
    MSF利用ms17-010漏洞过程记录
  • 原文地址:https://www.cnblogs.com/kexb/p/5646942.html
Copyright © 2011-2022 走看看