检查 dll 是否存在并基于该 class 库的使用属性

check dll exist and based on that use properties of that class library

我需要实施报告级别的 dll,如果我们添加该 dll,则会显示一些报告。为此我创建了 class 库 CustomeReports..在这个 class 文件中我列出了您可以在下面的代码中看到一些动态报告...

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;
        }
    }
}

现在我想使用此 class 方法 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");
            }

我在下面的方法中有 return 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;
        }

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

经过多次搜索后,我找到了查找 dll 和使用 keypairtext 属性的方法....感谢 dynamic keyword

下面是我在 aspx 页面中查找 dll 然后使用该 class 库属性的代码..

这是检查文件和dll是否存在的方法return布尔值

        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 方法...它将创建 class 和 return 对象的实例..

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;
        }

如果您有任何疑问或疑问,请告诉我,我们会讨论...谢谢...;)