tags:

views:

51

answers:

2

I need to convert this code from C# to VB. I'm not sure of the proper syntax.

C#

[XcoWorkerExtension(Optional = new Type[] { typeof(Subscribe<OnNewsArrived>) })]
    private readonly XcoPublisher<OnNewsArrived> publisher = new    XcoPublisher<OnNewsArrived>();

This is what I've come up with in VB:

<XcoWorkerExtension([Optional]:=New Type() {GetType(Subscribe(Of OnNewsArrived))})> _
    Private ReadOnly publisher As New XcoPublisher(Of OnNewsArrived)()

The C# version runs fine but when I try to run the VB version I'm getting this exception:

System.IO.FileLoadException was unhandled Message=The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) Source=mscorlib

The exception is generated at the first line of this sub:

internal XcoWorkerExtensionAttribute Get_worker_extension_attribute(FieldInfo field)
    {
        object[] fieldAttrs = field.GetCustomAttributes(typeof(XcoWorkerExtensionAttribute), false);
        object[] classAttrs = field.FieldType.GetCustomAttributes(typeof(XcoWorkerExtensionAttribute), false);
        if (fieldAttrs.Length > 0 && classAttrs.Length == 0)
            throw new XcoWorkerException("A field can only be marked with the XcoWorkerExtension attribute when its type is also marked with this attribute");

        if (fieldAttrs.Length > 0)
            return (XcoWorkerExtensionAttribute)fieldAttrs[0];
        if (classAttrs.Length > 0)
            return (XcoWorkerExtensionAttribute)classAttrs[0];
        return null;
    }
+1  A: 

Sorry for giving a "meta answer".

For small conversions like this, Reflector is a nice tool if you are unsure about syntax and/or results.

Possibilities of use:

  1. Compile in C#, and decompile to VB.Net.
  2. Compile in VB.Net, compare to original
GvS
Ok well I guess I've eliminated synatx as the cause of the error. I used Reflector and came up the same exact code.
jw7694
A: 

C# to VB Converter

Worked for one of my projects, though I suspect there are a few things that will make it unhappy. And you as well.

Carlos
I assume he already tried this, seeing as how it generates the same VB.NET code he's using.
R. Bemrose
I tried this converter and another as well and both came up with the same results.
jw7694
Fair enough, I hadn't tried plugging in your code. Have you done standard stuff like making sure the VB project has the same references, uses the same version of .NET, etc?
Carlos
I created another project from scratch to make sure it wasn't something like that and had the same results. I noticed that I can run the project fine if I remove the attribute from the Publisher deceleration.
jw7694