英文:
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 = ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论