小编The*_*esy的帖子

通用类中的静态列表

我有以下代码(一个更复杂项目的简单示例),其中我有一个对象类型列表的静态"主"列表.

如果您单步执行代码,我希望通过构造函数创建第二个referenceManager3类型时,_masterList将包含String和Object列表.但事实并非如此.

我假设这是因为ReferenceManager3的每个实例实际上是不同的类类型,因为泛型类型定义.我认为这是正确的吗?

我怎样才能做到这一点?

class Program
{
    static void Main(string[] args)
    {
        ReferenceManager3<string> StringManager = new ReferenceManager3<string>();
        ReferenceManager3<object> IntManager = new ReferenceManager3<object>();
    }
}

class ReferenceManager3<T> where T : class //IReferenceTracking
{
    // Static list containing a reference to all Typed Lists
    static List<IList> _masterList = new List<IList>();

    // Object Typed List
    private List<T> _list = null;

    public ReferenceManager3()
    {
        // Create the new Typed List
        _list = new List<T>();

        // Add it to the Static Master List
        _masterList.Add(_list); // …
Run Code Online (Sandbox Code Playgroud)

c# generics static-members

2
推荐指数
1
解决办法
881
查看次数

使用字符串到接口的GetType

我为Control DLL创建了一个DLL加载器(这些控件具有相同的接口iControlPlugin),一切都很好用.但是我现在需要为设备驱动程序创建一个DLL加载器.我开始查看我的Control Loader代码,并意识到代码是相同的,除非定义了接口类型.因此,我想使我的控制加载器功能更通用,以便它可以用于不同的接口类型.

我修改了函数,以便它返回一个字典,我遇到的问题是我做了一个检查,以确保DLL文件包含匹配的类型,在我的情况下,这是两种接口类型之一.但我总是无效.

这是我原来的工作功能,专门针对一种类型(iLPPControlPlugin)

public Dictionary<string, iLPPControlPlugin> LoadPlugins(string folder)
{
    /* Create a Dictionary object to store the loaded DLL's */
    Dictionary<string, iLPPControlPlugin> plugsins = new Dictionary<string,iLPPControlPlugin>();

    /* Get all the DLL files from the path specified */
    string[] files = Directory.GetFiles(folder, "*.dll");

    /* Go through each DLL... */
    foreach (string file in files)
    {
        /* Load the file, as an assembly. */
        Assembly assembly = Assembly.LoadFile(file);

        /* Get the type, can be more than one */
        var types …
Run Code Online (Sandbox Code Playgroud)

c# dll interface gettype

0
推荐指数
1
解决办法
3749
查看次数

标签 统计

c# ×2

dll ×1

generics ×1

gettype ×1

interface ×1

static-members ×1