zoukankan      html  css  js  c++  java
  • 反射调用DLL

    public static type GetClassType(string ClassName,string dllname)

    {

        Type ClassType =null;

         foreach(Assembly am in AppDomain.CurrentDomain.GetAssemblies())

    {

          if (am.FullName.Contains(dllname))

    {

      ClassType=am.GetType(ClassName);

      if (ClassType !=null) break;

    }
    }

    if (ClassType==null)

    {

    string filename=string.Empty;

    if (dllname.EndsWith(".dll"))

    {

    filename=Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllname);

    }

    else

     filename=string.Format("{0}.dll ",AppDomain.CurrentDomain.BaseDirectory, dllname));

    Assembly asm=Assembly.LoadFile(filename);

    ClassType =asm.GetType(ClassName);

    }

    return ClassType;

    }

    private static Dictionary <int,object> ServiceDe=new Dictionary<int,object>();

    delegate Result ActionDelegate<T>(T Data);

    private static Result ReflectionMethod<T>(FormShowRequest request)

    {

    object doAction =null;

    string dllname =request.module;

    string classname=request.action;

    string method=request.method;

    param sdata=request.param;

    Result result=new Result();

    string strKey=dllname+“,”+classname+","+method;

    int hashCode=strKey.GetHashCode();

    if (!ServiceDelegates.ContainsKey(hashCode))

    {

    Type clsname =GetClassType(classname,dllname);

    object action=clsname.GetConstructor(Type.EmptyTypes).Invoke(null);

    doAction =Delegate.CreateDelegate(typeof(ActionDelegate<T>),action,method);

    if (ServiceDelegates!=null && doAction!=null)

    ServiceDelegates.Add(hashCode,doAction);

    }

    else

    {

    doAction=ServiceDelegates[hashCode];

    }

    if (doAction!=null)

    {

    result =((ActionDelegate<param>)doAction)(sdata);

    return result;

    }

    else

    {

    result=new Result();

    result.errors=“反射调用错误!”

    }
    }

  • 相关阅读:
    2019总结及2020计划
    蓝牙BLE连接与操作
    Android蓝牙操作
    Cannot use JSX unless the '--jsx' flag is provided.
    PyQt打包可执行文件
    Springboot项目报错【java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.base/java.net.URLClassLoader】
    typescript枚举字符串型不能使用函数问题
    beautifulsoap常用取节点方法
    numpy常用矩阵操作
    MYSQL 碎片查询
  • 原文地址:https://www.cnblogs.com/huzige/p/7675540.html
Copyright © 2011-2022 走看看