开发者

How to check the base type of a c# console application?

开发者 https://www.devze.com 2023-04-13 03:45 出处:网络
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

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.开发者_如何学Python


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号