反射方法的使用

#region 反射调用多类别添加积分的方法
  /// <summary>
  /// 反射调用多类别添加积分的方法
  /// </summary>
  /// <returns>bool</returns>
  public bool ReturnAccount()
  {   
   bool isSuccess=false;
   //实例化通用反射类
   ReflectionInfo reflection = new ReflectionInfo();
   try
   {
    //调用FindPathInfoByName方法 判断配置是否完全
    if(reflection.FindPathInfoByName(ReflectionInfo.WebAccount.ClassName))
    {
     
     //要调用方法的参数
     string m_UserID =this.UserID.Trim();
     string m_moudleName="评论管理";
     string m_atName=this.ATName.Trim();
     //执行反射方法
     reflection.ExecuteMethod(ReflectionInfo.WebAccount.ClassName,ReflectionInfo.WebAccount.AddAccount,new object[] {m_UserID,m_moudleName,m_atName},true);
     isSuccess = (bool)reflection.RetrunValue;
    }
    return isSuccess;
   }
   catch(Exception ex)
   {
    string exMsg=ex.Message;
    return false;
   }   
  }
  #endregion

public void ExecuteMethod(string ClassName,string MethodName,object[] ArrayParaMeter,bool isReturn)
  {
   AssemblyConfig assembly=new AssemblyConfig();
   assembly.Create();

   AssemblyClass cla=assembly.FindClassInfoByName(ClassName);

   if (cla == null)
   {
    throw new Exception("类名设置不正确");
   }
 
   AssemblyMethod method=cla.FindMethodInfoByName(MethodName);

   
   if (method == null)
   {
    throw new Exception("方法名设置不正确");
   }
   if (isReturn)
   {
    _RetrunValue = method.Invoke(ArrayParaMeter);
   }
  }

  public object RetrunValue
  {
   get
   {
    return _RetrunValue;
   }
  }
/// <summary>
/// 遍历执行dll中的某个方法
/// </summary>
/// <param name="MethodName">需要遍历执行dll中的方法名称</param>
/// <returns>SortedList</returns>
  public SortedList ReturnMoudleName(string MethodName)
  {
   SortedList sl = new SortedList();

   DllDirectory=System.Configuration.ConfigurationSettings.AppSettings["DllDirectory"];

   //   MethodName=System.Configuration.ConfigurationSettings.AppSettings["MethodName"];      
       
   System.IO.FileInfo[] filesinfo=new System.IO.DirectoryInfo(Path.Combine(Server.MapPath("~"),DllDirectory)).GetFiles();
   
   //循环调用生成的DLL文件数组
   foreach (System.IO.FileInfo file in filesinfo)
   {
    //从固定的文件路径中加载程序集,判断是否为dll文件
    if(file.Name.EndsWith(".dll"))
    {
     System.Reflection.Assembly Ass=Assembly.LoadFrom(file.FullName.ToString());
    
     Type[] dlltypes = Ass.GetTypes();
    
     foreach(Type dlltype in dlltypes)
     {
      MethodInfo minfo =dlltype.GetMethod(MethodName);
      object obj = Ass.CreateInstance(dlltype.Namespace+"."+dlltype.Name);
      try
      {
       sl.Add(dlltype.Namespace+"."+dlltype.Name,(string)minfo.Invoke(obj,new string[]{"ssss"}));
       
      }
      catch
      {
       
       sl.Add("该文件没有实现需要提供的方法!->",file.Name);
      }
     }
    }
//    else
//    {
//     sl.Add("很抱歉,目录中含有非标准文件->",file.Name);
//    }

   }
   return sl;
  }

