Monday, March 21, 2011

GetType Gotchas in VB .NET

Since VB .NET has a built in GetType method , some confusion is created when using GetType in VB .NET.

I will conclude following two points from my experience :

1. Use the VB .NET GetType  to get types  of System defined types.
2. Use System.Type.GetType("FullyqualifiedTypeName , AssemblyName") to get Type of user
defined types.

In the following, "MyServiceClass" is a class defined in an external class library ClassLibrary1. Further the class
implements IMyService interface, which is also defined in the same class library. Here the class and interface are being
refered outside of the class library, in a differenct project, with a reference to the ClassLibrary1.dll added.



Imports SystemImports System.TypeImports System.ServiceModelImports System.ServiceModel.DescriptionImports ClassLibrary1Public





sh.Description.Behaviors.Add(mbehave)
sh.AddServiceEndpoint(
sh.AddServiceEndpoint(
sh.AddServiceEndpoint(System.Type.GetType(
sh.AddServiceEndpoint(System.Type.GetType(
sh.Open()

End
Class Form1Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadDim tcpa As New Uri("net.tcp://localhost:8000")Dim sh As New ServiceHost(System.Type.GetType("ClassLibrary1.MyServiceClass,ClassLibrary1"), New Uri("net.pipe://localhost"), tcpa)Dim tcpb As New NetTcpBindingDim mbehave As New ServiceMetadataBehaviorGetType(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex")GetType(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding, "mex")"ClassLibrary1.IMyService,ClassLibrary1"), tcpb, tcpa)"ClassLibrary1.IMyService,ClassLibrary1"), New NetNamedPipeBinding, New Uri("net.pipe://localhost"))End Sub Class

No comments:

Post a Comment