简体   繁体   中英

Doing c# class library a runnable application

I want to create a console applicaiton and add it to another class library projects. So my class libraries will be runnable applicaitons.

在此处输入图片说明

namespace MyWorker.Host
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }

    public interface IConfiguration
    {
        void Configure();
    }
}

And I am adding my MyWorker.Host as reference to MyWorker.Client class library project. And debug settings set as "Start external program".

在此处输入图片说明

And now I want to search client assemblies that referenced MyWorker.Host . I will find classes that implemented IConfiguration interface.

namespace MyWorker.Host
{
    class Program
    {
        static void Main(string[] args)
        {
            var implementations = AppDomain
                .CurrentDomain
                .GetAssemblies()
                .SelectMany(assemly => assemly.GetTypes())
                .Where(t => !t.IsInterface)
                .Where(t => typeof (IConfiguration).IsAssignableFrom(t));

            foreach (var assembly in implementations)
            {
                Console.WriteLine(assembly.FullName);
            }

            Console.Read();
        }
    }
}

But console writes nothing .

(If I add a concrate class in MyWorker.Host it writes console.)

I've done a similar thing before but had to look through DLLs for classes that implement the interface you're after using Reflection. Of course you need a means of passing in that path, which could be configurable, or just look through all subdirectories from the solution directory, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM