簡體   English   中英

使用反射動態實例化擴展基類的類

[英]Dynamically instantiate classes extending baseclass using reflection

很長時間以來,我一直在努力尋找一種方法來動態實例化擴展特定基類的所有類(在運行時)。 據我所讀,應該使用Reflection來完成,不幸的是我還沒有弄清楚該怎么做。

我的項目結構如下:

Library
--|
  |
  --Vehicle.cs (abstract class)
  |
  --Car.cs (extending vehicle)
  |
  --Bike.cs (extending vehicle)
  |
  --Scooter.cs (extending vehicle)
  |
  --InstanceService.cs (static class)
  |
  |
ConsoleApplication
--|
  |
  --Program.cs

InstanceService類包含一個通用方法,該方法應返回IEnumerable<T> ,該方法包含擴展Vehicle的實例化類,即Car, Bike & Scooter

在嘗試了許多不同的解決方案之后,下面介紹的代碼是InstanceService類的當前狀態,這意味着它主要包含調試工具。

InstanceService.cs

using System;
using System.Collections.Generic;

namespace Library
{
    public static class InstanceService<T>
    {
        //returns instances of all classes of type T
        public static IEnumerable<T> GetInstances()
        {

            var interfaceType = typeof(T);
            List<T> list = new List<T>();
            Console.WriteLine("Interface type: " + interfaceType.ToString());
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach(var assembly in assemblies)
            {
                Console.WriteLine("Assembly: " + assembly.ToString());
                if (assembly.GetType().IsAbstract)
                {
                    var instance = (T) Activator.CreateInstance(assembly.GetType());
                    list.Add(instance);
                }
            }
            return list;
        }
    }
}

我還附加了抽象Vehicle類的代碼以及其實現之一。

Vehicle.cs

namespace Library
{
    public abstract class Vehicle
    {
        protected float maxSpeedInKmPerHour;
        protected float weightInKg;
        protected float priceInDkk;
    }
}

Car.cs

namespace Library
{
    public class Car : Vehicle
    {

        public Car()
        {
            this.maxSpeedInKmPerHour = 1200;
            this.weightInKg = 45000;
            this.priceInDkk = 71000000;
        }
    }
}

我認為您應該感興趣的方法是IsAssignableFrom

此外,如果允許使用LINQ,則使用LINQ可以使代碼更加容易,並且由於您一次創建一個對象,因此我建議使用yield return

static IEnumerable<T> GetInstances<T>() 
{
    var baseType = typeof(T);
    var types = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany( a => a.GetTypes() )
        .Where
        (
            t => baseType.IsAssignableFrom(t)                  //Derives from base
              && !t.IsAbstract                                 //Is not abstract
              && (t.GetConstructor(Type.EmptyTypes) != null)   //Has default constructor
        );


    foreach (var t in types)
    {
        yield return (T)Activator.CreateInstance(t);
    }
}

或者,如果出於某種原因而炫耀,並且您想使用一條語句來做到這一點:

    var types = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany( a => a.GetTypes() )
        .Where
        (
            t => typeof(T)IsAssignableFrom(t)  
              && !t.IsAbstract 
              && (t.GetConstructor(Type.EmptyTypes) != null) 
        )
        .Select
        (
            t => (T)Activator.CreateInstance(t)
        );

該方法適用於可以使用默認構造函數實例化的任何類型。 除非我遺漏了某些東西,否則您的類是從另一個類派生的事實就不重要了。

private T MakeInstance<T>()
{
    // the empty Type[] means you are passing nothing to the constructor - which gives
    // you the default constructor.  If you need to pass in an int to instantiate it, you
    // could add int to the Type[]...
    ConstructorInfo defaultCtor = typeof(T).GetConstructor(new Type[] { });

    // if there is no default constructor, then it will be null, so you must check
    if (defaultCtor == null)
        throw new Exception("No default constructor");
    else
    {
        T instance = (T)defaultCtor.Invoke(new object[] { });   // again, nothing to pass in.  If the constructor needs anything, include it here.
        return instance;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM