的GetType一个类中引用的程序集失败 [英] GetType on a class in a referenced assembly fails
问题描述
我有一个asp.net web项目引用域项目。
I have an asp.net web project that references a domain project.
在Web项目中,我想使用反射来创建域项目的类的实例,但我总是空(没有,在VB中)。
From within the web project, I want to create an instance of a class from the domain project using reflection, but I always get null (Nothing, in VB).
注:我公司采用非完全限定的类名,并希望为MSDN似乎表明搜索将被执行(在大会级)
NOTE: I am using the non-fully qualified class name, and was hoping that a search would be performed as MSDN seems to indicate (at the Assembly level)
暗淡作为的myType类型= Type.GetType(MetricEntity) //得到任何(空)
Dim myType as Type = Type.GetType("MetricEntity") '// yields Nothing (Null)
'// lets try this
Dim WasFound As Boolean = False
For Each ObjectType In Me.GetType.Assembly.GetExportedTypes
If ObjectType.Name = theClassName Then
WasFound = True
Exit For
End If
Next
在这个问题的答案似乎通常是:
The answer to this question seems to typically be:
Dim myType as Type = Type.GetType("System.Linq.Enumerable, System.Core, "
+ "Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
不过,我看不出有到c版本号硬$ C $的逻辑(或在一个配置文件来放置)......所以,如果版本的变化,我忘记在更新会发生什么反射code .....是有可能做的GetType,忽略版本,区域性和公钥?
But I don't see the logic of having to hardcode the version number (or have to place in a config file)....so what happens if the version changes and I forget to update it in the reflection code.....is it possible to do a GetType, ignoring Version, Culture, and PublicKeyToken?
推荐答案
您可以做这样的事情,忽略这些属性:
You can do something like this, ignoring those attributes:
Dim myType as Type = Type.GetType("System.Linq.Enumerable"));
或
Dim myType as Type = Type.GetType("System.Linq.Enumerable, System.Core"));
这篇关于的GetType一个类中引用的程序集失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!