Late binding Outlook – VB.NET versus C#

Inca o data, cind e vorba de COM, VB este mult mai bun decit C#.
Incercam sa creez un email cu Late binding in C#:

Type app = Type.GetTypeFromProgID(“Outlook.Application”);
object oApp = Activator.CreateInstance(app);
object oNameSpace = app.GetMethod(“GetNamespace”).Invoke(oApp, new object[] { “MAPI” });
oNameSpace.GetType().GetMethod(“Logon”).Invoke(oNameSpace, new object[] { Type.Missing, Type.Missing, Type.Missing, Type.Missing});

Object mail = oApp.GetType().GetMethod(“CreateItem”).Invoke(oApp, new object[1] { 0 });
Type t = mail.GetType();
t.GetMethod(“Display”).Invoke(mail, new object[] { false });

Eroare….tipul t nu are metoda Display …

Aproape acelasi cod, mai usor scris , in VB.NET

Dim app As Type = Type.GetTypeFromProgID(“Outlook.Application”)
Dim oApp As Object = Activator.CreateInstance(app)
Dim oNameSpace As Object = oApp.GetNamespace(“MAPI”)
oNameSpace.Logon()
Dim mail As Object = oApp.CreateItem(0)
mail.Display(False)

Normal ca merge ?

Diferentele intre ele dpdv IL ? Daca va uitati cu reflector la ceea ce genereaza VB.NET, o sa vedeti ca nu e pur si simplu Reflection chior – ci tot felul de incercari   in speranta de  a da peste metoda respectiva….

Concluzie:
Daca aveti de a  face cu COM(Office), faceti un DLL de VB.NET -iesiti mai ieftin si mai rapid si mai curat ( e.g., eliminarea Type.Missing)

Leave a Reply

Your email address will not be published. Required fields are marked *