#region 根据一个类的名字查询反射的类的配置是否完全(FindPathInfoByName方法)
  /// <summary>
  /// 根据一个类的名字查询反射的类的配置是否完全<see cref="DiabloDotNet.ApplicationConfig.AssemblyClass"> DiabloDotNet.ApplicationConfig.AssemblyClass</see>类的信息
  /// </summary>
  /// <param name="SClassName">类的名称(全名:命名空间+类名)</param>
  /// <returns>
  /// true:web.config已配置该反射类同时物理路径也存在该类的dll文件
  /// false:表示配置不完全
  /// </returns>
  public  bool FindPathInfoByName(string SClassName)
  {
   
   string clsPath="";
   string fullPath="";
   bool isExist = false;
   AssemblyConfig assembly=new AssemblyConfig();
   assembly.Create();
   AssemblyClass cla=assembly.FindClassInfoByName(SClassName);
   AssemblyConfig cfg=(AssemblyConfig)System.Configuration.ConfigurationSettings.GetConfig("Assembly");
   this.ClassList=cfg.ClassList;
   foreach(AssemblyClass a in this.ClassList)
   {
    if(a.ClassName==SClassName)
    {
     clsPath=a.ClassPath;
     break;
    }
   }
   fullPath=Path.Combine(Server.MapPath("~"),clsPath);
   if(System.IO.File.Exists(fullPath) && cla!=null)
   {
    isExist=true;
   }
   return isExist;
  }
  #endregion

  #region 反射执行方法
  /// <summary>
  /// 反射执行方法
  /// </summary>
  /// <param name="ClassName">类名</param>
  /// <param name="MethodName">方法名</param>
  /// <param name="ArrayParaMeter">方法的参数</param>
  /// <param name="isReturn">是否要返回值</param>
  public void ExecuteMethod(string ClassName,string MethodName,object[] ArrayParaMeter,bool isReturn)
  {
   
   AssemblyConfig assembly=new AssemblyConfig();
   assembly.Create();

   AssemblyClass cla=assembly.FindClassInfoByName(ClassName);
   
   if (cla == null)
   {
    throw new Exception("类名设置不正确");
   }
 
   AssemblyMethod method=cla.FindMethodInfoByName(MethodName);

   if (method == null)
   {
    throw new Exception("方法名设置不正确");
   }
   if (isReturn)
   {
    _RetrunValue = method.Invoke(ArrayParaMeter);
   }
  }

  #endregion  

#region 根据一个类的名字查询反射的类的信息FindClassInfoByName
  /// <summary>
  /// 根据一个类的名字查询<see cref="DiabloDotNet.ApplicationConfig.AssemblyClass"> DiabloDotNet.ApplicationConfig.AssemblyClass</see>类的信息
  /// </summary>
  /// <remarks>查询不到返回null</remarks>
  /// <param name="sClassName">类的名称</param>
  /// <returns><see cref="DiabloDotNet.ApplicationConfig.AssemblyClass"> DiabloDotNet.ApplicationConfig.AssemblyClass</see>反射实现类< /returns>
  public AssemblyClass FindClassInfoByName(string sClassName)
  {
   AssemblyClass ac=null;
   foreach(AssemblyClass a in this.ClassList)
   {
    if(a.ClassName==sClassName)
    {
     ac=a;
     break;
    }
   }
   return ac;
  }
  #endregion 根据一个类的名字查询反射的类的信息FindClassInfoByName

#region 根据方法名称查询方法反射方法实现类信息FindMethodInfoByName
  /// <summary>
  /// 根据方法名称查询方法反射方法实现类信息
  /// </summary>
  /// <remarks>查询不到返回null</remarks>
  /// <param name="MoudleName">方法名称</param>
  /// <returns><see cref="DiabloDotNet.ApplicationConfig.AssemblyMethod"> AssemblyMethod</see>反射方法实现类</returns>
  public AssemblyMethod FindMethodInfoByName(string MoudleName)
  {
   AssemblyMethod method=null;
   foreach(AssemblyMethod m in this.MethodList)
   {
    if(m.MethodName==MoudleName)
    {
     method=m;
     break;
    }
   }
   return method;
  }
  #endregion 根据方法名称查询方法反射实现类信息FindMethodInfoByName

