2017-04-12 22 views
0

我需要实现报告级别的dll,如果我们添加了那个dll,那么一些repors会显示..为此我创建了类库CustomeReports..in这个类文件我已经列出了一些动态的报告,你可以看到下面的代码...检查dll是否存在,并基于那个类库的使用属性

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace CustomeReports 
{ 
    public class keyPairTextC 
    { 
     public bool keyID { get; set; } 
     public string keyN { get; set; } 
     public string keyE { get; set; } 
     public string keyFromDate { get; set; } 
     public string keyTillDate { get; set; }   

    } 
    public class CustomReport 
    { 
     public List<keyPairTextC> GetReportNameList() 
     { 
      CustomReport rpt = new CustomReport(); 
      List<keyPairTextC> getReportName = new List<keyPairTextC>(); 
      getReportName.Add(new keyPairTextC 
      { 
       keyID = false, 
       keyN = "Report2", 
       keyE = "R", 
       keyFromDate = "NA", 
       keyTillDate = "NA",    
      }); 
      return getReportName; 
     }   
     public static string getKeyEmp(string strPropertyValue) 
     { 
      try 
      { 
       List<string> newList1 = new List<string>(); 
       newList1 = getKeyValue(strPropertyValue).Select(C => C.keyE).ToList(); 
       strPropertyValue = newList1[0].ToString(); 
      } 
      catch (Exception ex) 
      { 
       strPropertyValue = ""; 
      } 
      return strPropertyValue; 
     } 
     public static List<keyPairTextC> getKeyValue(string strPropertyValue) 
     { 
      List<keyPairTextC> newList = new List<keyPairTextC>(); 
      CustomReport crm = new CustomReport(); 
      newList = crm.GetReportNameList(); 
      newList = newList.Where(c => c.keyN.Contains(strPropertyValue)).ToList(); 
      return newList; 
     } 
    } 
} 

,现在我想用这个类方法getReportNameList到的情况下,本报告我加入这个库的这个DLL或引用绑定。

在我的aspx形式,我有下面的代码

所以......

var className = "CustomReports"; 
      var assemblyName = "CustomeReports, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; 
      foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) 
      { 
       if (a.FullName == assemblyName) 
       { 
        assembly = a; 
        break; 
       } 
      } 
      if (assembly != null) 
      { 
       ViewState["AssemblyInfo"] = assembly; 
       hdnDLLValue.Value = "1"; 
       string strReportName = "Custom Reports";            
       object result = null; 
       result = returnDLLObject("GetReportNameList"); 
      } 

我有回报的DLL对象以下方法

public object returnDLLObject(string MethodName) 
     { 
      string className = "CustomeReports.CustomReports"; 
      object result = null; 
      object classInstance = null; 
      if (assembly == null) 
       assembly=(Assembly)ViewState["AssemblyInfo"]; 

      if (assembly != null) 
      { 
       try 
       { 

        Type _type_2 = assembly.GetType(className); 
        var methodInfo = _type_2.GetMethod(MethodName); 
        ParameterInfo[] parameters = methodInfo.GetParameters(); 
        classInstance = Activator.CreateInstance(_type_2, null); 
        if (parameters.Length == 0) 
        { 
         result = methodInfo.Invoke(classInstance, null); 
        } 
        else 
        { 
         object[] parametersArray = new object[] { ViewState["nodevalue"].ToStringIC() }; 
         result = methodInfo.Invoke(classInstance, parametersArray); 
        } 
       } 
       catch (Exception ex) 
       { 
        result=null; 
       } 
      } 
      return result; 
     } 

所以现在我无法这份报告对象绑定到列表类型类,也无法从该类库中获取属性值...

回答

0

经过太多的搜索后,我发现了一种方式来找到DLL和使用keypairtext属性....感谢dynamic keyword

这里是我在aspx页面下面的代码查找DLL,然后使用该类库的属性..

这是方法来检查文件和DLL是存在返回布尔值

 internal static bool FileOrDirectoryExists(string name) 
     { 
      return (Directory.Exists(name) || File.Exists(name)); 
     } 

