C# 获取泛型类的所有运行时构造类的列表

发布于 2024-10-16 19:25:41 字数 1157 浏览 2 评论 0原文

我试图列出由泛型类创建的所有运行时构造类的列表。换句话说,如果我有一个类:

public GenericCls<T> {
    public void Reset() { ... }
    ...
}

并且我在不同的地方有这样的代码:

GenericCls<int> gci = new GenericCls<int>();
GenericCls<String> gcs = new GenericCls<String>();
GenericCls<float> gcf = new GenericCls<float>();
...

我可以得到这样的东西吗?:

Type[] allconstructed = GetAllConstructed(typeof(GenericCls<>));

它返回 {GenericCls,GenericCls,GenericCls,。 ..}

该用例涉及一个通用分配器,它支持任何类型的对象分配(类似于 new XXX(),但对垃圾收集器来说更好)。我不会详细说明,因为这只会使问题变得复杂。基本上,我不会在编译时知道所有构造的类,因为该库是一个 dll,旨在与单独的代码项目一起使用。所以我需要某种形式的反思,但我在互联网上似乎找不到。

Assembly.GetExecutingAssembly().GetExportedTypes() 不包含除基泛型类之外的任何内容(即 typeof(GenericCls<>))

typeof(GenericCls<> ;).GetGenericArguments() 仅返回类型“T”,这不仅是无效类型,而且完全无用。

如果您只知道泛型类的类型,是否有可能找到泛型类的所有构造类? (typeof(GenericCls<>);) 我不确定“构造”是否是正确的词 - 我想知道当前活动的所有具体泛型派生类,或者所有这些它将永远存在(不确定 C# 如何在幕后处理通用构造)。

I'm trying to make a list of all the runtime constructed classes created by a generic class. In other words, if I have a class:

public GenericCls<T> {
    public void Reset() { ... }
    ...
}

And I have code in various places like this:

GenericCls<int> gci = new GenericCls<int>();
GenericCls<String> gcs = new GenericCls<String>();
GenericCls<float> gcf = new GenericCls<float>();
...

Can I get something like this?:

Type[] allconstructed = GetAllConstructed(typeof(GenericCls<>));

which returns {GenericCls<int>,GenericCls<String>,GenericCls<float>,...}

The use case involves a generic allocator, that supports any type of object allocation (it's like new XXX(), but nicer to the garbage collector). I won't go into specifics, because it will just complicate the question. Basically, I will not know all the constructed classes at compile time, since the library is a dll intended to be used with a separate code project. So I will need some form of reflection that I can't seem to find on the interwebs.

Assembly.GetExecutingAssembly().GetExportedTypes() does not contain anything but the base generic class (i.e. typeof(GenericCls<>))

typeof(GenericCls<>).GetGenericArguments() returns only Type "T", which is not only an invalid type, but entirely useless.

Is it even possible to find all constructed classes of a generic class if you only know the generic class' type? (typeof(GenericCls<>);) I'm not sure if "constructed" is the right word - I want to know either all the concrete generic-derived classes currently active, or all of these that will ever exist (not sure how C# handles generic construction behind the scenes).

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

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

发布评论

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

评论(2

七色彩虹 2024-10-23 19:25:41

@David Mårtensson:你的回答给了我一个想法。我可以在任何非泛型类中创建类型的静态列表,并在构造每个构造类时注册它(当 T 已知时)。

static public class ConcreteList {
    static public List<Type> concrete;
}
public class GenericCls<T> {
    static GenericCls() {
        ConcreteList.concrete.Add(typeof(GenericCls<T>));
    }
}

我用 ConcreteList.concrete[x].GetGenericArguments() 检查了它,它正在工作。哦,快点。

@David Mårtensson: Your answer gave me an idea. I could make a static list of types in any non-generic class, and register each constructed class as it was constructed (when T is known).
i.e.

static public class ConcreteList {
    static public List<Type> concrete;
}
public class GenericCls<T> {
    static GenericCls() {
        ConcreteList.concrete.Add(typeof(GenericCls<T>));
    }
}

I checked it with ConcreteList.concrete[x].GetGenericArguments(), and it's working. Oh snap.

十二 2024-10-23 19:25:41

您可以使用工厂类来创建实例,这样工厂类可以保留所有创建的类的列表。

You might use a factory class to create instances, that way the factory class could keep a list of all created classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文