开发者

GetType on a class in a referenced assembly fails

I have an asp.net web project that references a domain project.

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).

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)

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 = t开发者_JAVA技巧heClassName 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")); 

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 get a type by name alone if you have the Assembly it's in. Would that work for you?

That way you could specify the assembly name in a separate location from the types you're trying to access.

Assembly assembly = typeof(System.Linq.Enumerable).Assembly;
Type type = assembly.GetType("System.Linq.Enumerable");


You can do something like this, ignoring those attributes:

Dim myType as Type = Type.GetType("System.Linq.Enumerable")); 

or:

Dim myType as Type = Type.GetType("System.Linq.Enumerable, System.Core")); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