和在我的网页加载我有功能结合报告,以便在下面的代码,你可以看到

string path = Server.MapPath(@"../CustomReports/CustomeReports.dll"); 

     if (FileOrDirectoryExists(path)) 
     { 
      var className = "CustomReports"; 
      var ProductVersion = FileVersionInfo.GetVersionInfo(path).ProductVersion; 
      var assemblyName = "CustomeReports, Version=" + ProductVersion.ToStringIC() + ", Culture=neutral, PublicKeyToken=null"; 
      assembly = Assembly.LoadFrom(path); 

      // ViewState["AssemblyInfo"] = assembly; 
      hdnDLLValue.Value = "1"; 
      string strReportName = "Custom Reports"; 
      object result = null; 
      result = returnDLLObject("GetReportNameList"); 
      int a = SpectraBL.Application.AccessControl.ToInt32IC(); 

      if (result != null) 
      { 
       if (result is IEnumerable) 
       { 
        List<object> list = new List<object>(); 
        var enumerator = ((IEnumerable)result).GetEnumerator(); 
        while (enumerator.MoveNext()) 
        { 
         list.Add(enumerator.Current); 
        } 
        List<dynamic> dynamicList = list.Select(x => (dynamic)x).ToList(); 
        if (dynamicList.Count != 0) 
        { 
         Childnode = new TreeNode("<span id='reportgrp' class='reportmenu label' onclick= OnNodeClick('G','" + strReportName + "','1'); > " + strReportName + " </span>", "Dynamic Reports"); 
         Childnode.SelectAction = TreeNodeSelectAction.None; 
         TreeView1.Nodes.Add(Childnode); 
         for (var i = 0; i < dynamicList.Count; i++) 
         { 
          string strChildName = dynamicList[i].ReportName; 
          if (dynamicList[i].isDisplay) 
          { 
           TreeNode Namenode = new TreeNode(); 
           if (ReportID == dynamicList[i].Reportid) 
           { 
            //With selection on pageload 
            Namenode = new TreeNode("<span id='report" + dynamicList[i].ReportName + "' class='reportmenu label selected' onclick= OnNodeClick('C','" + dynamicList[i].Reportid + "','1'); > " + strChildName + " </span>", strChildName); 
           } 
           else 
           { 
            //Without selection 
            Namenode = new TreeNode("<span id='report" + dynamicList[i].ReportName + "' class='reportmenu label' onclick= OnNodeClick('C','" + dynamicList[i].Reportid + "','1'); > " + strChildName + " </span>", strChildName); 
           } 
           Namenode.SelectAction = TreeNodeSelectAction.None; 
           Childnode.ChildNodes.Add(Namenode); 
          } 
         } 
        } 
       } 
      } 
     } 

这里是我的returnDLLObject方法......这将创造的T实例他等级和返回的对象..

public object returnDLLObject(string MethodName) 
     { 
      string className = "CustomeReports.CustomReports"; 
      object result = null; 
      object classInstance = null; 
      string path = Server.MapPath(@"../CustomReports/CustomeReports.dll"); 

      // if (!FileOrDirectoryExists(path)) 
      assembly = Assembly.LoadFrom(path); 

      if (FileOrDirectoryExists(path)) 
      { 
       try 
       { 
        Type _type_2 = assembly.GetType(className); 
        var methodInfo = _type_2.GetMethod(MethodName); 
        ParameterInfo[] parameters = methodInfo.GetParameters(); 
        classInstance = Activator.CreateInstance(_type_2, null); 
        if (parameters.Length == 0) 
        { 
         result = methodInfo.Invoke(classInstance, null); 
        } 
        else 
        { 
         object[] parametersArray = new object[] { ViewState["nodevalue"].ToStringIC() }; 
         result = methodInfo.Invoke(classInstance, parametersArray); 
        } 
       } 
       catch (Exception ex) 
       { 
        //Error.BLErrorDesc = ex.Message; 
        result = null; 
       } 
      } 
      return result; 
     } 

如果您有任何疑问或质疑,然后让我知道,将讨论...谢谢...;)

相关问题