如何获取已实例化的类列表

huangapple go评论60阅读模式
英文:

How can I get a list of classes that have been instantiated

问题

我正在使用vb.net,并且正在使用反射在运行时创建类的实例:

Dim my_ass As Assembly = Assembly.GetExecutingAssembly
Dim my_class As New Object
my_class = my_ass.CreateInstance(my_class_name, True, BindingFlags.OptionalParamBinding, Nothing, New Object() {param_list}, Nothing, Nothing)

但我不想创建多个相同类的实例,因此我尝试检查是否已经创建了一个。我不知道类会存在在哪里,除了在根命名空间中,所以我需要一种方法来迭代所有已创建的类并查找它们,类似于以下方式:

For Each c As Class In Main
    If c.Name/Type = my_class_name Then
        c.RunMyFunction()
    Else
        ' 创建一个新实例
        my_class = my_ass.CreateInstance(my_class_name, True, BindingFlags.OptionalParamBinding, Nothing, New Object() {param_list}, Nothing, Nothing)
    End If
Next

当然,我的示例是虚构的,但这就是我需要的要点。

英文:

I'm using vb.net and I'm creating instances of classes at runtime using reflection:

Dim my_ass As Assembly = Assembly.GetExecutingAssembly
Dim my_class As New Object
my_class = my_ass.CreateInstance(my_class_name, True, BindingFlags.OptionalParamBinding, Nothing, New Object() {param_list}, Nothing, Nothing)

But I don't want to create multiple instances of the same class so I'm trying to check to see if I have already created one. I don't know where the class would exist except in the root namespace so I need a way to iterate through all of the created classes and find them like:

for each c as class in Main
 if c.name/type = my_class_name then
  c.run_my_function()
 else
  ' create a new instance
  my_class = my_ass.CreateInstance(my_class_name, True, BindingFlags.OptionalParamBinding, Nothing, New Object() {param_list}, Nothing, Nothing)
 end if
next

Of course my example is fictional but that is the gist of what I need.

答案1

得分: 1

拿这段代码例子

    Static classCache As New Dictionary(Of String, Object)
    Dim my_class As Object 
    If Not classCache.TryGetValue(my_class_name, my_class) Then
        Dim my_ass As Assembly = Assembly.GetExecutingAssembly()
        my_class = my_ass.CreateInstance(my_class_name, True, 
            BindingFlags.OptionalParamBinding, Nothing, New Object() {param_list}, 
            Nothing, Nothing)
        classCache(my_class_name) = my_class
    End If

`Static` 是VB.Net中一个特殊的特性就我所知),在大多数其他语言中的`static`特性不同它允许你声明变量以便它的作用范围是在一个方法内并且确保了对该变量的访问是线程安全的
    
---

最后对于问题中的这种模式个人总是有点担心

    Dim my_class As New Object
    my_class = ...

这通常表明某人可能不太了解正在发生的事情因为它告诉计算机为一个新对象分配内存然后立即将其丢弃而从未使用该代码总是可以安全地简化以提高效率就像这样

    Dim my_class As Object = ...

或者如果你的编码标准不允许你在同一行上声明和赋值可以这样写

    Dim my_class As Object
    my_class = ...
英文:

Take this code:

Dim my_ass As Assembly = Assembly.GetExecutingAssembly
Dim my_class As New Object
my_class = my_ass.CreateInstance(my_class_name, True, 
BindingFlags.OptionalParamBinding, Nothing, New Object() {param_list}, 
Nothing, Nothing)

And change it like this:

Static classCache As New Dictionary(Of String, Object)
Dim my_class As Object 
If Not classCache.TryGetVale(my_class_name, my_class) Then
    Dim my_ass As Assembly = Assembly.GetExecutingAssembly()
    my_class = my_ass.CreateInstance(my_class_name, True, 
        BindingFlags.OptionalParamBinding, Nothing, New Object() {param_list}, 
        Nothing, Nothing)
    classCache(my_class_name) = my_class
End If

Static is a special feature that is (as far as I know) unique to VB.Net. It's different from the static feature in most other languages in that it lets you declare the variable so it's scoped to a method and also ensures thread-safety for access.


Finally, FWIW this pattern from the question always scares me a bit:

Dim my_class As New Object
my_class = ...

It's often a sign someone might not really know what's going on, because it tells the computer to allocate memory for a new object and then immediately throws it away without ever using it. That code can ALWAYS be safely simplified to improve efficiency like this:

Dim my_class As Object = ...

Or like this if you have some weird coding standard that won't let you declare and assign on the same line:

Dim my_class As Object
my_class = ...

huangapple
  • 本文由 发表于 2023年2月24日 05:55:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定