如何检查ac#控制台应用程序的基本类型?

发布于 2024-12-09 13:22:31 字数 1645 浏览 0 评论 0原文

我有几个继承自通用 BaseExe 的小型控制台应用程序。每个独立地按预期正常工作。我需要通过中央位置提供对这些应用程序的访问以供执行。

我的想法是创建一个小型网络应用程序,为用户提供可以执行的所有可用 exe 的列表。目前,我正在使用 web.config 文件来定义我的选项。然而,我希望这个列表更加动态,因为这个列表将会不断增长。我认为可以使用反射来确定服务器目录中可执行文件的类型,并根据可用的内容生成列表。至少,生成此列表所需的信息是

  1. exe 的路径(我拥有)
  2. 名称和说明(这是我需要获取的)

所有 exe 都具有相同的依赖项,并且位于同一服务器目录中。

实际上,我的尝试是

  • 从服务器目录获取 exe 列表,
  • 使用 Assembly.LoadFile 检查每个 exe 的类型,
  • 获取 NameDescription< 我可以以某种

方式绕过第二步,因为我可以检查 GetReferencedAssemblies 的结果包含 BaseExe 类型的程序集。然而,最后一部分完全回避了我。

var path = @"C:\Code\ESG Server\ExeOutput\bin\debug";
string[] filePaths = Directory.GetFiles(path, "*.exe");

foreach (var exeReport in filePaths)
{
    Assembly exe = Assembly.LoadFile(exeReport);
    var exeType = exe.GetType();
    var referencedAssemblies = exe.GetReferencedAssemblies();   
}

如何检查 C# 控制台应用程序的基本类型?

简化的 exes

public class exe1 : BaseExe
{
    public exe1 ()
    {
        this.Name = "exe1";
        this.Description = "exe1";
    }

    static void Main(string[] args){  
       var exe1 = new exe1();
       /* snip */
    }
}

public class exe2 : BaseExe
{
    public exe2 ()
    {
        this.Name = "exe2";
        this.Description = "exe2";
    }

    static void Main(string[] args){  
       var exe2 = new exe2();
       /* snip */
    }
}

编辑:我在输入所有这些内容后才意识到 NameDescription 属性是在构造函数。我目前的方法行不通。我将研究自定义属性作为替代方案并使用它来保存信息。要求仍然保持不变。

I have several small console applications that inherit from a common BaseExe. Independently each works correctly as expected. I need to provide access to these applications via a central location for execution.

My idea is to this a small web-application that provides users a list of all available exe's that can be executed. At present, I am using the web.config file to define my options. I however, want this to be more dynamic since this list will be growing. I thought that one can maybe use reflection to determine the type of the executable from the server directory and based on what is available, generate the list from these. At minimum, the information I need for generating this list is

  1. The path of the exe (which I have)
  2. The Name and Description (which is what I need to get)

All exe's have the same dependencies and are located in the same server directory.

In effect, my attempt has been to

  • Get a list of exe's from the server directory
  • check the type of each exe using Assembly.LoadFile
  • get the Name and Description values for the exe

I can somehow get around the second step in that I can check the result of GetReferencedAssemblies contains the assembly of the BaseExe type. However, the last part completely evades me.

var path = @"C:\Code\ESG Server\ExeOutput\bin\debug";
string[] filePaths = Directory.GetFiles(path, "*.exe");

foreach (var exeReport in filePaths)
{
    Assembly exe = Assembly.LoadFile(exeReport);
    var exeType = exe.GetType();
    var referencedAssemblies = exe.GetReferencedAssemblies();   
}

How do check the base type of c# console applications?

Simplified exes

public class exe1 : BaseExe
{
    public exe1 ()
    {
        this.Name = "exe1";
        this.Description = "exe1";
    }

    static void Main(string[] args){  
       var exe1 = new exe1();
       /* snip */
    }
}

public class exe2 : BaseExe
{
    public exe2 ()
    {
        this.Name = "exe2";
        this.Description = "exe2";
    }

    static void Main(string[] args){  
       var exe2 = new exe2();
       /* snip */
    }
}

Edit: I just realized after type all of this that the Name and Description properties are set in the constructor. My current approach will not work. I am going to look into custom attributes as an alternative and use this to hold the information. The requirement still remains the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2024-12-16 13:22:31
using System;
using System.IO;
using System.Reflection;

class Sample {
    static void Main(){
        var path = @"C:\Code\ESG Server\ExeOutput\bin\debug";
        string[] filePaths = Directory.GetFiles(path, "*.exe");

        foreach (var exeReport in filePaths){
            Assembly exe = Assembly.LoadFile(exeReport);
            var exeType = exe.GetTypes()[0];//only one class;
            if(exeType.BaseType.Name != "BaseExe") continue;
            dynamic obj = exe.CreateInstance(exeType.Name);
            Console.WriteLine("Name:{0},Description:{1}", obj.Name, obj.Description);
        }
    }
}
using System;
using System.IO;
using System.Reflection;

class Sample {
    static void Main(){
        var path = @"C:\Code\ESG Server\ExeOutput\bin\debug";
        string[] filePaths = Directory.GetFiles(path, "*.exe");

        foreach (var exeReport in filePaths){
            Assembly exe = Assembly.LoadFile(exeReport);
            var exeType = exe.GetTypes()[0];//only one class;
            if(exeType.BaseType.Name != "BaseExe") continue;
            dynamic obj = exe.CreateInstance(exeType.Name);
            Console.WriteLine("Name:{0},Description:{1}", obj.Name, obj.Description);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文