#region 根据一个类的名字查询反射的类的信息FindClassInfoByName
  /// <summary>
  /// 根据一个类的名字查询<see cref="DiabloDotNet.ApplicationConfig.AssemblyClass"> DiabloDotNet.ApplicationConfig.AssemblyClass</see>类的信息
  /// </summary>
  /// <remarks>查询不到返回null</remarks>
  /// <param name="sClassName">类的名称</param>
  /// <returns><see cref="DiabloDotNet.ApplicationConfig.AssemblyClass"> DiabloDotNet.ApplicationConfig.AssemblyClass</see>反射实现类< /returns>
  public AssemblyClass FindClassInfoByName(string sClassName)
  {
   AssemblyClass ac=null;
   foreach(AssemblyClass a in this.ClassList)
   {
    if(a.ClassName==sClassName)
    {
     ac=a;
     break;
    }
   }
   return ac;
  }
  #endregion 根据一个类的名字查询反射的类的信息FindClassInfoByName

#region 实现IConfigHandler接口中的ICreate方法
  /// <summary>
  /// 实现<see cref="DiabloDotNet.ApplicationConfig.IConfigHandler"> DiabloDotNet.ApplicationConfig.IConfigHandler</see>接口中的ICreate方法
  /// </summary>
  /// <remarks>此方法由<see cref="DiabloDotNet.ApplicationConfig.AppConfigHandler"> DiabloDotNet.ApplicationConfig.AppConfigHandler</see>中的Create方法自动调用</remarks>
  /// <param name="xmlNode">配置的根节点</param>
  /// <returns>返回<see cref="DiabloDotNet.ApplicationConfig.AssemblyConfig"> DiabloDotNet.ApplicationConfig.AssemblyConfig</see>对象< /returns>
  public object ICreate(System.Xml.XmlNode xmlNode)
  {
   AssemblyConfig cfg=new AssemblyConfig();
   foreach(XmlNode xn in xmlNode.ChildNodes)
   {
    if(xn.Name=="ClassInfo")
    {
     foreach(XmlNode xnn in xn.ChildNodes)
     {
      if(xnn.Name=="Class")
      {
                            AssemblyClass aclass=new AssemblyClass();
                            aclass.ClassName=xnn.Attributes["name"].Value;
       aclass.ClassPath=xnn.Attributes["path"].Value;
       aclass.ClassAssemblyName=xnn.Attributes["classAssemblyname"].Value;
       try
       {
        aclass.UseFlag=xnn.Attributes["UseFlag"].Value;
       }
       catch
       {
        aclass.UseFlag="";
       }
       foreach(XmlNode xnnn in xnn.ChildNodes)
       {
        if(xnnn.Name=="ModuleInfo")
        {
         foreach(XmlNode xnnnn in xnnn.ChildNodes)
         {
          if(xnnnn.Name=="Module")
          {
           AssemblyMethod method=new AssemblyMethod();
           method.MethodClassAssemblyName=xnn.Attributes["classAssemblyname"].Value;
           method.MethodClassName=xnn.Attributes["name"].Value;
           method.MethodName=xnnnn.Attributes["name"].Value;
           method.MethodClassPath=xnn.Attributes["path"].Value;
           switch(xnnnn.Attributes["returntype"].Value)
           {
            case "Null":
             method.MethodReturnType=MethodReturnTypeStyle.Null;
             break;
            case "DataTable":
             method.MethodReturnType=MethodReturnTypeStyle.DataTable;
             break;
            case "Int":
             method.MethodReturnType=MethodReturnTypeStyle.Int;
             break;
            case "String":
             method.MethodReturnType=MethodReturnTypeStyle.String;
             break;
            case "Boll":
             method.MethodReturnType=MethodReturnTypeStyle.Bool;
             break;
            case "XmlNodeList":
                                                    method.MethodReturnType=MethodReturnTypeStyle.XmlNodeList;
             break;
                                          case "XmlNode":
             method.MethodReturnType=MethodReturnTypeStyle.XmlNode;
             break;
            default:
             method.MethodReturnType=MethodReturnTypeStyle.Null;
             break;
           }
           aclass.MethodList.Add(method);
          }
         }
        }
       }
       cfg.ClassList.Add(aclass);
      }
     }
    }
   }
   return cfg;
  }
  #endregion 实现IConfigHandler接口中的ICreate方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值