zoukankan      html  css  js  c++  java
  • 反射相关

    1.一般反射方法: 

    Type type= Type.GetType("FullClassName");
      MethodInfo methodInfo =type.GetMethod("MethedNam");
             object[] parameters;
                      foreach (var p in listPara)
                 {
                     parameters[i] = p;
                     i++;
                 }
    
             try
             {
                    object o = Activator.CreateInstance(type);
                return methodInfo.Invoke(o, parameters ).ToString();
    
             }
             catch (Exception)
             {
    
                 return string.Empty;
             }
           
             ///  T Get<T>() 
    
    ///{string interfaceName = typeof(T).Name;
    /// T temp = (T)Assembly.Load("Dal").CreateInstance("DAL" + "." + interfaceName.Substring(1, interfaceName.Length - 1))
    
    ///}
    
    
    

     Express的compile方法

    public Func<object, object[], object> GetDelegate()
    {
        Expression<Func<object, object[], object>> exp = (instance, parameters) =>
            ((ClassName)instance).Method(parameters[0], parameters[1]);

        return exp.Compile();
    }

     Emit: 代码片段

     internal static SetHandler CreateSetHandler(Type type, PropertyInfo propertyInfo)
            {
                MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true);
                DynamicMethod dynamicSet = CreateSetDynamicMethod(type);
                ILGenerator setGenerator = dynamicSet.GetILGenerator();
    
                setGenerator.Emit(OpCodes.Ldarg_0);
                setGenerator.Emit(OpCodes.Ldarg_1);
                UnboxIfNeeded(setMethodInfo.GetParameters()[0].ParameterType, setGenerator);
                setGenerator.Emit(OpCodes.Call, setMethodInfo);
                setGenerator.Emit(OpCodes.Ret);
    
                return (SetHandler)dynamicSet.CreateDelegate(typeof(SetHandler));
            }
     private static DynamicMethod CreateSetDynamicMethod(Type type)
            {
                return new DynamicMethod("DynamicSet", typeof(void),
                      new Type[] { typeof(object), typeof(object) }, type, true);
            }
    
  • 相关阅读:
    python深浅拷贝
    pyinstaller打包py文件为exe方法
    python学习笔记3-关于文件的复制、重命名、移动、删除操作
    BeautifulSoup4 print() 输出中文乱码解决方法
    进程和线程的开启效率
    python3 使用pymysql
    python3 __file__
    Flask Template ( 模板学习)
    响应对象
    nginx 以及 uwsgi 的配置
  • 原文地址:https://www.cnblogs.com/fuhui/p/1942449.html
Copyright © 2011-2022 走看看