none
Namespace Declaration and Reflection RRS feed

  • General discussion

  • This post pertains to differences in the way VB & C# handle namespace declaration. Any feedback is welcome, I'm not as proficient in C# and perhaps I'm missing something. Here's the background: I have a shell application that loads assemblies on the fly through a data driven API. I recently decided to start developing the client assemblies in C#, but ran into a wall when I tried loading them up using reflection:

    apd = AppDomain.CreateDomain("DomainSS1", Nothing, Nothing, Nothing, False)
                Dim oa(0) As Object
                oa(0) = Me.tools
                Dim o As Object = apd.Load(AssemblyName & ", Version=" & Version & " , Culture=" & Culture & ",  PublicKeyToken=" & PublicKeyToken).CreateInstance(
                    ObjectInstance, Nothing, Nothing, Nothing, oa, Nothing, Nothing)
    Assemblies written in VB load up fine. C# did not; turns out this was because in VB you can specify the assembly name and default namespace in the project properties without having to explicitly write them out in the individual class files. This does not appear to be the case in C#; once the namespaces were written out in the .cs class files, the object loaded fine. Making it difficult to debug was the fact that no error was thrown, object 'o' was simply nothing after the AppDomain.Load call.





    JB

    Friday, April 6, 2018 1:45 PM

All replies

  • But that is the main difference between VB and C#,

    In VB is much done "default", while in C# almost everything has to be explicitly written. There is much more about that. 

    The CP/M way of declaring variables from C# is not the main difference and neither is the parenthesis and the semicolon. 

    The resulting assemblies at the end are the same. (However, not any programmer does not make sometimes a not needed line so that can make differences). 


    Success
    Cor

    Friday, April 6, 2018 4:18 PM
  • I tried apd.Load using a simple C# assembly that contains just a class without namespace. It succeeded. Then the instance was created and invoked. Maybe the problem is reproducible in some specific circumstances.

    Friday, April 6, 2018 6:24 PM